我正在编写一个程序,其主题早先关闭,我遇到了麻烦。我写了代码。我也想使用随机代码,如果我选择了一个季节,它必须随机选择20到40之间的数字。输出不会显示生成的第一个,最后一个,最高和最低温度既不是总和也不是平均值。
import java.util.Scanner;
公共课TempSim {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int simulations = 0, InputTemp = 0, choice = 0;
double firstTemp = 0, lastTemp = 0, lowestTemp= 0, highestTemp = 0, sum = 0 ;
System.out.println("Select Season");
System.out.println("Press 1 for winter");
System.out.println("Press 2 for spring");
System.out.println("Press 3 for summer");
System.out.println("Press 4 for fall");
System.out.println("Press 5 to exit");
choice = input.nextInt();
System.out.print("Enter number of simulations:");
simulations = input.nextInt();
for(int i = 0; i < simulations; i++)
if (choice == 1){
while( InputTemp > 20 && InputTemp < 40){
System.out.print("Enter Temperature: ");
InputTemp = input.nextInt();
sum += InputTemp;
firstTemp = InputTemp;
lastTemp = InputTemp;
if (InputTemp > highestTemp){
highestTemp = InputTemp;
}
if (InputTemp < lowestTemp){
lowestTemp = InputTemp;
}
}
if (choice == 2){
while( InputTemp > 40 && InputTemp < 70){
System.out.print("Enter Temperature: ");
InputTemp = input.nextInt();
sum += InputTemp;
firstTemp = InputTemp;
lastTemp = InputTemp;
if (InputTemp > highestTemp){
highestTemp = InputTemp;
}
if (InputTemp < lowestTemp){
lowestTemp = InputTemp;
}
}
if (choice == 3){
while( InputTemp > 70 && InputTemp < 90){
System.out.print("Enter Temperature: ");
InputTemp = input.nextInt();
sum += InputTemp;
firstTemp = InputTemp;
lastTemp = InputTemp;
if (InputTemp > highestTemp){
highestTemp = InputTemp;
}
if (InputTemp < lowestTemp){
lowestTemp = InputTemp;
}
}
if (choice == 4){
while( InputTemp > 40 && InputTemp < 60){
System.out.print("Enter Temperature: ");
InputTemp = input.nextInt();
sum += InputTemp;
firstTemp = InputTemp;
lastTemp = InputTemp;
if (InputTemp > highestTemp){
highestTemp = InputTemp;
}
if (InputTemp < lowestTemp)
lowestTemp = InputTemp;
}
else
System.out.println("First Temperature: " +firstTemp);
System.out.println("Last Temperature: " +lastTemp);
System.out.println("Lowest Temperature: " +lowestTemp);
System.out.println("Highest Temperature: " +highestTemp);
System.out.println("Sum: " +sum);
System.out.println("Average: " +sum/simulations);
}
}
}
}
}
else
System.out.println("The program will now exit");
}}
答案 0 :(得分:0)
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int
simulations = 0,
InputTemp = 0,
choice = 0;
double
firstTemp = 0,
lastTemp = 0,
lowestTemp = 0,
highestTemp = 0,
sum = 0;
System.out.println("Select Season");
System.out.println("Press 1 for winter");
System.out.println("Press 2 for spring");
System.out.println("Press 3 for summer");
System.out.println("Press 4 for fall");
System.out.println("Press 5 to exit");
choice = input.nextInt();
if(choice==5) System.exit(0);
System.out.println("Enter number of simulations:");
simulations = input.nextInt();
for(int i = 1; i <= simulations; i++){
System.out.println("Starting simulation " + i);
if (choice == 1){
do{
System.out.print("Enter Temperature: ");
InputTemp = input.nextInt();
sum += InputTemp;
firstTemp = InputTemp;
lastTemp = InputTemp;
if (InputTemp > highestTemp)
highestTemp = InputTemp;
if (InputTemp < lowestTemp)
lowestTemp = InputTemp;
}while( InputTemp > 20 && InputTemp < 40); //end while
}//end if
if (choice == 2){
do{
System.out.print("Enter Temperature: ");
InputTemp = input.nextInt();
sum += InputTemp;
firstTemp = InputTemp;
lastTemp = InputTemp;
if (InputTemp > highestTemp)
highestTemp = InputTemp;
if (InputTemp < lowestTemp)
lowestTemp = InputTemp;
}while( InputTemp > 40 && InputTemp < 70); //end while
}//end if
if (choice == 3){
do{
System.out.print("Enter Temperature: ");
InputTemp = input.nextInt();
sum += InputTemp;
firstTemp = InputTemp;
lastTemp = InputTemp;
if (InputTemp > highestTemp)
highestTemp = InputTemp;
if (InputTemp < lowestTemp)
lowestTemp = InputTemp;
}while( InputTemp > 70 && InputTemp < 90); //end while
}//end if
if (choice == 4){
do{
System.out.print("Enter Temperature: ");
InputTemp = input.nextInt();
sum += InputTemp;
firstTemp = InputTemp;
lastTemp = InputTemp;
if (InputTemp > highestTemp)
highestTemp = InputTemp;
if (InputTemp < lowestTemp)
lowestTemp = InputTemp;
}while( InputTemp > 40 && InputTemp < 60); //end while
}//end if
}//end for
System.out.println("First Temperature: " +firstTemp);
System.out.println("Last Temperature: " +lastTemp);
System.out.println("Lowest Temperature: " +lowestTemp);
System.out.println("Highest Temperature: " +highestTemp);
System.out.println("Sum: " +sum);
System.out.println("Average: " +sum/simulations);
System.out.println("The program will now exit");
}//end main