r/javahelp • • Jan 09 '20

Homework I need you help

I am a Senior High student and was assigned to program a quiz in java. But my professor wants the questions to be randomized.

Unfortunately, i don't know how to execute it

Help me please

๐Ÿ‘‡here's the link

The Code

3 Upvotes

16 comments sorted by

1

u/sbhandari Jan 09 '20

I am not sure how you are implementing but based on the question, I am expecting you to have your question/answers in a list. If not, then consider putting them in list or any collections. Now shuffle the list using Collections.shuffle() . This will randomize your objects in the list, so now you can proceed to the quiz in sequential order like inside a simple loop.

If you already had everything in place and were using a list, you just need to add this one line to shuffle before you start your quiz and you are set.

1

u/TeanutMutterJelly Jan 10 '20

Can you please show me how to do that?

1

u/TeanutMutterJelly Jan 09 '20 edited Jan 09 '20

I took a picture of it but I don't think I can post it here

Ill just type it out

1

u/[deleted] Jan 09 '20

you could have each question correspond to a number, like most do, and randomly have the number chosen with random.nextInt(n-x); with n being the max number of questions and x being the amount that have already been selected.

5

u/[deleted] Jan 09 '20

[removed] โ€” view removed comment

3

u/TeanutMutterJelly Jan 09 '20

I already have all the codes. I already inserted the questions and the choices.

Problem is, I am still a newbie in java programming and I do not know anything about the import .Random..

Thanks for your reply by the way ๐Ÿ™

2

u/[deleted] Jan 09 '20

[removed] โ€” view removed comment

3

u/TeanutMutterJelly Jan 09 '20

package quizbee; import java.util.InputMismatchException; import java.util.Scanner;

public class Quizbee extends Exception { public static void main (String[] args) throws QuizBee{ Scanner scan = new Scanner (System.in); String[] questions = {"1. Question","2. Question"}"; String[] choices = {"A. Choice\tB. Choice\tC.Choice"}; String[] answerKey = {"Answer","Answer"}; String ans = " "; int score = 0;

System.out.println ("TakeTest Quiz") System.out.println("Choose the letter of the correct answer);

for (int x=0;x<questions.length;x++) { System.out.println("\n"+questions[x]); System.out.println("\n"+choices[x]); System.out.println("\nAnswer: ");

try { ans = scan.nextLine(); ans = ans.toUpperCase(); if (ans.trim().equalsIgnoreCase(answerKey[x].substring(0.1))){ System.out.println("\nYou got it Right!") score++ }

else if (ans.equalsIgnoreCase("A")|| ans.equalsIgnoreCase("B")||ans.equalsIgnoreCase("C") && !ans.equalsIgnoreCase(answerKey[x].substring(0.1))){ System.out.println("\nSorry, the correct answer is"+answerKey[x]+"."); } else if {!ans.equalsIgnoreCase(answerKey[x]))){ throw new InputMismatchException(); } else if {ans.equals(" ") { throw new NullPointerException(); } } catch (InputMismatchException e) { System.out.println("Please select a letter from the letters A,B and C"); x--; } catch(NullPointerException e) { System.out.println("Please select a letter from the letters A,B and C"); x--; } } System.out.println("Your score is: " +score+ "10."); } private static Exception InputMismatchException() { throw new UnsupportedOperationException("Not Supported Yet"); } }

8

u/desrtfx Out of Coffee error - System halted Jan 09 '20

Please, format your code. There is no excuse for putting unformatted code here as the sidebar has an entire section Code posting as well as Help on how to post code.

Unformatted code is lazy and inconsiderate towards anybody willing to help.

0

u/[deleted] Jan 09 '20

[deleted]

1

u/TeanutMutterJelly Jan 09 '20

https://pastebin.com/715yPvG9

Thank you for your reply๐Ÿ™