在Dr. Java中创建温度应用程序

时间:2016-01-12 03:53:51

标签: java

创建一个温度应用程序,提示用户每天的温度 过去5天,然后显示最高温度的五天,最高温度 五天和平均温度。应用程序输出应该类似于 以下: 在第1天:89输入高温 在第2天:65输入高温 在第3天22:22输入高温 在第4天:78输入高温 在第5天:63输入高温 平均气温为63.4 最低气温是22.0 最高温度为89.0

这基本上就是我所拥有的:

             System.out.print ("Enter the temperature on day 1: ");
             double day1 = kbReader.nextDouble();
             System.out.print ("Enter the temperatre on day 2: ");
             double day2 = kbReader.nextDouble();
             System.out.print ("Enter the temperature on day 3: ");
             double day3 = kbReader.nextDouble();
             System.out.print ("Enter the temperature on day 4: ");
             double day4 = kbReader.nextDouble();
             System.out.print ("Enter the temperature on day 5: ");
             double day5 = kbReader.nextDouble();

             double average = day1 + day2 + day3 + day4 + day5;
             double finalaverage = average/5;
             System.out.println ("The average high temperature is " +  finalaverage);    

我想知道如何使用循环找到最高和最低温度

1 个答案:

答案 0 :(得分:0)

请参考此代码在java中正常工作

            double temp[]=new double[5];
            double avgTemp=0;
            double highTemp=0;
            double lowTemp=0;

            Scanner sc=new Scanner(System.in);
            for(int i=0;i<5;i++){
                System.out.print ("Enter the temperature on day "+(i+1)+" :");
                temp[i]=sc.nextDouble();
            }

            //for calculation average temp
            double sum=0;
            for(int i=0;i<5;i++){
                sum+=temp[i];
            }

            System.out.println("Average temp is "+(sum/5));

       //for calculation high temp
    for(int i=0;i<5;i++){
        if(temp[i]>highTemp){
            highTemp=temp[i];
        }
    }
    System.out.println("hightemp is "+highTemp);
    lowTemp=temp[0];
    for(int i=1;i<5;i++){
        if(temp[i]<lowTemp){
            lowTemp=temp[i];
        }
    }
    System.out.println("hightemp is "+lowTemp);