I have written this program and it works great. However, I need to figure out ho
ID: 3651809 • Letter: I
Question
I have written this program and it works great. However, I need to figure out how to add an exit so that if the user types -1 as their first input the program will skip everything and go straight to message "END OF PROGRAM". Here is my program below.import java.util.Scanner;
public class CFP {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);//Physical connection to keyboard
double fine = 0.0;
double sum = 0;
double average = 0.0;
double value = 0.0;
int count = 0;
int roundedAverage = 0;
System.out.println("**************************************** CARBON FOOTPRINT PROGRAM *********************************************************** ");
System.out.println("This program will calculate the fine your city may pay, based on the average range of its Carbon Footprint. ");
System.out.println("The fine index is as follows: ");
System.out.println("If the average Carbon Footprint (CFP) per month is <= 1 then there is "No Fine."");
System.out.println("If CFP > 1 and CFP <= 3 Fine = $1000000.0");
System.out.println("If CFP > 3 and CFP <= 5 Fine = $2000000.0");
System.out.println("If CFP > 5 and CFP <= 7 Fine = $3000000.0");
System.out.println("If CFP > 7 Fine = $4500000.0 ");
System.out.println("Enter the value of the Carbon Footprint (Whole Number Only) or enter -1 to exit.");
value = keyboard.nextDouble();
while(value >= 0) {
System.out.println("Enter the value of the Carbon Footprint (Whole Number Only) or enter -1 to exit.");
sum += value;
count ++;
value = keyboard.nextDouble();
}
average = sum/count;
roundedAverage = (int)Math.floor(average);
if(roundedAverage<=1)
fine = 0;
else if(roundedAverage >1 && roundedAverage <= 3)
fine = 1000000;
else if(roundedAverage > 3&& roundedAverage <= 5)
fine = 2000000;
else if(roundedAverage > 5&& roundedAverage <= 7)
fine = 3000000;
else
fine = 4500000;
System.out.println("The sum of all the Carbon Foot Print values equals: " + sum + " ");
System.out.println("The total number of readings entered equals: " + count + " ");
System.out.println("The Carbon Footprint "Real Average" equals: " + average + " ");
System.out.println("For your benefit, the rounded down Carbon Footprint average equals: " + roundedAverage + " ");
System.out.println("Now we will compute your fine based on the rounded average value and the fine table. ");
System.out.println("Your cities fine equals: $" + fine + " ");
System.out.println(""END OF PROGRAM"");
keyboard.close();
//end main
//end class
}
}
Explanation / Answer
Changes to your program are written in BOLD LETTERS
import java.util.Scanner;
public class CFP {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);//Physical connection to keyboard
double fine = 0.0;
double sum = 0;
double average = 0.0;
double value = 0.0;
int count = 0;
int roundedAverage = 0;
System.out.println("**************************************** CARBON FOOTPRINT PROGRAM *********************************************************** ");
System.out.println("This program will calculate the fine your city may pay, based on the average range of its Carbon Footprint. ");
System.out.println("The fine index is as follows: ");
System.out.println("If the average Carbon Footprint (CFP) per month is <= 1 then there is "No Fine."");
System.out.println("If CFP > 1 and CFP <= 3 Fine = $1000000.0");
System.out.println("If CFP > 3 and CFP <= 5 Fine = $2000000.0");
System.out.println("If CFP > 5 and CFP <= 7 Fine = $3000000.0");
System.out.println("If CFP > 7 Fine = $4500000.0 ");
System.out.println("Enter the value of the Carbon Footprint (Whole Number Only) or enter -1 to exit.");
value = keyboard.nextDouble();
//Change To your Program
if(value<0)
{
sum+=value;
count++;
}
while(value >= 0) {
System.out.println("Enter the value of the Carbon Footprint (Whole Number Only) or enter -1 to exit.");
sum += value;
count ++;
value = keyboard.nextDouble();
}
average = sum/count;
roundedAverage = (int)Math.floor(average);
//Change to your program
if(roundedAverage<0)
fine = -1;
else if(roundedAverage<=1)
fine = 0;
else if(roundedAverage >1 && roundedAverage <= 3)
fine = 1000000;
else if(roundedAverage > 3&& roundedAverage <= 5)
fine = 2000000;
else if(roundedAverage > 5&& roundedAverage <= 7)
fine = 3000000;
else
fine = 4500000;
//Change To your Program
if(fine>=0)
{
System.out.println("The sum of all the Carbon Foot Print values equals: " + sum + " ");
System.out.println("The total number of readings entered equals: " + count + " ");
System.out.println("The Carbon Footprint "Real Average" equals: " + average + " ");
System.out.println("For your benefit, the rounded down Carbon Footprint average equals: " + roundedAverage + " ");
System.out.println("Now we will compute your fine based on the rounded average value and the fine table. ");
System.out.println("Your cities fine equals: $" + fine + " ");
}
System.out.println(""END OF PROGRAM"");
keyboard.close();
//end main