0

我正在编写一个程序,您将在其中使用扫描仪输入温度。您可以输入任意数量的温度,当您输入 -100 时,程序将停止,然后为您提供最低、最高、平均和总注册温度。我已经取得了一些成功,但无法弄清楚如何获得最小值、总数和平均工作量。现在被困在这里一段时间了。

import java.util.Scanner;

public class Tempratureprograme {
public static void main(String[] args) {

    temperatures();

}
public static void temperatures(){
    double temperature; 
    double biggest = 0;
    double smallest = 0;
    double totalNum = 0; 
    double counter = 1;
    int numberAmount = 0;
    double average = totalNum/numberAmount;

    Scanner input = new Scanner(System.in);

    System.out.println("Please enter the temperature(s) - end the program with -100");
    System.out.print("-> ");


    do {
        temperature = input.nextDouble();
        numberAmount++;
        totalNum = totalNum + temperatur;
        System.out.print("-> ");

        if(temperature > max)
        {
            max=temperature;
            counter= 0;
        }

        if(temperature < min)
        {
            min = temperature;
        }

    }

    while (temperature != -100);
    System.out.println();
    System.out.println("Highest temperature: " + max);
    System.out.println("Lowest temperature: " + min);
    System.out.println("Average temprature: " + average); 
    System.out.println("Total registered tempratures: " + totalNum);



}
}
4

4 回答 4

1

试试这个,你不能用 0 初始化 min,因为每当你尝试用它匹配一个正温度时,if 总是会失败。以下代码应该可以工作

 public static void main(String[] args) {

    temperatures();

}
public static void temperatures(){
    int counter = 0;
    double temperature; 
    double totalNum = 0; 
    int numberAmount = 0;
    double max = - Double.MAX_VALUE;
    double min = Double.MAX_VALUE;
    double average = 0;

    Scanner input = new Scanner(System.in);

    System.out.println("Please enter the temperature(s) - end the program with -100");
    System.out.print("-> ");


    while ((temperature = input.nextDouble()) != -100) {
      //  temperature = input.nextDouble();
        counter++;
        numberAmount++;
        totalNum = totalNum + temperature;
        System.out.print("-> ");

        if(temperature > max)
        {
            max=temperature;

        }

        if(temperature < min)
        {
            min = temperature;
        }

    }

    average = totalNum/numberAmount;
    System.out.println();
    System.out.println("Highest temperature: " + max);
    System.out.println("Lowest temperature: " + min);
    System.out.println("Average temprature: " + average); 
    System.out.println("Total registered tempratures: " + counter);



}
于 2013-10-21T11:38:45.873 回答
0

您应该将此代码放在for循环之后以计算平均值,而不是之前:

double average = totalNum/numberAmount;
于 2013-10-21T11:13:24.820 回答
0

我想这应该可以解决问题

public static void temperatures() {
    double temperature;
    double biggest = 0;
    double smallest = 0;
    double totalNum = 0;
    double counter = 1;
    int numberAmount = 0;
    double average = totalNum / numberAmount;

    Scanner input = new Scanner(System.in);

    System.out.println("Please enter the temperature(s) - end the program with -100");
    System.out.print("-> ");

    while ((temperature = input.nextDouble()) != -100) {
        numberAmount++;
        totalNum = totalNum + temperature;
        average=totalNum/numberAmount;

        if (temperature > biggest) {
            biggest = temperature;
        }
        if (temperature < smallest) {
            smallest = temperature;
        }
        System.out.print("-> ");
    }

    System.out.println();
    System.out.println("Highest temperature: " + biggest);
    System.out.println("Lowest temperature: " + smallest);
    System.out.println("Average temprature: " + average);
    System.out.println("Total registered tempratures: " + totalNum);

}
于 2013-10-21T11:15:54.150 回答
0

这是我使用集合的解决方案

 public static void temperatures(){
double temperature = 0; 
 double totalNum = 0; 
 double numberAmount = 0;
 List<Double> d= new ArrayList<Double>();
 Scanner input = new Scanner(System.in);

 System.out.println("Please enter the temperature(s) - end the program with -100");
 System.out.print("-> ");

  while (true){
   temperature = input.nextDouble();
     if (-100 == temperature) {
     break;
     }
     d.add(temperature);
     totalNum = totalNum+temperature;
     System.out.print("-> ");

 }
  numberAmount = d.size();

  System.out.println();
  System.out.println("Highest temperature: " + Collections.max(d));
  System.out.println("Lowest temperature: " +  Collections.min(d));
  System.out.println("Average temprature: " + totalNum/numberAmount); 
  System.out.println("Total registered tempratures: " + numberAmount);
  System.out.println("Sum of registered tempratures: " + totalNum);
}
于 2013-10-21T11:49:52.457 回答