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();
}
}
}
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();
}
}
Comments
Leave a comment