Answer to Question #191387 in Java | JSP | JSF for Hamza

Question #191387

Correct the program so that it works properly.


import java.util.*;

public class Main {

static Scanner console = new Scanner(System.in);

public static void main(String[] args) {

char response;

double num1;

double num2;

System.out.println("This program adds two numbers.");

System.out.print("Would you like to run the program: (Y/y) ");

response = console.next().charAt(0);

System.out.println();

while (response == 'Y' && response == 'y')

{

System.out.print("Enter two numbers: ");

num1 = console.nextInt();

num2 = console.nextInt();

System.out.println();

System.out.printf("%.2f + %.2f = %.2f %n", num1, num2, (num1 - num2));

System.out.print("Would you like to add again: (Y/y) ");

response = console.next().charAt(0);

System.out.println();

}

}

}


1
Expert's answer
2021-05-10T11:08:03-0400


import java.util.Scanner;


public class Main {


	static Scanner console = new Scanner(System.in);


	public static void main(String[] args) {


		char response;


		double num1;


		double num2;
		do {


			System.out.println("This program adds two numbers.");


			System.out.print("Would you like to run the program: (Y/N) ");


			response = console.next().charAt(0);


			System.out.println();
			if (response == 'Y') {


				System.out.print("Enter two numbers: ");


				num1 = console.nextInt();


				num2 = console.nextInt();


				System.out.println();


				System.out.printf("%.2f + %.2f = %.2f %n", num1, num2, (num1 + num2));


				System.out.print("Would you like to add again: (Y/N) ");


				response = console.next().charAt(0);


				System.out.println();
				
			}


		} while (response != 'N');
		console.close();


	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS