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

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条回答 默认 最新

  • 夜空中的程序猿 2016-02-24 01:32
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名