Question: JAVA Add a loop to the overall program that asks the user if they would like to repeat the program. If the user enters something
JAVA
Add a loop to the overall program that asks the user if they would like to repeat the program. If the user enters something other than a yes or no response the program should continue asking the user for a correct response until a correct response is given.
SHOW OUTPUT LOOP REPEATED 2 TIMES; PLEASE FIX ANY ERRORS THAT occur.
import java.util.Scanner;
public class LoopsMS
{
public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
String str;
int catCount = 0, dogCount = 0, llamaCount = 0;
String choice= "";
String degree;
double temperature;
double ansF;
double ansC;
int noGrades = 0;
int grade;
double noA =0;
double noB =0;
double noC =0;
double noD = 0;
double noF =0;
int highest = 0;
int lowest = 0;
double sumOfGrades = 0;
int totalNumbers = 0;
int diamondSize = 0;
int min= 0;
int z = 0;
System.out.print("Please enter sentence and I will check it for profane words: ");
str = keyboard.nextLine();
str = str.replaceAll("[<>{}()#$@^*.,?!'\"]","");
for (String word : str.split(" "))
{
if(word.equalsIgnoreCase("cat"))
catCount++;
else if(word.equalsIgnoreCase("dog"))
dogCount++;
else if(word.equalsIgnoreCase("llama"))
llamaCount++;
}
System.out.println("Your sentence contained the profane word \"cat\" " + catCount + " times.");
System.out.println("Your sentence contained the profane word \"dog\" " + dogCount + " times.");
System.out.println("Your sentence contained the profane word \"llama\" " + llamaCount + " times.");
System.out.println();
do
{
System.out.println("Please enter your temperature, Real number, followed by a space and the scale c or C for Celsius and f or F for Fahrenheit.");
temperature = keyboard.nextDouble();
degree = keyboard.next();
while(!degree.equalsIgnoreCase("C") && !degree.equalsIgnoreCase("F"))
{
System.out.println("You did not enter a valid temperature scale, Please enter a valid scale:");
degree = keyboard.next();
}
if (degree.equalsIgnoreCase("F"))
{
ansF = (temperature-32)*5/9;
System.out.printf(temperature +" degrees Fahrenheit is equal to %.2f Celsius", ansF);
}
else if (degree.equalsIgnoreCase("C"))
{
ansC = (temperature*9/5)+32;
System.out.printf(temperature + " degrees Celsius is equal to %.2f Fahrenheit ", ansC );
}
System.out.println(" Would you like to repeat the temperature project?(Enter Q or q for quit): ");
choice = keyboard.next();
System.out.println();
}
while(!choice.equalsIgnoreCase("Q"));
System.out.println();
System.out.println("Enter a grade from 0 to 100 or enter negative number to quit");
while (true)
{
grade =keyboard.nextInt();
if (grade<0)
break;
sumOfGrades=sumOfGrades+grade;
if(grade>=90 && grade<=100)
noA++;
else if (grade>=80 && grade<90)
noB++;
else if(grade>=70 && grade<80)
noC++;
else if(grade>=60 && grade<70)
noD++;
else if (grade>=0 && grade<60)
noF++;
if(noGrades==0)
highest=lowest=grade;
if(highest highest=grade; if(lowest>grade) lowest=grade; noGrades++; } totalNumbers =noGrades; System.out.println("Total number of grades are: "+noGrades); System.out.println("Highest Score: "+highest); System.out.println("Lowest Grade: "+lowest); System.out.printf("Total Average: %.2f",(sumOfGrades/noGrades)); System.out.println(); System.out.println((int)noF + "-"+ "Fs"+ "- " + (noF/totalNumbers)*100 + "%"); System.out.println((int)noD + "-"+ "Ds"+ "- " + (noD/totalNumbers)*100 + "%"); System.out.println((int)noC + "-"+ "Cs"+ "- " + (noC/totalNumbers)*100 + "%"); System.out.println((int)noB + "-"+ "Bs"+ "- " + (noB/totalNumbers)*100 + "%"); System.out.println((int)noA+ "-"+ "As"+ "- " + (noA/totalNumbers)*100 + "%"); System.out.println(); System.out.println("Please enter the size of the diamond you would like to build. (3 to 50)"); diamondSize=keyboard.nextInt(); min=1; for(int i=0;i { for(int j=diamondSize-1;j>i;j--) { System.out.print(" "); } for(int k=0;k { if(k%2==0) { System.out.print("*"); }else { System.out.print(" "); } } min+=2; System.out.println(); } z=diamondSize+diamondSize-3; for(int i=1;i { for(int j=0;j { System.out.print(" "); } for(int k=0;k { if(k%2==0) { System.out.print("*"); }else { System.out.print(" "); } } z-=2; System.out.println(); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
