-4

I'm writing a program whose topic was closed earlier and I'm having trouble with it. I wrote the code though.Also I want to use the random code in which if I chose a season it has to randomly select numbers between like 20 and 40. The output doesn't show me the first, last, highest and lowest temperature generated and neither the sum nor average.

import java.util.Scanner;

public class 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");

}}
user3326710
  • 1
  • 1
  • 1
  • Your question is very unclear and the code you're providing has syntax errors. Please read the guidelines for posting a good example: http://stackoverflow.com/help/mcve – Durandal Feb 20 '14 at 02:09
  • @user3326710 but your `while` loops are not going to be executed even once, because your `InputTemp` is zero initially – mangusta Feb 20 '14 at 02:13
  • This isn't a question. Also, you have curly braces in all the wrong places in your code. – Epiglottal Axolotl Feb 20 '14 at 02:43
  • I'm sorry I should've been clearer. Is the while loop correct to generate the simulator – user3326710 Feb 20 '14 at 04:12

1 Answers1

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
mangusta
  • 3,470
  • 5
  • 24
  • 47
  • Could you give some explanation of what you changed and why? Just dumping the code with the errors corrected won't help all that much. – Epiglottal Axolotl Feb 20 '14 at 02:45
  • @Stendika I just corrected braces, replaced `while` with `do-while` and added option 5 :) – mangusta Feb 20 '14 at 02:52
  • it can further be improved by including `Math.random()` but I leave it to the author – mangusta Feb 20 '14 at 03:27
  • How do use Math.random(). I corrected the code but it kept asking to enter temperature while starting simulation. Each simulation has to generate random numbers. Do I use math.random in a while loop? – user3326710 Feb 20 '14 at 05:08
  • You should remove these two lines `System.out.print("Enter Temperature: "); InputTemp = input.nextInt();` from all 4 cases, and put `InputTemp = * Math.random();` instead – mangusta Feb 20 '14 at 05:16
  • Do I have to have to make up a scaling factor or is it a real variable? – user3326710 Feb 20 '14 at 05:32
  • see this link to make a proper statement for generating a random number within a given range: http://stackoverflow.com/questions/363681/generating-random-numbers-in-a-range-with-java – mangusta Feb 20 '14 at 05:35
  • Another problem I ran the program and it's not simulated properly. My professor tells me to use a do while loop so i select the options and simulations stop, it should go back to the menu. – user3326710 Feb 24 '14 at 01:02
  • /Select Season Press 1 for winter Press 2 for spring Press 3 for summer Press 4 for fall Press 5 to exit 2 Enter number of simulations: 7 Starting simulation 1 Starting simulation 2 Starting simulation 3 Starting simulation 4 Starting simulation 5 Starting simulation 6 Starting simulation 7 First Temperature: 70.0 Last Temperature: 70.0 Lowest Temperature: 0.0 Highest Temperature: 70.0 Sum: 8753.0 Average: 1250.4285714285713 The program will now exit – user3326710 Feb 24 '14 at 02:59