java - java最大和最小输入的建议

标签 java for-loop while-loop

我正在编写一个程序,您将使用扫描仪输入温度。您可以输入任意数量的温度,当您输入 -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);



}
}

最佳答案

试试这个,你不能用 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);



}

关于java - java最大和最小输入的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19492910/

相关文章:

java - 如何在 Apache Camel 中结合 Redelivery 策略和 Hystrix 断路器?

c# - 如何使用httpClient在没有太多请求的情况下循环通过rest api

python - 在 python 中计算余弦而不导入数学

java - 如何在 Java8 流/过滤器中使用非最终变量?

java - 是否可以从父类型调用子静态方法?

java - Spark : Serialization not working with Aggregate

java - 一个 while 循环,其中包含一个 if 语句和一个 for 循环

php - 在 PHP 中将一个键分离两个数组值并插入到数据库

Python 3.3 序列 key 生成器列表问题

c - 使用 while、pi 近似在 C 上进行无限循环