查找最小值,最大值,Evens,赔率和平均值。 Java的

时间:2013-03-22 06:27:59

标签: java average

正如标题所述,我试图找到最大值和最小值,平均数和赔率数,以及所有输入数字的平均值。

问题所在。当我运行我的代码时,我的赔率和平均值计数器似乎正在读取它们的相反,奇数会读取偶数输入甚至会读取奇数输入。至于我的平均水平,我不知道它有什么问题,我所知道的是它只能找到适当分数的平均值。

我的输出示例将在最后粘贴。

import java.util.Scanner;

public class Homework7APrinter
{
    public static void main (String[] args)
    { 
        System.out.println("Enter a sequence of integers. Any non-integer to quit");
        Scanner scan = new Scanner (System.in);
        int min = Integer.MAX_VALUE;
        int max = 0;
        int count = 0;
        int sum = 0;
        int oddsCounter = 0;
        int evensCounter = 0;
        int getInt = Integer.MIN_VALUE;
        double average = 0;
        boolean notnull = true;

        while(scan.hasNextInt())
        {
            if(true)
            {
                if (getInt%2==1)
                {
                    evensCounter++;
                    System.out.println("even: " + evensCounter);
                }
                else
                {
                    oddsCounter++;
                    System.out.println("odd: " + oddsCounter);
                }

                getInt = scan.nextInt();

                if(getInt < min)
                {
                    min = getInt;
                }
                else if(getInt > max)
                {
                    max = getInt;
                }
            }
            else
            {
                notnull = false;
            }

            sum += getInt;
            System.out.println("sum " +sum);
            count++;
            System.out.println("count " +count);
            average = sum/(count);
            System.out.println("average " +average);
        }

        System.out.println("smallest: " + min);
        System.out.println("largest: " + max);
        System.out.println("even: " + oddsCounter);
        System.out.println("odd: " + evensCounter);
        System.out.println("average: " + average);
    }
}

结果:

Enter a sequence of integers. Any non-integer to quit
1 //first input
odd: 1
sum 1
count 1
average 1.0
2 //second input
even: 1
sum 3
count 2
average 1.0
3 //third input
odd: 2
sum 6
count 3
average 2.0
4 //fourth input
even: 2
sum 10
count 4
average 2.0
q //quit the scan
smallest: 1
largest: 4
even: 2
odd: 2
average: 2.0 //average of 1, 2, 3, 4 = sum/n = 10/4 = 2.5 not 2.0

任何帮助将不胜感激。谢谢!

4 个答案:

答案 0 :(得分:0)

(getInt%2==1)实际上是对奇数的检查,偶数检查应为getInt%2==0

答案 1 :(得分:0)

很少 -

  1. 移动getInt = scan.nextInt();如果(真实)条件
  2. 之后立即
  3. 平均值为 - 平均=(双)和/(计数);
  4. 对于偶数/奇数问题进行此更改 - if(getInt%2 == 0)
  5. 所以这是你的代码,有一些清理 -

        System.out.println("Enter a sequence of integers. Any non-integer to quit");
        Scanner scan = new Scanner(System.in);
        int min = Integer.MAX_VALUE;
        int max = 0;
        int count = 0;
        int sum = 0;
        int oddsCounter = 0;
        int evensCounter = 0;
        int getInt;
        double average = 0;
    
        while (scan.hasNextInt()) {
    
            getInt = scan.nextInt();
            if (getInt % 2 == 0) {
                evensCounter++;
                System.out.println("even: " + evensCounter);
            } else {
                oddsCounter++;
                System.out.println("odd: " + oddsCounter);
            }
    
            if (getInt < min) {
                min = getInt;
            } else if (getInt > max) {
                max = getInt;
            }
    
            sum += getInt;
            System.out.println("sum " + sum);
            count++;
            System.out.println("count " + count);
            average = (double) sum / (count);
            System.out.println("average " + average);
        }
    
        System.out.println("smallest: " + min);
        System.out.println("largest: " + max);
        System.out.println("even: " + oddsCounter);
        System.out.println("odd: " + evensCounter);
        System.out.println("average: " + average);
    

答案 2 :(得分:0)

这就是你要找的东西

 while(scan.hasNextInt())
        {

            getInt = scan.nextInt();
//            if(true)
//            {
                if (getInt%2==0)
                {
                    evensCounter++;
                    System.out.println("even: " + evensCounter);
                }
                else
                {
                    oddsCounter++;
                    System.out.println("odd: " + oddsCounter);
                }

                //getInt = scan.nextInt();

                if(getInt < min)
                {
                    min = getInt;
                }
                else if(getInt > max)
                {
                    max = getInt;
                }
//            }
//            else
//            {
//                notnull = false;
//            }

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

答案 3 :(得分:0)

你的程序中有五个错误

  1. 您打印不正确(System.out.println(“even:”+ oddsCounter))。甚至应该作为偶数打印

  2. 您必须将总和也设为double或float,以获得十进制格式的平均值。

  3. 在进行奇数偶数测试之前,您必须阅读getINT。

  4. 偶数/奇数计算错误。它应该是getInt%2 == 0

  5. 在计算min和max时,你不应该有if / else。它应该是两个单独的if

  6. 以下完整的工作计划。

    package com.stackoverflow.test;
    

    import java.util.Scanner;

    public class Homework7APrinter {     public static void main(String [] args){         System.out的                 .println(“输入一个整数序列。任何要退出的非整数”);         扫描仪扫描=新扫描仪(System.in);         int min = Integer.MAX_VALUE;         int max = Integer.MIN_VALUE;         int count = 0;         double sum = 0;         int oddsCounter = 0;         int evensCounter = 0;         int getInt = 0;         double average = 0;

        while (scan.hasNextInt()) {
    
            getInt = scan.nextInt();
            if (getInt % 2 == 0) {
                evensCounter++;
                // System.out.println("even: " + evensCounter);
            } else {
                oddsCounter++;
                // System.out.println("odd: " + oddsCounter);
            }
    
            if (getInt < min) 
            {
                min = getInt;
            }
    
            if (getInt > max)
            {
                max = getInt;
            }
    
            sum += getInt;
            // System.out.println("sum " +sum);
            count++;
            // System.out.println("count " +count);
            average = sum / (count);
            // System.out.println("average " +average);
        }
        System.out.println("*****************");
        System.out.println("smallest: " + min);
        System.out.println("largest: " + max);
        System.out.println("even: " + evensCounter);
        System.out.println("odd: " + oddsCounter);
        System.out.println("average: " + average);
        System.out.println("sum: " + sum);
        System.out.println("count: " + count);
    }
    

    }