Hello I have a problem in my coding. This coding is a Quiz Game creator. Lets say I want to create 3 question in this game.
and when the game starts.. it will display the 3 question.
So at that selection of question I chose to do Number 5 when it only has 3. It should have displayed a message saying Invalid selection please try again. But instead the program display a Question 5 but with null data.
How can I make my program display that message? It doesnt seem to work when I try the IF statement it tends to skip that if statement or I typed something wrong but I don't see where it is.
/** * Write a description of class QuizGamePA3 here. * * @author (your name) * @version (a version number or a date) */ import java.util.*; import java.io.*; import java.util.Scanner; public class QuizGamePA3 { public static void main(String[]args ) throws FileNotFoundException { Scanner input = new Scanner (System.in); String s = " "; int[] choice,answer,pick,pick2,numbers; choice = new int[5000]; answer = new int[5000]; numbers= new int[5000]; pick = new int[5000]; pick2= new int[5000]; int alt = 1; String[] data,category,option,option2,option3,option4; data = new String[5000]; category = new String[5000]; option = new String[5000]; option2= new String[5000]; option3= new String[5000]; option4= new String[5000]; int Point=0; System.out.printf("\n%10s Welcome to Quiz Game Creator \n\n",s); System.out.printf("%16s How to do it?%n",s); System.out.printf("1] Specify number of question you want to do.\n"); System.out.printf("2] Create the question CATEGORY\n"); System.out.printf("3] Enter the QUESTION for each category.\n"); System.out.printf("4] Enter the OPTION for each question.\n"); System.out.printf("5] Provide the ANSWER for each question.\n"); System.out.printf("6] Then your QUIZ GAME is ready to be play by your friends\n"); System.out.println(); System.out.println("-----------------------------------------------"); System.out.println("Press the ENTER key to start creating the Quiz"); System.out.println("-----------------------------------------------"); input.nextLine(); System.out.printf("\nHow many questions you want to create: "); choice[1] = input.nextInt(); // Input of how many questions to create System.out.println(); System.out.print('\u000C'); input.nextLine(); System.out.println(); System.out.println(" Please enter your question CATEGORY"); for(int a=1; a<=choice[1];a++){ System.out.print(numbers[1]+1+ "] "); data[a]=input.nextLine(); numbers[1]++; } System.out.print('\u000C'); System.out.println(); System.out.println(" Please enter your QUESTION"); for(int b=1; b<=choice[1];b++){ System.out.println(); System.out.println("Category: " +data[b]); System.out.printf(choice[3]+1+"] "); category[b] = input.nextLine();// Input of the questions for certain category choice[3]=choice[3]+1; } System.out.print('\u000C'); System.out.println(); System.out.println("Please enter your question OPTION"); for(int c = 1; c<=choice[1];c++){ System.out.println(); System.out.println("Question : "+category[c]);// Storing of 4 options for each questions System.out.println(); System.out.printf("1] "); option[c] = input.nextLine(); System.out.printf("2] "); option2[c] = input.nextLine(); System.out.printf("3] "); option3[c] = input.nextLine(); System.out.printf("4] "); option4[c] = input.nextLine(); System.out.println(); System.out.println("Please Set the answer to this question"); System.out.print("The answer is: "); answer[c] = input.nextInt();// Chosen answer for each questions. input.nextLine(); System.out.println(); } System.out.println("Your Quiz Game is now READY. Press the ENTER key to start..."); input.nextLine(); System.out.print('\u000C'); System.out.printf("%15s Welcome to the QUIZ GAME\n\n",s); System.out.printf("%8s Game Rule: 1] Select one topic.\n",s); System.out.printf("%18s 2] Answer the MCQ question.\n",s); System.out.printf("%18s 3] Each correct answer will be awarded 10 points.\n",s); System.out.printf("%18s 4] Each incorrect answer will be deducted 10 points.\n",s); System.out.printf("%18s 5] The game will be over once you answer the question wrongly.\n",s); System.out.println(); System.out.printf("%10s Press the ENTER key to start the game...\n",s); input.nextLine(); System.out.print('\u000C'); for(int z=1; z<=choice[1];z++){ System.out.println(" Your current point is: "+Point); // game currency for(int d=1; d<=choice[1];d++){ System.out.println((d)+ "] "+data[d]);// display the categories of the questions } System.out.print("Please select question topic : "); pick[z]=input.nextInt();// This is where the user choose which category to answer if (pick2[z] > choice[1]) { System.out.println(); System.out.println("Invalid number. Please re-select your selection"); System.out.println(); } else if(pick2[z] <= choice[1]) { System.out.println("--------------------------------------"); System.out.println(); System.out.println("Category: "+data[pick[z]]); System.out.println("Question: "+ category[pick[z]]); System.out.println(); System.out.println("1] "+option[pick[z]]); System.out.println("2] "+option2[pick[z]]); System.out.println("3] "+option3[pick[z]]); System.out.println("4] "+option4[pick[z]]); System.out.print("What is your answer: "); pick2[z]=input.nextInt(); if (pick2[z] == answer[pick[z]]){ Point=Point + 10; System.out.println(); System.out.println("Your answer is correct! You earn 10 Points"); System.out.println(); System.out.println("------------------------------------------"); } else{ System.out.print('\u000C'); Point=Point-10; System.out.println(); System.out.println("Your answer is WRONG!"); System.out.println(" GAME OVER!"); System.out.println(); System.out.println("Your final point is :"+Point+" Point"); System.out.println(); System.out.println(" Thanks your for playing"); input.nextLine(); System.exit(0); } } } } }