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("----------------------");
}
}
Re: Problem with Generator of random questions
This doesn't cause a compiler error?
Code:
x=otazky [(byte)(Math.round(Math.random()*4))];
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.
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.
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.
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.
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:
Re: Problem with Generator of random questions
You should use the equals() method for comparing Strings, not the == operator.
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.
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 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.