好吧,所以我的猜測遊戲幾乎完成我只是不知道爲什麼中間部分不工作。基本上,用戶輸入一個五位數的數字,它應該在他們的猜測中加入數字,然後增加他們的總數,並告訴他們有多少數字他們得到正確,所以他們可以再次猜測,但它甚至沒有通過我的循環任何幫助,將不勝感激。Java猜測遊戲調試問題我無法解決
//Jonathan
import javax.swing.JOptionPane;
import java.util.Scanner;
public class Guessing
{
public static void main(String[] args)
{
//JOptionPane.showMessageDialog(null, "Welcome to Jonathan's number guessing game.");
int correctPosition = 0, sum = 0, numOfGuesses = 5, i;
String usersGuess, secretCode;
double number = Math.random() * 100000;
number = Math.round(number);
secretCode = Double.toString(number);
do
{
numOfGuesses--;
System.out.println(number);
Scanner keyboard = new Scanner(System.in);
usersGuess = keyboard.nextLine();
if (usersGuess.equalsIgnoreCase(secretCode))
{
JOptionPane.showMessageDialog(null, "You got the number correct!");
System.exit(0);
}
else if (!(usersGuess.equalsIgnoreCase(secretCode)))
{
for (i=0; i > 5; i++)
{
int digit = Character.getNumericValue(usersGuess.charAt(i));
if (digit == secretCode.charAt(i))
System.out.println(digit);
{
correctPosition++;
}
}
JOptionPane.showMessageDialog(null, "You have " + correctPosition + " numbers in the correct position." + " The sum of which are\n" + sum + " keep guessing!");
}
} while (numOfGuesses > 0);
}
}
您的循環條件是不正確,應該是I'5.您應該通過增加一個調試器,或使用打印語句調試這些類型的問題你自己在周圍你感覺代碼沒有被執行。 – tinker 2015-03-19 00:05:30
'if(digits == secretCode.charAt(i))'後面的行看起來不太正確 – MadProgrammer 2015-03-19 00:07:20