我正在编写一个程序,您可以使用扫描仪来调节温度。您可以输入任意数量的温度,当您输入-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 :(得分: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);
}
答案 1 :(得分:0)
您应该将此代码放在for
循环后计算平均值而不是之前:
double average = totalNum/numberAmount;
答案 2 :(得分: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);
}
答案 3 :(得分:0)
以下是我使用Collections的解决方案
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);
}