Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java while loop赢了';t在停止条件结束后打印文本_Java_Loops_While Loop - Fatal编程技术网

java while loop赢了';t在停止条件结束后打印文本

java while loop赢了';t在停止条件结束后打印文本,java,loops,while-loop,Java,Loops,While Loop,我刚开始学习Java,我需要使用while来根据球员的人数来决定有多少球员可以成为守门员。循环应该在用户输入0并打印出可以成为守门员的球员数量后停止 public class Q3_201303719 { public static void main(String[] args) { Scanner input = new Scanner (System.in); int num; int count=0; System.out.pr

我刚开始学习Java,我需要使用
while
来根据球员的人数来决定有多少球员可以成为守门员。循环应该在用户输入
0
并打印出可以成为守门员的球员数量后停止

public class Q3_201303719 {

    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);
        int num; int count=0;

        System.out.println("Enter the players' numbers");
        num = input.nextInt();

        while ((num != 0) && (num < 31) && (num%2==0) || (num%3==0))    
            count++; 

        System.out.println(count+ " players can be goalkeepers.\n");
        // Above line should be printed once the user enter 0, but in my case it won't
        // print and keeps asking the user to enter a number.
    }
}
公共类Q3\u 201303719{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int num;int count=0;
System.out.println(“输入玩家编号”);
num=input.nextInt();
而((num!=0)&&(num<31)&&(num%2==0)| |(num%3==0))
计数++;
System.out.println(count+“球员可以是守门员。\n”);
//用户输入0后,应立即打印上面的行,但在我的情况下不会打印
//打印并不断要求用户输入数字。
}
}

循环的括号应该可以解决这个问题。

您需要输入多少玩家的号码? 您有一个依赖于num值的while循环,但从未更改num值。那将在无尽的循环中结束

如果有更多玩家,请创建for循环以输入数字,将数字存储在数组或arraylist中。如果使用while循环,请使用它,使其取决于在循环内更改的值。否则它将永远循环

do{
   System.out.println("Enter the players' numbers");
   num = input.nextInt();
   count++;
}while(num!=0);
您的代码应该如下所示:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int num;
    int count = 0;

    do {
        System.out.println("Enter the players' numbers");
        num = input.nextInt();
        count++;
    } while (num != 0);

    System.out.println(count + " players can be goalkeepers.\n");
    // Above line should be printed once the user enter 0, but in my case it
    // won't
    // print and keeps asking the user to enter a number.
}
while( false && true && true || true ) 
while( false || true )
while( true )  

您需要在while循环块周围使用大括号。一般来说,最好是过度使用大括号,而不是使用不足

您的代码应该如下所示:

package q3_201303719;
import java.util.Scanner;

public class Q3_201303719 {

    public static void main(String[] args) {

        Scanner input = new Scanner (System.in);
        int num; int count=0;

        System.out.println("Enter the players' numbers");
        num = input.nextInt();

        while((num != 0) && (num < 31)&& (num%2==0)|| (num%3==0)) {
            count++; 
        }
        // The braces added above are required for the while loop and will keep your program
        // from not outputting your results.

        System.out.println(count+ " players can be goalkeepers.\n");

    }

}
q3号包\u 201303719;
导入java.util.Scanner;
公共类Q3_201303719{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int num;int count=0;
System.out.println(“输入玩家编号”);
num=input.nextInt();
而((num!=0)&&(num<31)&&(num%2==0)| |(num%3==0)){
计数++;
}
//上面添加的大括号是while循环所必需的,将保留您的程序
//从没有输出您的结果。
System.out.println(count+“球员可以是守门员。\n”);
}
}

祝你好运和幸福。:)

您的代码没有意义

  • 第一:While循环没有修改“num”值,导致 无限循环,如果输入
  • 第二:代码应该做什么?从你的表演很难判断
  • 我认为放置while循环可以帮助您,但代码仍然没有多大意义

    public class Q3_201303719 {
    
        public static void main(String[] args) {
    
           int num = 0; int count=0;
    
           while((num != 0) && (num < 31)&& (num%2==0)|| (num%3==0)) {
    
              Scanner input = new Scanner (System.in);
    
              System.out.println("Enter the players' numbers");
              num = input.nextInt();
              count++; 
           }
    
           System.out.println(count+ " players can be goalkeepers.\n");
           // Above line should be printed once the user enter 0, but in my case it won't
           // print and keeps asking the user to enter a number.
        }    
    }
    
    公共类Q3\u 201303719{
    公共静态void main(字符串[]args){
    int num=0;int count=0;
    而((num!=0)&&(num<31)&&(num%2==0)| |(num%3==0)){
    扫描仪输入=新扫描仪(System.in);
    System.out.println(“输入玩家编号”);
    num=input.nextInt();
    计数++;
    }
    System.out.println(count+“球员可以是守门员。\n”);
    //用户输入0后,应立即打印上面的行,但在我的情况下不会打印
    //打印并不断要求用户输入数字。
    }    
    }
    
    编辑: 出现此问题是因为您没有遵循一条非常简单的规则:在编码之前先思考。
    对于本练习,您需要考虑代码应该做什么,然后为其编写代码。编码只是一种语言,如果你知道你想写什么,那就更容易了。在这里,您显然不知道自己想要做什么,而且由于您是新开发人员,很可能会被绊倒。如果您不能按照Ubica的建议使用
    do-while循环,那么您需要解决这个问题:

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num=2;   // you need to initialize num with a value, that allows you to go inside the while loop at least once
        int count = 0;
    
        while((num != 0) && (num < 31) && (num % 2 == 0) || (num % 3 == 0)) {
            System.out.println("Enter the players' numbers");  
            num = input.nextInt();   // user input is here inside the loop
            count++;  // your count will count every valid input + the user input that ends the loop
        }
        count--;  // if your user entered 0 to exit the loop, count would have still incremented, so you need do subtract one again
        System.out.println(count + " players can be goalkeepers.\n");
    }
    
    因此,当用户输入
    0
    时,while条件的计算如下:

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num;
        int count = 0;
    
        do {
            System.out.println("Enter the players' numbers");
            num = input.nextInt();
            count++;
        } while (num != 0);
    
        System.out.println(count + " players can be goalkeepers.\n");
        // Above line should be printed once the user enter 0, but in my case it
        // won't
        // print and keeps asking the user to enter a number.
    }
    
    while( false && true && true || true ) 
    while( false || true )
    while( true )  
    

    从这个问题很难理解你想要达到的目标。但是,我们可以尝试帮助您理解编写的代码

    while循环当前编写为:

    while ((num != 0) && (num < 31) && (num%2==0) || (num%3==0))
    
    由于这些值都计算为true,因此不计算OR部分(
    num%3==0
    ),因为它不是必需的。参见短路评估()

  • 计数
    增加到1

  • 循环第二次再次执行
    num
    仍然是10

    num != 0 is true as 10 != 0.
    num < 31 is true as 10 is less than 31.
    num%2==0 is true as 10 divided by 2 is 5 and leaves a remainder of 0.
    
    因此,现在评估| |部分:

    num%3==0 is true as 9 divided by 3 is 3 and leaves a remainder of 0.
    
    同样,这将导致无限循环

    如果变量
    num
    改为0:

    num != 0 is false
    

    作为第一个
    num=0
    为false,则
    num代码正在执行什么操作?有什么问题?请参阅关于家庭作业的问题。只要不是0,我可以输入任意数量的玩家号码,一旦输入了0,就可以打印多少玩家。另外,我只允许使用while。那么上面的代码应该可以修复您的错误problem@futeenu,这也是一个
    while
    循环,它只允许您在检查条件之前在循环内执行一次代码–如果您想请求用户输入,这是必要的。建议的代码工作正常,并且在用户输入0时完全停止,最终计数为false。例如,如果我输入2,3,5,6,33,0,它应该打印“3名球员可以成为守门员”,但上面的代码是6名球员。@futeenu:如果你将球员算为守门员或不算守门员,你应该将while条件(循环直到用户输入0)与条件分开!e、 g.
    while(num!=0){if(num%3==0){count++;}}
    Hi!谢谢你的回答:)但是当我使用大括号时,它会无限地打印计数。另外,如果我打扰了你,我也很抱歉,但我对这个很陌生,它一直在给我压力,因为最后期限只有几天。嗨,你的代码工作了,并给出了确切的输出条件,但它在第三次输入后立即终止。当我使用for循环时,我也面临同样的问题。e、 g:对于(g=1;g)也许你应该根据用户输入的数字来解释你的逻辑,然后
    num != 0 is true as 9 != 0.
    num < 31 is true as 9 is less than 31.
    num%2==0 is false as 9 divided by 2 is 4 and leaves a remainder of 1.
    
    num%3==0 is true as 9 divided by 3 is 3 and leaves a remainder of 0.
    
    num != 0 is false
    
    num%3==0 is true as 0 divided by 3 leaves a remainder of 0.