qq_34018371
qazqzad
2016-02-23 21:31
采纳率: 100%
浏览 1.3k
已采纳

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
    夜空中的程序猿 2016-02-24 01:32
    已采纳

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

    点赞 评论
  • caozhy

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

    点赞 评论
  • a1055186977
    a1055186977 2016-02-24 00: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);
        }
    
    点赞 评论
  • xiongzirui
    夜空中的程序猿 2016-02-24 01:47

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

    点赞 评论
  • xionglangs
    编程爱好者熊浪 2016-02-24 03: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方法只是加一个扫描器,不太好判断,还是直接加个标记好一些。
    
    点赞 评论

相关推荐