Java 为什么我的代码没有运行?

Java 为什么我的代码没有运行?,java,if-statement,Java,If Statement,我有以下代码: 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);

我有以下代码:

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);    
    }
}
import java.util.Scanner;
public类PositiveNegative{public static void main(字符串[]args){
整数,加=0,减=0;
整数计数=0;
双倍合计=0;
扫描仪扫描=新扫描仪(System.in);
System.out.print(“输入整数(0表示退出):”;
number=scan.nextInt();
while(数字!=0)
{
总数+=个数;
如果(数字>0)
plus++;
如果(数字<0)
负++;
}
System.out.println(“正数为:“+plus”);
System.out.println(“负片数为:“+减”);
System.out.println(“总数为:“+total”);
}
}

问题是我试着运行它并键入数字,但它什么也没做。我想要它,这样当你输入0时,它就不再接受数字并开始处理代码。我该怎么办?

您需要更新
数字
,否则您的循环将永远运行。我建议使用大括号(和
else
)。大概

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();
}
试试这个:

    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);
    }
}
import java.util.Scanner;
公共类积极否定{
公共静态void main(字符串[]args){
整数=0,加=0,减=0;
双倍合计=0;
做{
扫描仪扫描=新扫描仪(System.in);
System.out.print(“输入整数(0表示退出):”;
numbers=Integer.valueOf(scan.nextLine());
总数+=个数;
如果(数字>0)
plus++;
如果(数字<0)
负++;
}
while(数字!=0);
System.out.println(“正数为:“+plus”);
System.out.println(“负片数为:“+减”);
System.out.println(“总数为:“+total”);
}
}

将扫描仪置于while循环中,以便每次循环启动时它都会请求用户输入。

您必须每次修改
数字,以使其在
while
中工作

因此,在现有代码中,只需注释掉
numbers=scan.nextInt()并在下面使用--

这将为您提供所需的输出--


您从不修改
数字
,因此一旦它进入循环,它就永远不会离开。如果您输入的数字不是0,那么您将陷入一个无休止的循环。只需在运行程序时检查CPU使用情况。。。循环中还需要有
numbers=scan.nextInt()
。为什么
numbers
被初始化为-1?只是为了确保while循环第一次运行?他是对的,在这种情况下,您使用do-while循环。
    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);
    }
}
// numbers = scan.nextInt();  //comment out this call

    while ((numbers = scan.nextInt()) != 0) {
    ....
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