2 qq 34018371 qq_34018371 于 2016.02.24 05:31 提问

Java语言程序设计第4章编程练习题1求问
import java.util.Scanner;

public class Practice {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int data = 0;
        int positive = 0;
        int negative = 0;
        int sum = 0;
        int count = 0;
        System.out.print("Enter int value (0 for exit):");

        do {
            data = input.nextInt();
            if (data > 0)
                positive += 1;
            else 
                negative += 1;
            sum += data;
            count += 1;
        }while (input.hasNextInt() && data != 0);

        double average = sum / count;
        System.out.println("The totla is " + sum);
        System.out.println("The average is " + average);
    }
}

执行的时候不显示结果,0退出不了。怎样能够达到输出这样的结果啊?

  • Enter int value(0 for exit):
  • 1 2 -1 3 0 (enter)
  • The number of positives is 3
  • The number of positives is 1
  • The number of positives is 1The total is 5
  • The average is 1.25

5个回答

xiongzirui
xiongzirui   2016.02.24 09:32
已采纳

将while循环里面的条件交换一下位置就行了
while(data!=0 && input.hasNextInt())

xiongzirui
xiongzirui   2016.02.24 09:47

原因是input.hasNextInt()放在前面的话,当最后一个int值处理之后会等待输入,所以不会执行后面的输出部分的代码
另外最后记得加上input.close();养成好的习惯

qq_34018371
qq_34018371 回复xiongzirui: 看的书学的,压根不晓得有input.close();
接近 2 年之前 回复
caozhy
caozhy   Ds   Rxr 2016.02.24 06:57

把Scanner input = new Scanner(System.in);放在do循环里

a1055186977
a1055186977   2016.02.24 08:58
     Scanner input = new Scanner(System.in);
        int data = 0;
        int positive = 0;
        int negative = 0;
        int sum = 0;
        int count = 0;
        System.out.print("Enter int value (0 for exit):");

        do {
            data = input.nextInt();
            if (data > 0)
                positive += 1;
            else 
                negative += 1;
            sum += data;
            count += 1;
        }while (data != 0);
        double average = sum / count;
        System.out.println("The positive is " + positive);
        System.out.println("The negative is " + negative);
        System.out.println("The totla is " + sum);
        System.out.println("The average is " + average);
    }
qq_34018371
qq_34018371 回复a1055186977: 你这改法最简单哈。我把问题想复杂了![图片说明](http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/001/face/28.gif)
接近 2 年之前 回复
xionglangs
xionglangs   Rxr 2016.02.24 11:13

加个标记public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int data = 0;
int positive = 0;
int negative = 0;
int sum = 0;
int count = 0;
boolean flag = true;
do {
System.out.println("Enter int value (0 for exit):");
data = sc.nextInt();
if (data > 0)
positive += 1;
else
negative += 1;
sum += data;
count += 1;
System.out.println("是否需要再次输入 Y/N");
if ("N".equals(sc.next())) {
flag = false;
}
} while (flag && data != 0);

    double average = sum / count;
    System.out.println("The totla is " + sum);
    System.out.println("The average is " + average);
}你的hasNextInt方法只是加一个扫描器,不太好判断,还是直接加个标记好一些。
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!