2013-03-16 66 views
0

我收到該錯誤,我不知道爲什麼。我已經嘗試了一些不同的東西,比如從(!(flag... then == '+'開始,另一個與==開始,其中do語句下面的行也出現錯誤。任何人都看到了問題?我現在想要得到的主要目標是for循環重複打印繩索,並在另一個地方向左或向右打印旗幟。Do-while循環「字符串索引超出範圍」

package program2; 

import java.util.Scanner; 
import java.lang.Math; 

public class Program2 { 

public static int MAX_LENGTH = 21; 
public static int MIN_LENGTH = 5; 

public static void main(String[] args) { 
    Scanner keyboard = new Scanner(System.in); 
    System.out.print("Enter the length of the rope: "); 
    int ropeLength = keyboard.nextInt(); 
    while (ropeLength < MIN_LENGTH || ropeLength > MAX_LENGTH || ropeLength % 2 != 1) { 
     System.out.println("Thats not a valid length (odd number between 5 and 21)"); 
     System.out.print("Enter the length of the rope: "); 
     ropeLength = keyboard.nextInt(); 
    } 
    char a; 
    String flag = "+"; 
    for (int i = 0; i < ropeLength/2; i += 1) { 
     System.out.print("-"); 
    } 
    System.out.print(flag); 

    for (int i = 0; i < ropeLength/2; i += 1) { 
     System.out.print("-"); 
    } 
    System.out.println(""); 


    do { 
     //a = flag.charAt(ropeLength); 
     double rand = Math.random(); 

     if (rand > 0.5) { 
      for (int i = 0; i < (ropeLength/2) - 1; i++) { 
       System.out.print("-"); 
      } 

      System.out.print(flag); 

      for (int i = 0; i < (ropeLength/2) + 1; i++) { 
       System.out.print("-"); 
      } 
     if (rand < 0.5) { 
      for (int i = 0; i < (ropeLength/2) + 1; i++) { 
       System.out.print("-"); 
       } 

      System.out.print(flag); 

      for (int i = 0; i < (ropeLength/2) - 1; i++) { 
       System.out.print("-"); 
       } 

      } 
     } 
    } while (flag.charAt(0) != '+' || flag.charAt(ropeLength - 1) != '+'); 

} 

}

,並作爲do while循環,我的循環似乎只被重複一次或兩次。

do { 
    //a = flag.charAt(ropeLength); 
    double rand = Math.random(); 

    if (rand > 0.5) { 
     for (int i = 0; i < (ropeLength/2) - 1; i++) { 
      System.out.print("-"); 
     } 

     System.out.print(flag); 

     for (int i = 0; i < (ropeLength/2) + 1; i++) { 
      System.out.print("-"); 
     } 
    if (rand < 0.5) { 
     for (int i = 0; i < (ropeLength/2) + 1; i++) { 
      System.out.print("-"); 
      } 

     System.out.print(flag); 

     for (int i = 0; i < (ropeLength/2) - 1; i++) { 
      System.out.print("-"); 
      } 

     } 
    } 
} while (flag.charAt(0) != '+' || flag.charAt(ropeLength - 1) != '+'); 

}

和最後一件事,我需要,我有權下做註釋掉的代碼?

+0

假設ropeLength = 13。'flag.charAt(ropeLength - 1)'的值應該是什麼? – Ingo 2013-03-16 00:53:12

+0

你標誌變量的長度爲1. – 2013-03-16 01:00:58

回答

1

您有:

String flag = "+"; 

,你永遠不會修改它。所以,當你有條件:

flag.charAt(ropeLength - 1) != '+' 

除了如果ropeLength等於1,它總是會超出範圍。

關於您的代碼的實際行爲,正如我所提到的,您從不修改flag變量。所以它的第一個字符將始終是'+',因此您的循環將始終執行一次。

因此,我根據您的目標與您的代碼看到的第一個問題是您使用flagprint/println方法的方式。如果你想知道'+'在哪裏,你可以這樣使用StringBuilder

前:

String flag = "+"; 
for (int i = 0; i < ropeLength/2; i += 1) { 
    System.out.print("-"); 
} 
System.out.print(flag); 

for (int i = 0; i < ropeLength/2; i += 1) { 
    System.out.print("-"); 
} 
System.out.println(""); 

後:

StringBuilder flag = new StringBuilder(ropeLength); 
for (int i = 0; i < ropeLength/2; i += 1) { 
    flag.append('-'); 
} 
flag.append('+'); 

for (int i = 0; i < ropeLength/2; i += 1) { 
    flag.append('-'); 
} 

System.out.println(flag.toString()); 

// Resetting the StringBuilder for reuse 
flag.setLength(0); 

然而,關於DO/while循環,你應該修改你的整個一個算法。這樣寫的方式,如果您像上面提到的那樣使用StringBuilder,則'+'將只能無限期地圍繞「繩索」的中心抖動。我很確定這不是真正的意圖。

+0

哦,你先做了。 – 2013-03-16 01:02:48

+0

因此,我刪除我的答案,就像你的答案一樣。 – 2013-03-16 01:03:05

+0

爲'flag.charAt(ropeLength)!='+''部分,not not notequals只是檢查是否有一個+的字符? – tawnpawt 2013-03-16 01:11:04

相關問題