简体   繁体   English

为什么我的代码无法运行?

[英]Why does my code not run?

I have this code: 我有以下代码:

import java.util.Scanner;  

public class PositiveNegative   {    public static void main(String[] args)    {
      int numbers, plus = 0, minus = 0;
      int count = 0;
      double total = 0;

      Scanner scan = new Scanner(System.in);
      System.out.print("Enter an integer (0 to quit): ");
      numbers = scan.nextInt();

      while(numbers != 0)
      {
         total += numbers;
         if(numbers > 0)
            plus++;
         if(numbers < 0)
            minus++;   
      }
      System.out.println("The number of positives is: " +plus);
      System.out.println("The number of negatives is: " +minus);
      System.out.println("The number of total is: " +total);    
    }
}

The problem with is that I try to run it and type the numbers but it does nothing. 问题是我尝试运行它并键入数字,但是它什么也没做。 I want it so that when you type 0 it stops taking numbers and starts processing the code. 我想要它,以便当您键入0时,它停止输入数字并开始处理代码。 What should I do? 我该怎么办?

You need to update numbers or your loop will run for ever. 您需要更新numbers否则循环将永远运行。 And I recommend using braces (and an else ). 我建议使用括号(和else )。 Something like, 就像是,

System.out.print("Enter an integer (0 to quit): ");
numbers = scan.nextInt();
while (numbers != 0) {
    total += numbers;
    if (numbers > 0) {
        plus++;
    } else if (numbers < 0) {
        minus++;   
    }
    System.out.print("Enter an integer (0 to quit): ");
    numbers = scan.nextInt();
}

Alternatively, you could use a do-while loop. 另外,您可以使用do-while循环。 Then you only need one copy of the prompt. 然后,您只需要一份提示。 Like, 喜欢,

do {
    System.out.print("Enter an integer (0 to quit): ");
    numbers = scan.nextInt();
    total += numbers;
    if (numbers > 0) {
        plus++;
    } else if (numbers < 0) {
        minus++;   
    }
} while (numbers != 0);

Try this: 尝试这个:

    import java.util.Scanner;

public class PositiveNegative {
    public static void main(String[] args) {
        int numbers = 0, plus = 0, minus = 0;
        double total = 0;
        do{
            Scanner scan = new Scanner(System.in);
            System.out.print("Enter an integer (0 to quit): ");
            numbers = Integer.valueOf(scan.nextLine());
            total += numbers;
            if (numbers > 0)
                plus++;
            if (numbers < 0)
                minus++;
        }
        while (numbers != 0);
        System.out.println("The number of positives is: " + plus);
        System.out.println("The number of negatives is: " + minus);
        System.out.println("The number of total is: " + total);
    }
}

Put your Scanner in the while loop so that everytime loop start it will ask for User input. 将Scanner放入while循环中,以便每次循环启动时都将要求用户输入。

You have to modify numbers each time to make it work in your while . 你要修改numbers每次都让它在你的工作while

So, in your existing code, just comment out numbers = scan.nextInt(); 因此,在您现有的代码中,只需注释掉numbers = scan.nextInt(); and use below-- 并在下面使用-

// numbers = scan.nextInt();  //comment out this call

    while ((numbers = scan.nextInt()) != 0) {
    ....

this will give you desired output-- 这将为您提供所需的输出-

Enter an integer (0 to quit): 9
4
-9
1
0
The number of positives is: 3
The number of negatives is: 1
The number of total is: 5.0

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

相关问题 为什么这段代码不运行? - Why does not this code run? 为什么我的while循环需要运行这行代码? - Why does my while loop needs this line of code to run? 为什么抛出异常时我的方面代码无法运行? - why does my aspect code not run when exception is thrown? 为什么我运行代码时会安装某些东西? - Why does something get installed when I run my code? 为什么我的 CompletableFuture 代码在 Java 8 中运行,但在 Java 11 中没有运行? - Why does my CompletableFuture code run in Java 8 but not in Java 11? 为什么我的“ if(p.exitValue()!= 0)”代码运行两次? - Why does my “if (p.exitValue() != 0)” code run twice? 为什么我的代码永远运行? 它只是一堆应该递减的整数的for循环? - Why does my code run forever? its just a bunch of for loops with ints that are supposed to decrement? 为什么我的Java代码在eclipse中运行,但不能作为.exe运行。 或.class文件 - why does my java code run in eclipse but not as an .exe. or .class file 我的java代码有一个明显的错误。为什么要编译并运行? - My java code has an obvious error. Why does it compile and run? 无论我将组件存储在何处,为什么我的Java代码都不能在Eclipse中运行? - Why does my Java code not run in Eclipse regardless of where I store the components?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM