每当我尝试运行代码时,在我输入我想购买多少张彩票后,它都会给我一个错误。这是用于彩票程序的。这是代码
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.");
}
}
}
例外情况:
线程中的异常\"main"java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 atjava.lang.String.charat(String.java:658)atlottery.Lottery.main(Lottery.java:32)C:\用户\Ben\AppData\本地\NetBeans\缓存\8.1\执行器片段\run.xml:53:Java返回:1
这是我尝试运行程序时显示的异常错误。帮助将不胜感激!
该错误是由< code>Scanner的< code>nextInt方法的行为引起的。此外,您的代码还有其他错误。
< code>nextInt方法只读取下一个整数,不读取其他任何内容。这意味着不读取新行字符(取决于操作系统,\n或\r或\r\n)。这很好,直到您调用< code>nextLine。< code>nextLine读取每个字符,直到遇到新的行字符。因此< code>nextLine看到这个未读的新行字符,并停止进一步读取,它返回一个空字符串,然后将这个空字符串放入< code>guess变量中。当您尝试使用< code>charAt(0)访问< code>guess的第一个字符时,它会崩溃,因为< code>guess没有任何字符。
我建议您使用< code > integer . parse int(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在评论中所说,我使用了< code > string . value of(rand num)。