[英]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.