Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#1 Find the errors in the following codes: // This code contains ERRORS! // It a

ID: 441482 • Letter: #

Question

#1 Find the errors in the following codes: // 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)

Explanation / Answer

// i have updated your code.please check !!


#1 Find the errors in the following codes:
// This code contains ERRORS!
// It adds two numbers entered by the user.

int num1, num2;
String input;
char again = 'Y'; // here assing Y
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;
count++; // this line is missing.
}
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); // here double equal should come.. and one semicolon at end.