循环不';我不能正常工作 import java.util.*; 公共类猜测数{ 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(系统输入); int x=0; String num=“65854”;//这是密码 字符串s; System.out.println(“此程序要求您猜测5位数字的代码。”); System.out.println(“您有5次尝试”); 对于(int i=0;i

循环不';我不能正常工作 import java.util.*; 公共类猜测数{ 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(系统输入); int x=0; String num=“65854”;//这是密码 字符串s; System.out.println(“此程序要求您猜测5位数字的代码。”); System.out.println(“您有5次尝试”); 对于(int i=0;i,java,Java,删除do while循环。它将一直运行,直到用户猜到正确的代码。 插入以下代码以检查字符串的长度 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 se

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

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);

        }
    }
}
您可以为输入字符串中没有字符添加更多限制。 每次在外循环开始时也重置
x

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

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

if(s.equals(num)){
    System.out.println("Congrats! You guessed the secret code. ");
    System.exit(0);
}
for(int i=0;i<5;i++){
做{
s=in.nextLine();
对于(int c=0;c
建议代码-

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
}
字符串s;
整数计数=0;
int x;
做{
x=0;
如果(计数>=5){break;}//5次尝试
s=in.nextLine();
String formattedNumber=String.format(“%05d”,
Integer.parseInt(s));//确保输入为5位整数(此处用0填充)
对于(int c=0;c
此代码将在您输入真实代码后运行,因为您在执行
时,其条件为
!s.equals(num)
,因此您必须先删除
do,而
首先,这不是必需的,当您的预测代码等于
num
时,那么变量
x
必须等于
5
,因此您在打印成功后使用
return
语句终止程序。请注意
x
的值,每次迭代时它必须等于零,我的意思是
x=0

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

for(int i=0;i)到底什么超出了界限?错误到底在哪里?为什么会有
do/while
循环,它基本上会给出无限多的猜测?你所说的“值>5”是什么意思?没有“值”变量。(提示:可能唯一的问题是,当您读取下一行用户输入时,
x
没有重置为0…
if(s.charAt(c)=num.charAt(c))
-
num
的前缀长度为
5
,而
s
来自用户输入。如果
s
num
长,会发生什么情况?你的意思是我应该为“s”定义限制?@catzbro1是的。
for
循环使用
s.length()
作为上限,您使用相同的索引访问
num
字符。这意味着如果
s.length()
10
c
可以从
0
9
s.charAt(9)
将按预期工作,但
num.charAt(9)
将超出范围,因为
num
5个
字符。如果您要求用户“猜测五位数的前缀代码”,则将
s
的最大长度限制为
num.length()
这不是真的!你必须有5次机会!你忽略了chance@Mohsen_Fatemi如果您没有给出正确的字符数,我猜用户仍然在使用attemptPro提示:检查
count>=5
@Fildor-是否会有
的更改?在那里,完成了。一个同事更改了代码,所以count将跳过“5”。然后您将再次永远运行…在本例中,这可能不会发生,但当您在生产代码中有类似的内容时,您将花费数小时逐行检查代码,只看到需求“最多5个”,而您编码的“不完全是5个”。。。
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 !! :((");