2016-12-16 126 views
0

每當我嘗試運行代碼時,在輸入我想要購買的票數後,它會給我一個錯誤。這是一個彩票計劃。下面是代碼嘗試運行代碼時出現異常。字符串索引超出範圍

import java.util.Scanner; 
public class Lottery { 
public static void main(String[] args) { 
    //local data 
    String guess; 
    int numTickets; 
    int counter; 
    Scanner in = new Scanner(System.in); 

    //output 
    System.out.println("How many tickets would you like to buy?"); 
    numTickets = in.nextInt(); 
    for (counter = 0; counter < numTickets; counter++) { 
     System.out.println("Please enter your three numbers (e.g. 123): "); 
     guess = in.nextLine(); 
     int randNum = (int) (Math.random() * 1000); 
     String randNumb; 
     randNumb = Integer.toString(randNum); 
     char ch1 = randNumb.charAt(0); 
     char ch2 = randNumb.charAt(1); 
     char ch3 = randNumb.charAt(2); 

     if (ch1 == guess.charAt(0) && ch2 == guess.charAt(1) && ch3 == guess.charAt(2)) { 
      System.out.print("Winner: " + randNumb); 
      System.out.print("Congratulations, both pairs matched. /n"); 
     } else if (ch3 == guess.charAt(2) && ch2 == guess.charAt(1)) { 
      System.out.print("Winner: " + randNumb); 
      System.out.print("Congratulations, the end pair matched."); 
     } else if (ch1 == guess.charAt(0) && ch2 == guess.charAt(1)) { 
      System.out.print("Winner: " + randNumb); 
      System.out.print("Congratulations, the first pair matched."); 
     } else { 
      System.out.print("The Correct Number: " + randNumb); 
     } 
     System.out.println("\t Sorry, no matches, You only had"); 
     System.out.println("\t one chance out of 100 to win anyway."); 

     } 
    } 
} 

例外:

異常螺紋\ 「主」 java.lang.StringIndexOutOfBoundsException: 字符串索引超出範圍:在 java.lang.String.charAt 0( String.java:658)at lottery.Lottery.main(Lottery.java:32) C:\ Users \ Ben \ AppData \ Local \ NetBeans \ Cache \ 8.1 \ executor-snippets \ run.xml:53: Java返回:1

這是我嘗試運行該程序時顯示的異常錯誤。 幫助將不勝感激!

+0

更好用String.valueOf();而不是Integer.toString(); (有幾個問題)。 nextLine和netxInt也存在問題。谷歌它 – XtremeBaumer

+0

你有沒有嘗試用調試器通過你的代碼? –

+0

請注意'Math.random()* 1000'會給出一個介於0和1000之間的值,它可能是一個小於三位的值。 – Berger

回答

0

該錯誤是由ScannernextInt方法的行爲引起的。另外,你的代碼也有其他錯誤。

nextInt方法只讀取下一個整數,沒有別的。這意味着不會讀取新行字符(\ n或\ r或\ r \ n,具體取決於操作系統)。在您致電nextLine之前,這很好。 nextLine會讀取每個字符,直到遇到新的行字符。所以nextLine看到這個未讀的新行字符,並停止讀取任何進一步,它返回一個空字符串,然後將其放入您的guess變量。當您嘗試使用charAt(0)訪問guess的第一個字符時,它會崩潰,因爲guess沒有任何字符。

我建議你使用Integer.parseInt(in.nextLine())從用戶讀取一個整數。

我已經固定的代碼爲您提供:

String guess; 
int numTickets; 
int counter; 
Scanner in = new Scanner(System.in); 

//output 
System.out.println("How many tickets would you like to buy?"); 
numTickets = Integer.parseInt(in.nextLine()); 
Random rand = new Random(); 
for (counter = 0; counter < numTickets; counter++) { 
    System.out.println("Please enter your three numbers (e.g. 123): "); 
    guess = in.nextLine(); 
    int randNum = rand.nextInt(1000); 
    String randNumb = String.format("%03d", randNum); 
    char ch1 = randNumb.charAt(0); 
    char ch2 = randNumb.charAt(1); 
    char ch3 = randNumb.charAt(2); 

    if (ch1 == guess.charAt(0) && ch2 == guess.charAt(1) && ch3 == guess.charAt(2)) { 
     System.out.print("Winner: " + randNumb); 
     System.out.print("Congratulations, both pairs matched. /n"); 
    } else if (ch3 == guess.charAt(2) && ch2 == guess.charAt(1)) { 
     System.out.print("Winner: " + randNumb); 
     System.out.print("Congratulations, the end pair matched."); 
    } else if (ch1 == guess.charAt(0) && ch2 == guess.charAt(1)) { 
     System.out.print("Winner: " + randNumb); 
     System.out.print("Congratulations, the first pair matched."); 
    } else { 
     System.out.print("The Correct Number: " + randNumb); 
    } 
    System.out.println("\t Sorry, no matches, You only had"); 
    System.out.println("\t one chance out of 100 to win anyway."); 

} 

正如你所看到的,我做了其他改進。首先,Math.random() * 1000可以返回非三位數字,因此我將其更改爲使用Random對象。使用rand.nextInt(900) + 100只會生成三位數字。

其次,我使用String.valueOf(randNum)作爲XtremeBaumer在評論中說過。

+1

'String.valueOf'和'Integer.toString'之間沒有可檢測到的差異。事實上,人們只是稱呼另一方。 –

+0

另外,您已更改規格。現在ch1總是9,從不隨意。你應該改用'int randNum = rand.nextInt(1000); String randNumb = String.format(「%03d」,randNum);' –

+0

@KlitosKyriacou ch1並不總是9.我測試了它,得到了636,408,886! – Sweeper

相關問題