尝试运行代码时出现异常。字符串索引超出范围

时间:2016-12-16 10:26:33

标签: java

每当我尝试运行代码时,在输入我想要购买的票数后,它会给我一个错误。这是一个彩票计划。这是代码

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.");

       }
    }
}

例外:

  

线程中的异常\&#34; main&#34; java.lang.StringIndexOutOfBoundsException:   字符串索引超出范围:0 at   java.lang.String.charAt(String.java:658)at   lottery.Lottery.main(Lottery.java:32)   C:\用户\本\应用程序数据\本地\的NetBeans \缓存\ 8.1 \执行人-片段\ run.xml:53:   Java返回:1

这是我尝试运行程序时显示的异常错误。 非常感谢帮助!

1 个答案:

答案 0 :(得分:0)

错误是由nextInt的{​​{1}}方法的行为引起的。此外,您的代码也有其他错误。

Scanner方法只读取下一个整数,而不读取任何其他整数。这意味着不读取新行字符(\ n或\ r或\ r \ n取决于操作系统)。这可以,直到你拨打nextIntnextLine读取每个字符,直到遇到换行符。所以nextLine看到这个未读的新行字符并停止读取任何进一步的字符并返回一个空字符串,然后将其放入nextLine变量中。当您尝试使用guess访问guess的第一个字符时,它会崩溃,因为charAt(0)没有任何字符。

我建议你使用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作为XtremeBaumer在评论中说过。