Description:
This lab is used to simulate seasonal temperature.
The user will enter a season, and number of trial of number will be executed.
This program will print out the following:
1. first generated data
2. last generated data
3. highest generated data
4. lowest generated data
5. sum of generated data
6. average of generated data
Code:
/* * this program will simulate temperature according to the season entered. * it will print out the following: * 1. first generated data * 2. last generated data * 3. highest generated data * 4. lowest generated data * 5. sum of generated data * 6. average of generated data * */ import java.util.Scanner ; public class Lab01 { private static Scanner scanner = new Scanner( System.in ); public static void main(String[] args) { int programLooping = 0; outer: while (programLooping == 0) { int rollRange = 20; //set up the roll range int rollTemp = 0; //initialize int rollBase = 0; //initialize System.out.println("Select a season you want to simulate:"); // Display the string. System.out.println("1. Spring"); System.out.println("2. Summer"); System.out.println("3. Fall"); System.out.println("4. Winter"); System.out.println("5. Exit"); System.out.print("Your choice is:"); String inSeason = scanner.nextLine(); //set up the base and modify range if necessary. switch(inSeason) { case ("1") : System.out.print("40-70"); rollRange +=10 ; rollBase = 40; break; case ("2"): System.out.print("70-90"); rollBase = 70; break; case ("3"): System.out.print("40-60"); rollBase = 40; break; case ("4"): System.out.print("20-40"); rollBase = 20; break; case ("5"): programLooping = 1 ; return; default: System.out.print("invalid input\n"); continue outer; } //promote user to set up the number of trial System.out.println( "\nEnter number of trials you want:"); String StInTrial = scanner.nextLine(); int intInTrial1 = Integer.parseInt( StInTrial ); //declare and initialize variables int preRollTempHigh = 0; int preRollTempLow = 100; int rollTempTotal = 0; //start rolling!! for (int Counter=0; Counter < intInTrial1; Counter ++) { rollTemp = (int) (Math.random() * rollRange + rollBase); //System.out.println(Counter+ " "+ rollTemp ); //used to monitor rolled number if (Counter == 0) //print out first generated data System.out.println("The first data generated is :"+rollTemp); if (Counter == intInTrial1-1) //print out last generated data System.out.println("The last data generated is :"+rollTemp); if (preRollTempHighrollTemp) //if rolled number is smaller than previous, save it to preRollTempLow preRollTempLow = rollTemp; rollTempTotal += rollTemp; //sum rolled number up } //print out the rest of collected data . System.out.println("The highest data generated is :"+preRollTempHigh); System.out.println("The lowest data generated is :"+preRollTempLow); System.out.println("The sum of generated data is :"+rollTempTotal); System.out.println("The avarage of generated data is :"+rollTempTotal/intInTrial1); } } }
Screenshot: