In: Computer Science
How to fix this debugging in java
Problem Debugging
This problem is to assign a value to num2 based on the input value of num1. It should then print both numbers.
int num1 = 0;
int num2 = 0;
System.out.print("Enter a number - 1, 2, or 3: ");
num1 = keyboard.nextInt();
if (num1 == 1);
num2 = 2;
else if (num1 == 2);
num2 = 3;
else if (num1 == 3);
num2 = 4;
System.out.println("num1 = " + num1 // + " and num2 = " + num2);
System.out.println("\n");
Light yellow marks indicate the errors and fixed places.
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
int num1 = 0;
int num2 = 0;
System.out.print("Enter a number - 1, 2, or 3: ");
num1 = keyboard.nextInt();
if (num1 == 1)
num2 = 2;
else if (num1 == 2)
num2 = 3;
else if (num1 == 3)
num2 = 4;
System.out.println("num1 = " + num1 + " and num2 = " + num2);
System.out.println("\n");
}
}