循环无法正常工作

时间:2017-01-27 11:24:10

标签: java

import java.util.*;

public class GuessNumber{
    public static void main(String[]args){
        Scanner in = new Scanner(System.in);
        int x = 0;
        String num = "65854"; // This is the secret code
        String s;
        System.out.println("This program asks you to guess the code of 5 digits. ");
        System.out.println("You have 5 attempts. ");

        for(int i=0; i<5; i++){
            do{
                s = in.nextLine(); 


                for(int c=0; c<s.length(); c++){
                    if(s.charAt(c)==num.charAt(c)) //if digit in 's' equals the digit in the same position in 'num', increment variable x
                        x++;
                }

                System.out.println("Number of correct digits in right position: " + x);  // here the execution goes out of bounds
            }
            while(!s.equals(num));
            System.out.println("Congrats! You guessed the secret code. ");
            System.exit(0);

        }
    }
}

我试图创建一个简单的java程序,它应该允许用户猜测一个五位数的前缀代码(只有五次尝试)。 do-while循环仅显示前两次尝试的正确值,然后超出界限(显示值> 5,对于仅5位数的代码是不可能的)。有人能解释一下原因吗?

3 个答案:

答案 0 :(得分:0)

删除do-while循环。它将运行直到用户猜出正确的代码 插入以下代码以检查字符串的长度

if(s.length()!=5){
    System.out.println("code should be of length 5");
    continue;
}

您可以为输入字符串中的任何字符添加更多限制。 还要在每次外循环开始时重置x

同时检查每个外部循环中的输入字符串是否正确

if(s.equals(num)){
    System.out.println("Congrats! You guessed the secret code. ");
    System.exit(0);
}

答案 1 :(得分:0)

当前代码 -

for (int i = 0; i < 5; i++) {
    do {
         s = in.nextLine();
         for (int c = 0; c < s.length(); c++) { // the length of s can exceed 5 as of **num = "65854"**
             if (s.charAt(c) == num.charAt(c)) //if digit in 's' equals the digit in the same position in 'num', increment variable x
             x++;
             System.out.println("Number of correct digits in right position: " + x);
         }
      }
      while (!s.equals(num)); // this ensures currently N number of attempts NOT JUST 5 as stated otherwise.
      System.out.println("Congrats! You guessed the secret code. ");
      System.exit(0); // with this line the for loop would execute just once
}

建议代码 -

String s;
int count = 0;
int x;
do {
    x = 0;
    if (count >= 5) { break; } // 5 attempts
    s = in.nextLine();
    String formattedNumber = String.format("%05d",
           Integer.parseInt(s)); // make sure the input is of 5 digit integer (padding here with 0's)
    for (int c = 0; c < formattedNumber.length(); c++) {
        if (formattedNumber.charAt(c) == num.charAt(c)) {
            x++;
        }
        if (x == 5) { // correct guess if all chars are equal
            System.out.println("Congrats! You guessed the secret code.");
            break; // break if guessed correct
        } else {
            System.out.println("Number of correct digits in right position: " + x);
        }
    }
    count++; //next attempt
} while (!s.equals(num)); //input not equals the desired, next attempt

答案 2 :(得分:0)

此代码将运行 EVER ,因为您输入了真实代码,因为您的!s.equals(num)条件为do-while,因此您必须删除{{1首先,这不是必要的,当您的预测代码等于num时,变量x必须等于5,因此您使用{{1}终止程序打印成功后的语句。要注意return的值,它在每次迭代时必须等于零,我的意思是x

x=0