Thread: Java Beginner

    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    The Pleiades
    Posts
    299
    Rep Power
    9

    Java Beginner


    Hi just looking for a bit of help on a question/answer game im trying to make.

    What I am looking to do is create 2 arrays: 1st questions, 2nd answers.

    I want to print out the first question, then ask the user for input. then I want to test the user input against the element in the same store as the answers array. I want to then tell the user whether they are rght or wrong. Now I know I should be using some sort of loop to iterate through the array, but I want to just display the answers this way for the time being.

    Code:
    import java.util.Scanner;
    
    public class MyProgram {
    
         public static void main(String[] args) {
    	
    		 arraysMethod();
    		 
    	 } 
         
    	 public static void arraysMethod() {
    	 
    	     String input;
    	 
             String[] questions = new String[11];
    		 
    		 questions[1] = "What is the largest continent?";
    		 questions[2] = "Which animal has black and white stripes?";
    		 questions[3] = "What is a skeleton made of?";
    		 questions[4] = "Where do bats live?";
    		 questions[5] = "Where does a king and queen live?";
    		 questions[6] = "What colour does a rainbow start with?";
    		 questions[7] = "What sport does Wayne Rooney play?";
    		 questions[8] = "What animal goes 'oink oink'?";
    		 questions[9] = "Which planet is the nearest to Earth?";
    		 questions[10] = "What colour is a banana?";
    		 
    		 String[] answers = new String[11];
    		 
    		 answers[1] = "asia";
    		 answers[2] = "zebra";
    		 answers[3] = "bone";
    		 answers[4] = "cave";
    		 answers[5] = "castle";
    		 answers[6] = "red";
    		 answers[7] = "football";
    		 answers[8] = "pig";
    		 answers[9] = "moon";
    		 answers[10] = "yellow";
    
             System.out.println(questions[1]);
    
             Scanner scan = new Scanner(System.in);
    		 input = scan.nextLine();
    		 
             if (input == answers[1]) {
    		 
    		     System.out.println("Correct!");
    		 }
    		 else {
    		 
    		     System.out.println("Wrong!");
    		 
    		 }
    		 
    
    
    		 
    	 }
    	 
    }

    Thank you for any feedback!
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,632
    Rep Power
    1945
    I am not sure what you need help with. What you have looks like to do what you want.

    Can you be more specific?

    One note: when you compare string you should use the equals() function instead of ==.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    The Pleiades
    Posts
    299
    Rep Power
    9
    Sorry I did delve off into my own world writing that.

    What I basically need to do is check what the user has inputted with the value in the answers array at the corresponding index.

    Thanks for getting back to me!

    Can you use the equals() function within the if statement? If so how?

    Would it look something like this:

    Code:
    if (input.equals(answers[x])) { //x being whichever index
    
    }
    edit: Ah I see yes, just tried that out, my if statement is now working!

    Regards!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    21
    Rep Power
    0
    hello
    I suggest you to search via youtubes videos and look for good reviews
    and friend's honest recommendation.
    and u can also learn java from javatutorials
    which are easily available on net.

IMN logo majestic logo threadwatch logo seochat tools logo