繁体   English   中英

循环无法正常工作

[英]A loop doesn't work properly

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 位数字的代码是不可能的)。 有人可以解释为什么吗?

删除你的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);
}

当前代码-

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

自从您输入真正的代码以来,此代码将永远运行,因为您有do-while其条件说!s.equals(num) ,因此您必须首先删除do-while ,这不是必需的,当您预测代码等于num那么变量x必须等于5 ,因此您可以在打印成功后使用return语句终止程序。 注意x的值,每次迭代时它必须等于 0,我的意思是x=0

for(int i=0; i<5; i++){
    s = in.nextLine();
    if(s.length!=5){
       System.out.println("code should be of length 5");
       continue;
    }
    x = 0;
    for(int c=0; c<5; 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
    if(x==5){
       System.out.println("Congrats! You guessed the secret code. ");
       return;
    }
}
System.out.println("Sryy !! :((");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM