Find the errors in the following code: 1. // This code contains ERRORS! // It adds two
Question:
Find the errors in the following code:
1. // This code contains ERRORS!
// It adds two numbers entered by the user.
int num1, num2;
String input;
char again;
Scanner keyboard = new Scanner(System.in);
while (again == 'y' || again == 'Y')
System.out.print("Enter a number: ");
num1 = keyboard.nextInt();
System.out.print("Enter another number: ";
num2 = keyboard.nextInt();
System.out.println("Their sum is "+ (num1 + num2));
System.out.println("Do you want to do this again? ");
keyboard.nextLine(); // Consume remaining newline
input = keyboard.nextLine();
again = input.charAt(0);
2. // This code contains ERRORS!
int count = 1, total;
while (count <= 100)
total += count;
System.out.print("The sum of the numbers 1 - 100 is ");
System.out.println(total);
3. // This code contains ERRORS!
int choice, num1, num2;
Scanner keyboard = new Scanner(System.in);
do
{
System.out.print("Enter a number: ");
num1 = keyboard.nextInt();
System.out.print("Enter another number: ");
num2 = keyboard.nextInt();
System.out.println("Their sum is " + (num1 + num2));
System.out.println("Do you want to do this again? ");
System.out.print("1 = yes, 0 = no ");
choice = keyboard.nextInt();
} while (choice = 1)
4. // This code contains ERRORS!
// Print the numbers 1 through 10.
for (int count = 1, count <= 10, count++;)
{
System.out.println(count);
count++;
}
Step by Step Answer:
Starting Out With Java From Control Structures Through Data Structures
ISBN: 9780133957051
6th Edition
Authors: Tony Gaddis