Problem with Generator of random questions

• 11-30-2011, 05:31 PM
astronaut
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

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("----------------------");                       } }```
• 11-30-2011, 05:43 PM
Fubarable
Re: Problem with Generator of random questions
This doesn't cause a compiler error?

Code:

` x=otazky [(byte)(Math.round(Math.random()*4))];`
• 11-30-2011, 05:47 PM
astronaut
Re: Problem with Generator of random questions
Thanks, i changed it like this

Code:

`x=questions [(byte)(Math.round(Math.random()*4))];`
But it still doesnt work.
• 11-30-2011, 07:47 PM
Norm
Re: Problem with Generator of random questions
Quote:

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.
• 11-30-2011, 09:42 PM
Dark
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.
• 11-30-2011, 09:45 PM
astronaut
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();
}

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.

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 :o:
• 11-30-2011, 09:50 PM
Norm
Re: Problem with Generator of random questions
You should use the equals() method for comparing Strings, not the == operator.
• 12-01-2011, 07:55 PM
Dark
Re: Problem with Generator of random questions
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.

`if(a45 = b52-j24 || h83 = b35+q115){//do some more obscure code/}`