I\'m trying to get this if / else program in Java and I\'m not sure where it\'s
ID: 3717893 • Letter: I
Question
I'm trying to get this if / else program in Java and I'm not sure where it's going wrong.
package u3a1_debugfixifstmts;
import java.util.Scanner;
/**
*
* @Searls
*/
public class U3A1_DebugFixIFStmts {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Teacher's Copy");
Scanner input = new Scanner(System.in);
// prompt the user to enter 3 ints
// and read them into
// firstChoice
//secondChoice
//thirdChoice
System.out.print("Enter three integers: ");
int firstChoice = input.nextInt();
int secondChoice = input.nextInt();
int thirdChoice = input.nextInt();
//Determine & print the state of choices made
if (firstChoice == 0)
System.out.println("State of choices: " +
"no choices made yet");
if (secondChoice == 0)
System.out.println("State of choices: " +
"user made first choice (" + firstChoice + ") " +
"number of choices = 1");
else if (thirdChoice = 0)
System.out.println("State of choices: " +
"user made first choice (" + firstChoice + ") " +
"user made second choice (" + secondChoice + ") " +
"number of choices = 2");
System.out.println("State of choices: " +
"user made first choice (" + firstChoice + ") " +
"user made second choice (" + secondChoice + ") " +
"user made third choice (" + thirdChoice + ") " +
"number of choices = 3")
}
}
Explanation / Answer
import java.util.Scanner;
/**
*
* @Searls
*/
public class U3A1_DebugFixIFStmts {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Teacher's Copy");
Scanner input = new Scanner(System.in);
// prompt the user to enter 3 ints
// and read them into
// firstChoice
//secondChoice
//thirdChoice
System.out.print("Enter three integers: ");
int firstChoice = input.nextInt();
int secondChoice = input.nextInt();
int thirdChoice = input.nextInt();
//Determine & print the state of choices made
if (firstChoice == 0)
System.out.println("State of choices: " +
"no choices made yet");
if (secondChoice == 0)
System.out.println("State of choices: " +
"user made first choice (" + firstChoice + ") " +
"number of choices = 1");
else if (thirdChoice == 0)
System.out.println("State of choices: " +
"user made first choice (" + firstChoice + ") " +
"user made second choice (" + secondChoice + ") " +
"number of choices = 2");
System.out.println("State of choices: " +
"user made first choice (" + firstChoice + ") " +
"user made second choice (" + secondChoice + ") " +
"user made third choice (" + thirdChoice + ") " +
"number of choices = 3");
}
}