0

我无法让这个程序正确运行。我所有的答案,最小值,最大值,平均值都是零。我知道我一开始就将它们初始化为零,但是它们应该通过代码进行更改吗?

public static void main(String[] args) {
    // TODO Auto-generated method stub
int a,b;
int max;
double average;
int min;
int total;
max=0;
min=0;
total=0;
Scanner keyboardInput = new Scanner(System.in);
System.out.println(" Enter a number :");
a = keyboardInput.nextInt();
System.out.println(" You entered :"+ a);
if (a>=0)
{a=max;
max=min;}
total=a;
System.out.println(" Max and min are "+ max  +" & " + min + " The total and average is    :" +total + " & "+a);             
System.out.println(" Enter a second number");

b = keyboardInput.nextInt();

System.out.println(" You entered :"+ b);
if (b>=a)
{b=max;}
if  (b<=a)
{b=min;}

average= (a+b)/2;
total= a+b;
System.out.println(" Max and min are "+ max + " & " + min + " and the total & average     are :" +total + " & " +average +" respectively.");

//c = keyboardInput.nextDouble();


}

}

输出:输入一个数字:

2

您输入:2

最大值和最小值为 0 & 0 总和平均值为 :0 & 0

输入第二个数字

3

您输入:3

最大值和最小值分别为 0 和 0,总和平均值分别为:0 和 0.0。

4

3 回答 3

1

因为您的作业颠倒了:

这一行:

a=max;

应该:

max=a;

max是零,您正在用 覆盖,a也为零。maxa

你也犯了同样的错误b

于 2013-09-25T23:43:46.623 回答
0

代替

 if (a>=0)
 {  max=a;
    max=min;  }

最大值应分配给 a 和 min 应分配给 maxmax=amin=max。否则,它将设置amaxwhich is0然后再次设置max为which is 。min0

if (a>=0)
 {  max=a;
    min=max;  }

到处更新分配问题。记住左变量被赋值为右变量的值

于 2013-09-25T23:43:27.010 回答
0
public static void main(String[] args) {
    int a, b;
    int max;
    double average;
    int min;
    int total;
    max = 0;
    min = 0;
    total = 0;
    Scanner keyboardInput = new Scanner(System.in);
    System.out.println(" Enter a number :");
    a = keyboardInput.nextInt();
    System.out.println(" You entered :" + a);
    if (a >= 0) {
        //modify a=max;
        max = a;
        //modify max=min;
        min = max;
    }
    total = a;
    System.out.println(" Max and min are " + max + " & " + min
            + " The total and average is    :" + total + " & " + a);
    System.out.println(" Enter a second number");

    b = keyboardInput.nextInt();

    System.out.println(" You entered :" + b);
    if (b >= a) {
        //modify b=max;
        max = b ;
    }
    if (b <= a) {
        b = min;
    }
    // modify average= (a+b)/2;
    average = (double)(a + b) / 2;
    total = a + b;
    System.out.println(" Max and min are " + max + " & " + min
            + " and the total & average     are :" + total + " & "
            + average + " respectively.");

    // c = keyboardInput.nextDouble();

}
于 2013-09-25T23:49:12.663 回答