r/javahelp • u/TeanutMutterJelly • 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
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
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
2
u/slowfly1st Jan 09 '20
I would probably use this one:
https://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#shuffle(java.util.List)
1
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
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
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.
1
0
1
u/TeanutMutterJelly Jan 09 '20
The Code