-6

我已经得到了平均值,并且我的程序已经从用户那里读取了三个整数,我的问题是我不知道如何获得程序的最大和最小数字:(

-我是编程新手 :(

这是我的程序:

主类:

import java.util.Scanner;
public class TestNumbers {

    public static void main (String[]args){    

    int n1, n2, n3;
    System.out.println("Enter three integer numbers ");

          Scanner in = new Scanner(System.in);

          n1 = in.nextInt();
          n2 = in.nextInt();
          n3 = in.nextInt();      

    Numbers num=new Numbers();
    num.setNum(n1, n2, n3);

    System.out.println("The Maximum of : "+n1+ " , " +n2+ " , " +n3+ " is ");
    System.out.println("The Minimum of : "+n1+ " , " +n2+ " , " +n3+ " is ");
    System.out.println("The Average of : "+n1+ " , " +n2+ " , " +n3+ " is "+num.getAve());
    System.out.println("Press any key to continue...");      
    }
}

基类:

public class Numbers {
    private int n1;
    private int n2;
    private int n3;
    private int ave;


    public void setNum(int n1, int n2, int n3){
        this.n1=n1;
        this.n2=n2;
        this.n3=n3;
    }

    public double getAve(){
        ave=(n1+n2+n3)/3;
        return ave;
    }

}
4

2 回答 2

8

我会给你一个简单的算法。比较您的第一个数字和第二个数字。找到最多两个并将其与第三个数字进行比较。现在你可以做编码了吗?

于 2013-01-23T13:49:42.060 回答
1

(n1+n2+n3)/3.0

public double getAve(){
   ave=(n1+n2+n3)/3;
   return ave;
}

这个函数让我担心,因为 ave 是一个整数并且该函数应该返回一个双精度数。您将失去用于计算的整数的精度。考虑铸造。

public double getAve(){
   double result = (double)(n1+n2+n3)/3.0
   return result;
}
于 2013-01-23T13:56:23.660 回答