# Thread: Problem with Generator of random questions

1. Member Join Date
Nov 2011
Posts
3
Rep Power
0

## 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();

if(x=="A2")
System.out.println("Whats in the bed?");
Scanner sc2=new Scanner(System.in);
y2=sc2.nextLine();

if(x=="A3")
System.out.println("Whats under...?");
Scanner sc3=new Scanner(System.in);
y3=sc3.nextLine();

if(x=="A4")
System.out.println("How are u?");
Scanner sc4=new Scanner(System.in);
y4=sc4.nextLine();

if(x=="A5")
System.out.println("Is this a wwwww?");
Scanner sc5=new Scanner(System.in);
y5=sc5.nextLine();

return;

}

System.out.println("----------------------");

}
}```
Last edited by astronaut; 11-30-2011 at 04:34 PM.  Reply With Quote

2. ## Re: Problem with Generator of random questions

This doesn't cause a compiler error?

Java Code:
` x=otazky [(byte)(Math.round(Math.random()*4))];`  Reply With Quote

3. Member Join Date
Nov 2011
Posts
3
Rep Power
0

## 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.  Reply With Quote

4. ## 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.  Reply With Quote

5. ## 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.  Reply With Quote

6. Member Join Date
Nov 2011
Posts
3
Rep Power
0

## 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.

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();
j=j+1;
System.out.println("yeah, right");
}
else{
j=j;
}
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.  Reply With Quote

7. ## Re: Problem with Generator of random questions

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

8. ## 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.

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

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.  Reply With Quote

#### Posting Permissions

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