Here is my first original program, written after 5 days of learning java. The goal was to include a looping control structure with decision making paths (if/else).
____________________________________________________________________
import java.util.Scanner;
public class AverageTemp
{
//Calculates the average temperature of weekdays in centigrade
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
double monday, tuesday, wednesday,thursday,friday;
char option;
//User input for five weekdays
do
{
System.out.println ("What was the average temperature on Monday?:");
monday = keyboard.nextDouble();
System.out.println ("What was the average temperature on Tuesday?:");
tuesday = keyboard.nextDouble();
System.out.println ("What was the average temperature on Wednesday?:");
wednesday = keyboard.nextDouble();
System.out.println ("What was the average temperature on Thursday?:");
thursday = keyboard.nextDouble();
System.out.println ("What was the average temperature on Friday?:");
friday = keyboard.nextDouble();
double average = (monday + tuesday + wednesday + thursday + friday)/5;
System.out.println ("The average temperature this week was: " + average);
//Decision Tree comments on temperature
if (average >= 38)
{ System.out.println("Wow, that is hot!");
}
else if (average <= -10)
{ System.out.println ("Brrr!");
}
else
{ System.out.println ("That's all right.");
}
//option to continue or end program
System.out.println ("Enter Y to continue or N to quit: ");
option = keyboard.next().charAt(0);
}
while (option == 'Y' || option == 'y');
}
}
____________________________________________________________________
import java.util.Scanner;
public class AverageTemp
{
//Calculates the average temperature of weekdays in centigrade
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
double monday, tuesday, wednesday,thursday,friday;
char option;
//User input for five weekdays
do
{
System.out.println ("What was the average temperature on Monday?:");
monday = keyboard.nextDouble();
System.out.println ("What was the average temperature on Tuesday?:");
tuesday = keyboard.nextDouble();
System.out.println ("What was the average temperature on Wednesday?:");
wednesday = keyboard.nextDouble();
System.out.println ("What was the average temperature on Thursday?:");
thursday = keyboard.nextDouble();
System.out.println ("What was the average temperature on Friday?:");
friday = keyboard.nextDouble();
double average = (monday + tuesday + wednesday + thursday + friday)/5;
System.out.println ("The average temperature this week was: " + average);
//Decision Tree comments on temperature
if (average >= 38)
{ System.out.println("Wow, that is hot!");
}
else if (average <= -10)
{ System.out.println ("Brrr!");
}
else
{ System.out.println ("That's all right.");
}
//option to continue or end program
System.out.println ("Enter Y to continue or N to quit: ");
option = keyboard.next().charAt(0);
}
while (option == 'Y' || option == 'y');
}
}
Comments
Post a Comment