Reputation: 27
So I'm trying to make a class to keep score throughout my Jeopardy program. The question I have is how do I keep the variable persistent in the responsePrompt method as well as increasing or decreasing the variable in the score. Below is the code I have so far
import java.util.*;
import java.io.*;
public class Game
{
public static void main(String[] args)
throws java.io.IOException
{
menu();
String[] jeoCategory = new String[]{"Brands", "Bands", "Lands", "Fact?", "France"};
printCategory(jeoCategory);
String[][] jeoBoard = new String[25][3];
loadFile(jeoBoard);
}
public static void printCategory(String[] jeoCategory)
{
for(int i = 0; i < 5; i++)
{
System.out.print("\t" + jeoCategory[i] + "\t");
if(i > 3)
{
System.out.println();
}
}
}
public static void menu()
{
System.out.println("Welcome to Jeopardy " + "\n" +
"Enter the correct answer and win 10 points" + "\n" +
"Enter the incorrect answer and lose 10 points" + "\n");
}
public static void loadFile(String[][] jeoBoard)
throws java.io.IOException
{
String filName = " ";
filName = "C:\\temp\\JeopardyValues.txt";
Scanner input = new Scanner(new File(filName));
for(int row = 0; row < 25; row++)
{
for(int col = 0; col < 3; col++)
{
jeoBoard[row][col] = input.nextLine();
}
}
input.close();
printBoard(jeoBoard);
}
public static void printBoard(String[][] jeoBoard)
{
for(int r = 0; r < 25; r++)
{
System.out.print("\t" + jeoBoard[r][2] + "\t");
if(r == 4 || r == 9 || r == 14 || r == 19 || r == 24)
{
System.out.println("\n");
}
}
responsePrompt(jeoBoard);
}
public static void responsePrompt(String[][] jeoBoard)
{
Scanner input = new Scanner(System.in);
Scanner resp = new Scanner(System.in);
int score = 0;
int counter = 0;
for(int i = 0; i < 25; i++)
{
System.out.print("Pick a question: ");
int ans = input.nextInt();
if(ans > 25 || ans < 0)
{
System.out.println("Invalid selection, please choose a number on the board");
break;
}
if(jeoBoard[ans - 1][2].equals(" "))
{
System.out.println("That question has already been answered, please pick another");
break;
}
System.out.println(jeoBoard[ans - 1][0]);
System.out.print("Your answer: ");
String questResponse = resp.nextLine();
if(questResponse.equalsIgnoreCase(jeoBoard[ans - 1][1]))
{
score++;
System.out.println("Correct " + "\t" + score);
jeoBoard[ans - 1][2] = " ";
counter++;
}
else
{
score--;
System.out.println("Incorrect " + "\t" + score);
jeoBoard[ans - 1][2] = " ";
counter++;
}
System.out.print("\n");
printBoard(jeoBoard);
if(counter == 25)
{
break;
}
}
System.out.println(" ");
}
}
Upvotes: 1
Views: 516
Reputation: 110
If you do not want your score
to be set to 0 every time responsePrompt
is called you should make it a global variable (outside of the methods) and your break is exiting the for
loop. I am not sure that you want to do that, I belive that you are trying to get the user to enter a different number that is valid. You should do a
while (ans > 25 || ans < 0 || jeoBoard[ans - 1][2].equals(" "))
{
System.out.println("Invalid selection, please choose a number on the board");
ans = input.nextInt();
}
I hope that helps.
Upvotes: 1