Results 1 to 8 of 8
Like Tree3Likes
  • 1 Post By Fubarable
  • 1 Post By Norm
  • 1 Post By Dark

Thread: Problem with Generator of random questions

  1. #1
    astronaut is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Problem with Generator of random questions

    Hi,
    im a complete noob in Java, been learning just for a few months.
    I tried to make program that generate a random questions and ask for answers (1 question --> 1 answer). Here I need to pick 3 Qs out of 5.
    Ive got no syntax errors, but it still doesnt work. Do you know where the problem is? I work in BlueJ.

    I would be really grateful for any help.
    Thank you very much



    Java Code:
    import java.util.Scanner;
    
    public class Test
    {
      public static void main(String[] args)
       {
           byte i,poc=3; 
           String x,y1,y2,y3,y4,y5;     // x question, y answer
           
           String[] questions={"A1","A2","A3","A4","A5"};
           
           
                for(i=0; i<poc; i++)
          
                {
                   
                   x=otazky [(byte)(Math.round(Math.random()*4))];
                   
                   
                   if(x=="A1")
                   System.out.println("Whats in the box?");
                   Scanner sc1=new Scanner(System.in);
                   y1=sc1.nextLine();
                   System.out.println("answer is: "+y1);
                  
                   
                   if(x=="A2")
                   System.out.println("Whats in the bed?");
                   Scanner sc2=new Scanner(System.in);
                   y2=sc2.nextLine();
                   System.out.println("answer is: "+y2);
                   
                   
                   if(x=="A3")
                   System.out.println("Whats under...?");
                   Scanner sc3=new Scanner(System.in);
                   y3=sc3.nextLine();
                   System.out.println("answer is: "+y3);
                   
                   
                   if(x=="A4")
                   System.out.println("How are u?");
                   Scanner sc4=new Scanner(System.in);
                   y4=sc4.nextLine();
                   System.out.println("answer is: "+y4);
                   
                  
                   if(x=="A5")
                   System.out.println("Is this a wwwww?");
                   Scanner sc5=new Scanner(System.in);
                   y5=sc5.nextLine();
                   System.out.println("answer is: "+y5);
                  
                   
                   return;
                   
                   
                }
                                  
                System.out.println("----------------------");
               
               
       }
    }
    Last edited by astronaut; 11-30-2011 at 04:34 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with Generator of random questions

    This doesn't cause a compiler error?

    Java Code:
     x=otazky [(byte)(Math.round(Math.random()*4))];
    astronaut likes this.

  3. #3
    astronaut is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: Problem with Generator of random questions

    Thanks, i changed it like this

    Java Code:
    x=questions [(byte)(Math.round(Math.random()*4))];
    But it still doesnt work.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Problem with Generator of random questions

    it still doesnt work.
    Please explain. Show what the code does now and add comments to it describing what is wrong with its output and say what you want the output to be.

    One problem in the code is that you need to use the equals() method to compare two Strings, not the == operator.
    astronaut likes this.

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Problem with Generator of random questions

    You are telling us that your program doesn't work however you are not telling us what is the actual problem. If you don't tell us what your expected output is, and what your actual output is if it compiles, then we have absolutely no idea where to look.
    astronaut likes this.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  6. #6
    astronaut is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: Problem with Generator of random questions

    Hey, I figured out what was bad there. Brackets in IF cycles were missing.

    if(x=="A1"){
    System.out.println("Whats in the box?");
    Scanner sc1=new Scanner(System.in);
    y1=sc1.nextLine();
    System.out.println("answer is: "+y1);
    }


    And please would you help me with checking the answers? I can check and evaluate question with one certain answer (for example word CAR), but how could I add another word into template of correct answers? (for example CAR and VEHICLE would give the same "Your answer is True" sentence)

    I tried to write it like if(b.equals("TheRightAnswerONE" || "TheRightAnswerTWO")) but it did not work, compiler said these signs "||" cant be there.


    Java Code:
    if(x=="A1"){
                   System.out.println("Question no."+(i+1));
                   System.out.println("This is the question....?");
                   Scanner sc=new Scanner(System.in);
                   y=sc.nextLine();
                   b=y.toLowerCase();
                    if(b.equals("the_right_answer")){
                       j=j+1;
                       System.out.println("yeah, right");
                       }
                      else{
                       j=j;
                       System.out.println("no, wrong answer");
                      }
                   System.out.println("score: "+j);
                   System.out.println();
                  }


    Btw. sorry for my English. Im not native
    Thanks for trying to help me guys
    Last edited by astronaut; 11-30-2011 at 08:50 PM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Problem with Generator of random questions

    You should use the equals() method for comparing Strings, not the == operator.

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Problem with Generator of random questions

    Java Code:
    if (string1.equalsIgnoreCase("car")||string1.equalsIgnoreCase("vehicle")){//Do actions};
    The || means or in a logical sequence. Ultimately you can use the || operator whenever a decision is being made. As far as I know, there is also no limit to how many ||'s can be placed in a decision statement.

    These articles will help you.
    Comparing Strings and Portions of Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
    Using && in if statement : If Statement*«*Statement Control*«*Java Tutorial
    Using || (or operator) in if statement : If Statement*«*Statement Control*«*Java Tutorial

    This article may help you.
    The Order of Evaluation of Logic Operators

    Also, I suggest you get in the habit of properly naming your variables. x1 and y1 are not good string names. Think about it this way. If your game contains 10,000 questions with answers and you then get a partner, if all your variable names are a,b,c,d,e,f,g,etc... then how does the new coder know what each variable does? Or how about you take a break from your coding project, you have all these improperly named variables and now you come back to it. You don't remember where you left off so you try to back track. I don't know about you but if I see this
    Java Code:
    if(a45 = b52-j24 || h83 = b35+q115){//do some more obscure code/}
    I'm not going to know what the hell that does. See what I'm getting at? That obscure code would be simple to reverse in comparison to some actual logical statements or formulas you will be using in the future.
    Last edited by Dark; 12-01-2011 at 07:02 PM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Help with Random Number Generator
    By celtics in forum New To Java
    Replies: 0
    Last Post: 03-07-2011, 08:18 PM
  2. Random number generator
    By zerwik in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 12:10 PM
  3. Random Generator
    By Learning Java in forum New To Java
    Replies: 9
    Last Post: 05-31-2010, 08:15 PM
  4. Random number generator
    By Michailangelo in forum Advanced Java
    Replies: 4
    Last Post: 04-02-2010, 06:47 PM
  5. Random Generator
    By padutch2 in forum New To Java
    Replies: 1
    Last Post: 12-03-2007, 06:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •