我用我的代码完成了90%以上...想要添加一些东西,但我不知道如何继续。
这是我的代码到目前为止...我还有其他类,我在这段代码中调用它们。
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class SensorStatsApp extends Sensor {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
HumiditySensor hSensor = new HumiditySensor();
int sChoice = -2;
int hChoice = -2;
int seasonTemp = -2;
int humidityTemp = -2;
int iterations = -2;
while(true)
{
try {
System.out.println("What season would you like to simulate?");
System.out.println(" 1. Winter \n 2. Spring \n 3. Summer \n 4. Fall \n 5. Random \n 6. EXIT");
System.out.print("Selection: ");
sChoice = sc.nextInt(); // gathers user input for which season to simulate
System.out.println(); // adds a break from the previous question
if(sChoice == 6){
System.exit(0); // exits the program
}
System.out.println("What humidity would you like to simulate?");
System.out.println(" 1. Full Range \n 2. Low Humidity \n 3. High Humidity \n 4. Random \n 5. EXIT");
System.out.print("Selection: ");
hChoice = sc.nextInt(); // gathers user input for humidity to simulate
System.out.println(); // adds a break from previous question
if(hChoice == 5){
System.exit(0); // exits the program
}
System.out.print("Input number of simulations: ");
iterations = sc.nextInt(); // gathers user input for number of iterations
}
catch(InputMismatchException e) { // if user inputed non numeric characters
System.out.println(e + " - Error: expecting a number for input");
return; // exits the program
}
for(int i = 0; i < iterations; i++){
try {
seasonTemp = seasonToSimulate(sChoice); // sends the sChoice int to be converted
humidityTemp = humidityToSimulate(hChoice); // sends the sChoice int to be converted and processed
display(seasonTemp, humidityTemp, i); // displays the current iteration of information
}
catch(IOException e){
System.out.println(e + " - Error: user input"); // lets the user know why their sChoice failed.
return; // doesn't return anything cause the return type is void. This just ends the program.
}
}
}
}
public static void display(int sTemp, int hTemp, int iteration){
System.out.println();
System.out.println(++iteration + " Simulations:");
System.out.println("Season Temperature: " + sTemp);
System.out.println("Humidity Temperature: " + hTemp);
System.out.println();
}
public static int humidityToSimulate(int choice) throws IOException {
int temp = -2;
HumiditySensor hSensor = new HumiditySensor();
Boolean done = false; // if random another iteration through switch is needed
while(!done){ // if the user decides to use random then done != true so I can iterate one more time in the switch statement
switch(choice){ // sChoice is the season in terms of an int
case 1: { // full range, if choice is 1 it falls into this case and breaks at the statement break; (ends the current switch)
temp = hSensor.getHumidity();
done = true;
break;
}
case 2: { // low humidity, if choice is 2 it falls into this case.
temp = hSensor.getLowHumidity();
done = true;
break;
}
case 3: { // high humidity
temp = hSensor.getHighHumidity();
done = true;
break;
}
case 4: { // random
choice = (int) Math.random() * 3; // random times (3) for 3 humidity types
if(choice == 0) choice++; // 0 is a possibility but not an option
break;
}
case 5: { // exit
temp = -1; // lets the calling function know it's done
break;
}
default:
{
throw new IOException(); // user gave improper option
}
}
}
return temp;
}
public static int seasonToSimulate(int choice) throws IOException {
int temp = -1;
TemperatureSensor tSensor = new TemperatureSensor();
Boolean done = false; // if random through switch is needed
while(!done){
switch(choice) {
case 1: { // winter
temp = tSensor.getWinterTemp();
done = true;
break;
}
case 2: { // spring
temp = tSensor.getSpringTemp();
done = true;
break;
}
case 3: { // summer
temp = tSensor.getSummerTemp();
done = true;
break;
}
case 4: { // fall
temp = tSensor.getFallTemp();
done = true;
break;
}
case 5: { // random
choice = (int) Math.random() * 4;// random *(4) for 4seasons
if(choice == 0) choice++;// 0 is a possibility not an option
break;
}
case 6: { // exit
temp = -1; // lets the calling function know it's done
break;
}
default:
{
throw new IOException(); // user gave improper option
}
}
}
return temp;
}
}
我如何在我的程序中执行以下操作? 每次迭代都应显示为每个温度和湿度生成的读数。最后,您的程序应显示模拟摘要,如下所示:
Season: _________ (if random, then display Random:Summer for example)
1- First temperature generated
2- Last temperature generated
3- Lowest temperature generated
4- Highest temperature generated
5- Total sum of all temperatures generated
6- Average for the season
Humidity Type: _________ (if random, then display Random:Full for example)
1- First humidity reading generated
2- Last humidity reading generated
3- Lowest humidity reading generated
4- Highest humidity reading generated
5- Total sum of all humidity readings generated
6- Average humidity reading