[英]Java string index out of bound , CharAt problem
我的代码抛出错误“字符串索引越界:1”,这是代码:
package inlamningsuppgift2;
import java.util.Random;
import java.util.Scanner;
import java.lang.Math;
public class inlamningsuppgift2del2 {
public static void main(String[] args) {
System.out.println("Guess a number between 01-99");
randomNumber();
}
public static void randomNumber () {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int value = Integer.valueOf(scanner.nextLine());
String str1 = Integer.toString(value);
String sa = String.format("%02d", value);
int randomNum = 10 * random.nextInt(9);
int randomNum2 = 1 * random.nextInt(9) +1;
int wholeNum = randomNum + randomNum2;
String str2 = Integer.toString(randomNum + randomNum2);
String s = String.format("%02d", randomNum + randomNum2);
System.out.println("You guessed :" + sa);
System.out.println("The correct number is : " + s);
if (str2.charAt(0) == str1.charAt(0) && str2.charAt(1) == str1.charAt(1)) {
System.out.println("Congratulations, you guessed the right number in the right order and won 1000$!");
}
else if (str1.charAt(0) == str2.charAt(1) && str1.charAt(1) == str2.charAt(0) ) {
System.out.println("Congratulations, you guessed the right number but in the wrong order and won 500$ !");
}
else if (str2.charAt(0) == str1.charAt(0) && str2.charAt(1) != str1.charAt(1)
|| str2.charAt(0) != str1.charAt(0) && str2.charAt(1) == str1.charAt(1)
|| str1.charAt(1) == str2.charAt(0) && str1.charAt(0) != str2.charAt(1)
|| str1.charAt(0) == str2.charAt(1) && str1.charAt(1) != str2.charAt(0)) {
System.out.println("Congratulations, you guessed one of the numbers right!");
}
else {
System.out.println("Sorry you guessed wrong, try again");
}
}
}
分配说明是:“彩票程序生成一个介于 01-99 之间的两位数。如果用户以正确的顺序猜对了数字,他们将赢得 1000$。如果用户以错误的顺序猜对了数字,示例 16 “他们赢了 500 美元,而不是 61。如果用户猜对了其中一个数字,他们就赢了 100 美元。”
当用户输入和/或 randomNum 为 10 及以上时,代码工作正常。 但是,当它低于 10 时,会发生错误,因为该值被读取为 1 个字符而不是 2,例如: 09 读取为 9 ,因此 charAt(1) 不存在。
我怎样才能解决这个问题? 有没有办法让 0 算作 charAt(0) 以便 charAt(1) 计算 0 之后的数字,或者我需要更改其他内容吗?
谢谢!
正如我在评论中提到的:你有String sa = String.format("%02d", value);
但是你在主代码中使用了str1
。 使用sa
代替,即使数字小于 10,它也会有 2 个字符。
另一种选择是不使用字符串。 这是一个仅限数学的版本。
public static void randomNumber () {
{
System.out.println("Guess a number between 01-99");
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int value = Integer.valueOf(scanner.nextLine());
// TODO: add a check 1 <= value <= 99
int randomNum = random.nextInt(98) + 1;
System.out.printf("You guessed :%02d%n", value);
System.out.printf("The correct number is : %02d%n", randomNum);
// Check for exact match
if (value == randomNum) {
System.out.println("Congratulations, you guessed the right number in the right order and won 1000$!");
return;
}
// Get the digits of the random number (works for single digit numbers, too)
int randOnes = randomNum % 10;
int randTens = randomNum / 10;
// Check for reverse match
if (value == (randOnes * 10 + randTens)) {
System.out.println("Congratulations, you guessed the right number but in the wrong order and won 500$ !");
return;
}
// Get user's digits
int userOnes = value % 10;
int userTens = value / 10;
// Check for single digit match
if (userOnes == randOnes || userOnes == randTens || userTens == randOnes || userTens == randTens) {
System.out.println("Congratulations, you guessed one of the numbers right!");
return;
}
System.out.println("Sorry you guessed wrong, try again");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.