# will this text based quiz code work?

This topic is 747 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello
for my first project in java i have created a quiz program
a simple text based one, i have only completed the java class at codecademy so i dont know much, but of what i have learned, i have but this together.
i dont know how to tun it though and i am not very comfortable on where to put like colons and semibrack and that kind of stuff.
what i am trying to do here is making one arraylist for each question, and then using a for each loop to show the alternatives

here is the code:

[Test]
import java.Util.ArrayList;
import java.Util.Scanner;Scanner user_Input = new Scanner (System.in);

class quiz {

public static void main(String[] args) {

System.out,println(QuestionA);

System.out,println("Alternatives:");

int Answer_Wrong = "That is wrong, next question!"

int Answer_Right = "That is correct, next question!"

int QuestionA = "what is 1 + 3 + 7 + 1 x 6 x 2 + 1 + 9?"

int QuestionB = "what is the capital of canada?"

int QuestionC = "What is the capital of brazil"

ArrayList<String> QuestionB = new ArrayList<String>;

ArrayList<string> QuestionC = new ArrayList<String>;

ArrayList<String> QuestionA = new ArrayList<String>;

for(Integer Alternatives : QuestionA);

if (user_Input = ("c") ) {

System.out.println(QuestionB)
}

else if {
System.out.println(QuestionB)
}

}

[/Test]

-i do not know how to write the user_input code. can someone plz correct it for me?

-is the int correct, or should i have them in a hashmap or arraylist?

-and is the FOR EACH loop correct here?
i worked with it yesterday and could not figure out what i was think at that time xD

-Should i put the arraylists under the main public void and make a sepparate for all the commands to keep it a little more

-if you see any mistakes or things i can do better or cange, plz tell me and plz tell me why also, its very good to know the reason, that way i'll understand more.

thx in advance ^^

##### Share on other sites

It looks like you are trying to build the entire program all at the same time. A lot is wrong in your code. I could explain it all, but I fear you'll get so much information that you'd be just completely lost.

So instead, I would like to suggest to start smaller. (No worries, a lot of what you have above will be needed at some point, so don't throw it all away )

You should start thinking in building blocks. Making one block at a time, stacking blocks on top of each other to build the program. In particular, small building blocks. It may seem like a weird way to build a large program, but it's one of the few ways to manage all the little details that you must handle.

For comparison, you don't build a castle by buying a lot of bricks and wood, and then throwing them together and see if it works. You make a global layout (answering questions like I posted yesterday) so you have a good idea of what you need to have, and then build small sections, one at a time. Each section consists of a number of walls, each wall consists of a (large) number of bricks. A single bricks doesn't look much, but as you can see in the world, people can literally build castles with it.

In Java, a building block is a class. One elementary class in your program is QuizQuestion I think, I'd suggest you start with that. Think what it should contain before reading further.

I would say it contains a question, a number of answers, and a way to indicate which answer is the correct one. Using the arraylist for storing answers is useful. Have a go at it.

If you succeed, you can make an extension.

Just storing the text of the questions and answers is one thing, a useful extension is to be able to print the question to the screen. Try adding a "print" method that outputs the question and the possible answers to the screen.

If you have that, you should be able to use your class by code like

public class Quiz {
public final void main(String[] args) {

QuizQuestion question = new QuizQuestion(.......); // Instantiate a question with question text, and answers.
question.print();
}
}

If you run the program, the question should appear at the screen.

(if you wonder how I add that code in a box in my post, the forum has a "code" tag. Paste your code into the editor, select it, then press the "code" tag, the "<>" symbol next to the image button, at the bar above. That will do proper formatting, and also add some syntax highlighting colours when you press "Post".

If you get this running, think of a small improvement to just printing the question, then try to implement it.

Edited by Alberth

##### Share on other sites

It looks like you are trying to build the entire program all at the same time. A lot is wrong in your code. I could explain it all, but I fear you'll get so much information that you'd be just completely lost.

So instead, I would like to suggest to start smaller. (No worries, a lot of what you have above will be needed at some point, so don't throw it all away :) )

You should start thinking in building blocks. Making one block at a time, stacking blocks on top of each other to build the program. In particular, small building blocks. It may seem like a weird way to build a large program, but it's one of the few ways to manage all the little details that you must handle.

For comparison, you don't build a castle by buying a lot of bricks and wood, and then throwing them together and see if it works. You make a global layout (answering questions like I posted yesterday) so you have a good idea of what you need to have, and then build small sections, one at a time. Each section consists of a number of walls, each wall consists of a (large) number of bricks. A single bricks doesn't look much, but as you can see in the world, people can literally build castles with it.

In Java, a building block is a class. One elementary class in your program is QuizQuestion I think, I'd suggest you start with that. Think what it should contain before reading further.

I would say it contains a question, a number of answers, and a way to indicate which answer is the correct one. Using the arraylist for storing answers is useful. Have a go at it.

If you succeed, you can make an extension.
Just storing the text of the questions and answers is one thing, a useful extension is to be able to print the question to the screen. Try adding a "print" method that outputs the question and the possible answers to the screen.

If you have that, you should be able to use your class by code like

public class Quiz {
public final void main(String[] args) {

QuizQuestion question = new QuizQuestion(.......); // Instantiate a question with question text, and answers.
question.print();
}
}
If you run the program, the question should appear at the screen.

(if you wonder how I add that code in a box in my post, the forum has a "code" tag. Paste your code into the editor, select it, then press the "code" tag, the "<>" symbol next to the image button, at the bar above. That will do proper formatting, and also add some syntax highlighting colours when you press "Post".

If you get this running, think of a small improvement to just printing the question, then try to implement it.

So by building block you mean like

Public class questions
for all the questions and replies to the user

Public class altenatives
for the arraylists with alternatives

Public class quiz
for the code that does runs the program (system.out.println and such)

Inside the (.......) i should add the question right.
But would it be smart to make a public class with all of these and name them
Public class questions

QuizQuestion question_A = new QuizQuestion;

QuizQuestion question_B = new QuizQuestion;
And then i add another class that stores the "print" code
Public class [insertnamehere]

Question_A.print();

For(string Alt_A : Alternatives); // Alt_A is
short for alternatives_A wich i am naming the arraylist with the answers.
(System.out.println(Alt_A);

If user_input = ("A");

Else if

Question_B.print();
For(String Alt_B : Alternatives_B);

i hope you see where i am going with this

And what program do you use to run the code??? I have bern looking for days

##### Share on other sites

So by building block you mean like

Public class questions

for all the questions and replies to the user

Public class altenatives

for the arraylists with alternatives

Public class quiz

for the code that does runs the program (system.out.println and such)

yes, you split the problem into smaller blocks. This changes the problem from one big thing, to a lot of very small blocks.

The blocks have a purpose too, so it gets more clear where to put what functionality. For example, you now decided that a reply to the user comes from "questions", and not eg from "alternatives". In other words, you're drawing lines in the sand where each part is.  You can also draw lines between blocks, for example, "quiz" needs questions, so it uses the "questions" class.

Quite likely, your first lines are not optimal, but it's a start (and this won't get better, "programming" is to a large extent knowing how and where to draw these lines. While you get better at it with more experience, finding the optimal solution is always just out of reach ).

Therefore, if you find your lines are not good (this happens quite often), improve on them. This is easiest earlier in the process when you don't have much code. When you have code, and you change a building block, all code that uses the old building block has to be changed.

Inside the (.......) i should add the question right.

That was my intention indeed. However, you made a different set of blocks. If you use those, such things do change as well. Obviously you will still have a question, but it may end up at another spot in the program.

But would it be smart to make a public class with all of these and name them
Public class questions

QuizQuestion question_A = new QuizQuestion;

QuizQuestion question_B = new QuizQuestion;

The first question is always "does it work at all?". In other words, does the program actually do what it is supposed to do. Asking about "smart" implies you are confident things will work, and are looking for a way to do the same, but with less work or such that tomorrow you can add more building blocks without effort. I would suggest pick a (any) solution, and make it work first.

Having said that, no, it's not smart. You can use an arraylist of questions instead of storing every question in a separate variable (just like you have an arraylist of alternatives rather than each alternative in a separate variable). However, don't worry about it yet, once you have a working program, you can worry about improvements.

And then i add another class that stores the "print" code

This is possible, but not recommended in object oriented programming. A class is a unit of data and functionality, it's a smart building block. If you store a question in a class, you can add code to print itself, tell other blocks how many alternatives it has, or return whether "F" is the right answer to the question. A class is not just data, it can also have code associated with it.

Example

class Question {
private final String question;

public Question(String question) {
this.question = question;
}

public print() {
System.out.printf("%s?\n", question);
}
}

This class has a string named "question", that you can set from a parameter in the constructor. It also has a "print" method, that outputs the question to the console.

(Sorry about the printf, Java probably has a better way to print a string, but I am quite stuck in C/C++, and don't know such things.)

And what program do you use to run the code??? I have bern looking for days

A Java compiler, and a Java interpreter. The former is called javac, and the latter is called java (yep, same as the language).

I think however, you'll be much happier with an IDE that handles these things.

for example.

Edited by Alberth