First post here on Stack Overflow, however I've been stalking this site for a while. I am learning Java (I'm super noob at this) and using some websites I found online in order to help me better. One site that I was practicing on was http://programmingbydoing.com and I was completing one of the questions (which admittedly took me about 2 hours... - http://programmingbydoing.com/a/twenty-questions.html) and I came up with this code, which works, however I feel that I may have way too many nested if-statements and could have condensed this long code into something more simpler. I tried creating separate class files in the package (question1.class, question2.class, answers.class) however after not being able to call the classes and returning the values I needed, I gave up and did this:
package twoQuestions;
import java.util.Scanner;
public class twoQuestions {
public static void main (String [] args) {
String item;
System.out.println("TWO QUESTIONS!");
System.out.println("Think of an object and I will try to guess it!");
questions();
}
public static void questions(){
//Declare variables for user input
String input1;
String input2;
System.out.println("Question 1) Is it an animal, vegetable, or mineral?");
Scanner input = new Scanner(System.in);
input1 = input.nextLine();
if (input1.equalsIgnoreCase("animal") || input1.equalsIgnoreCase("vegetable") || input1.equalsIgnoreCase("mineral"))
{
//Start 2nd question
System.out.println("Question 2) Is it bigger than a breadbox");
Scanner inputt = new Scanner(System.in);
input2 = input.nextLine();
if (input2.equalsIgnoreCase("yes") || input2.equalsIgnoreCase("no")) {
if(input1.equalsIgnoreCase("animal") && input2.equalsIgnoreCase("no"))
{
System.out.println("My guess is that you are thinking of a squirel");
System.out.println("I would ask you if I'm right, but I don't actually care");
}
else if(input1.equalsIgnoreCase("animal") && input2.equalsIgnoreCase("yes"))
{
System.out.println("My guess is that you are thinking of a moose");
System.out.println("I would ask you if I'm right, but I don't actually care");
}
if(input1.equalsIgnoreCase("vegetable") && input2.equalsIgnoreCase("no"))
{
System.out.println("My guess is that you are thinking of a carrot");
System.out.println("I would ask you if I'm right, but I don't actually care");
}
if(input1.equalsIgnoreCase("vegetable") && input2.equalsIgnoreCase("yes"))
{
System.out.println("My guess is that you are thinking of a watermelon");
System.out.println("I would ask you if I'm right, but I don't actually care");
}
if(input1.equalsIgnoreCase("mineral") && input2.equalsIgnoreCase("no"))
{
System.out.println("My guess is that you are thinking of a paperclip");
System.out.println("I would ask you if I'm right, but I don't actually care");
}
if(input1.equalsIgnoreCase("mineral") && input2.equalsIgnoreCase("yes"))
{
System.out.println("My guess is that you are thinking of a Camaro");
System.out.println("I would ask you if I'm right, but I don't actually care");
}
}
else {
System.out.println("Enter what I asked please");
questions();
}
}
else {
System.out.println("Enter what I asked please");
questions();
}
}
}
Any help with either condensing this code or help with calling other classes would be great.
Aucun commentaire:
Enregistrer un commentaire