I am creating arrays in Java and doing a little quiz game in the terminal. The arrays are not showing all the options when I sout the program.
When I am printing the arrays I can only start at [1] [1] | [2] [2] | [3] [3] | [4] [4] | and that forces me to only have a few slots for questions.
import java.util.Scanner;
public class Main{
//This is a short program that uses 2d arrays to do a quiz about tolkien characters
public static void main(String[] args){
String quiz[] = new String[4];
String quiz2D[][] = new String[9][9];
//creating questions and assigning them positions
quiz2D[0][0] = "What is gandalf?";
quiz2D[0][1] = "Valar";
quiz2D[0][2] = "Maiar";
quiz2D[0][3] = "Ainur";
quiz2D[0][4] = "Dwarf";
quiz2D[0][5] = "Hobbit";
quiz2D[1][1] = "Who was the original and first evil tolkien character chronologically?!";
quiz2D[1][2] = "Sauron";
quiz2D[1][3] = "Morgoth";
quiz2D[1][4] = "Melkor";
quiz2D[1][5] = "Super evil guy!";
quiz2D[2][2] = "Radagast";
quiz2D[2][3] = "Isildur son of Elendil";
quiz2D[2][4] = "Adar";
quiz2D[2][5] = "Tom Bombadil";
quiz2D[3][3] = "1954";
quiz2D[3][4] = "1917";
quiz2D[3][5] = "1945";
quiz2D[3][6] = "1943";
//Question 1 input output
for (int i = 0; i <= 5; i ) {
System.out.println(quiz2D[0][i]);
}
Scanner sc = new Scanner(System.in);
String a = sc.next(); //this will take the user's input
if ((quiz2D[0][2].toLowerCase().equals(a.toLowerCase()))){
System.out.println("You are correct! The answer is Maiar!");
}
else {
System.out.println("Nope, sorry.");
}
System.out.println(" "); // adds space before question
//Question 2 input output
for (int i = 1; i <= 5; i ) {
System.out.println(quiz2D[1][i]);
}
Scanner sx = new Scanner(System.in);
String ax = sx.next(); //this will take the user's input
if ((quiz2D[1][4].toLowerCase().equals(ax.toLowerCase()))) {
System.out.println("You are correct! The answer is Melkor!");
}
else {
System.out.println("Nope, sorry.");
}
System.out.println(" "); // adds space before question
//Question 3 input output
System.out.println("Who is the character that lived in the woods not featured in the films?");
for (int i = 2; i <= 5; i ) {
System.out.println(quiz2D[2][i]);
}
Scanner scnq3 = new Scanner(System.in);
String scannerQuestion3 = scnq3.next(); //this will take the user's input
if ((quiz2D[2][5].toLowerCase().equals(scannerQuestion3.toLowerCase()))){
System.out.println("You are correct!");
}else{
System.out.println("Nope, sorry.");
}
System.out.println("When was the lord of the rings published?");
for (int i = 3; i <= 5; i ) {
System.out.println(quiz2D[3][i]);
}
Scanner scnq4 = new Scanner(System.in);
String scannerQuestion4 = scnq3.next(); //this will take the user's input
if ((quiz2D[3][3].toLowerCase().equals(scannerQuestion4.toLowerCase()))) {
System.out.println("You are correct!");
} else {
System.out.println("Nope, sorry.");
}
}
}
CodePudding user response:
Because your for
loop doesn't go to that index.
for (int i = 3; i <7; i ) {
System.out.println(quiz2D[3][i]);
}
Is this your desired output?
CodePudding user response:
I tried your code with the question number 3 (so quiz2D[2][i], about "Who is the character that lived in the woods not featured in the films?") and it works. Here are the small changes :
quiz2D[2][0] = "Radagast";
quiz2D[2][1] = "Isildur son of Elendil";
quiz2D[2][2] = "Adar";
quiz2D[2][3] = "Tom Bombadil";
/****/
//Question 3 input output
System.out.println("Who is the character that lived in the woods not featured in the films?");
for(int i = 0; i<4; i ) {
System.out.println(quiz2D[2][i]);
}Scanner scnq3 = new Scanner(System.in);
String scannerQuestion3 = scnq3.next(); //this will take the user's input
if ((quiz2D[2][3].toLowerCase().equals(scannerQuestion3.toLowerCase()))){
System.out.println("You are correct!");
}else{
System.out.println("Nope, sorry.");
}
CodePudding user response:
Thats because you start your for loops at the question number, i=2
for example. When you start your for loops on i=0
the question wil be printed first and after that your answers.
The following example should work:
for(int i = 0; i<=5; i ) {
System.out.println(quiz2D[2][i]);
}
Scanner sc = new Scanner(System.in);
String a = sc.next(); //this will take the user's input
if ((quiz2D[2][2].toLowerCase().equals(a.toLowerCase())))
{
System.out.println("You are correct! The answer is Maiar!");
}
else{
System.out.println("Nope, sorry.");
}
Hope this helps you!