所以我正努力在整个Jeopardy计划中上课。我的问题是如何将变量持久保存在responsePrompt方法中以及增加或减少分数中的变量。下面是我到目前为止的代码
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(" ");
}
}
答案 0 :(得分:1)
如果您不希望每次调用score
时将responsePrompt
设置为0,则应将其设置为全局变量(在方法之外)并且您的中断将退出{{1循环。我不确定你是否想这样做,我相信你试图让用户输入一个有效的不同号码。你应该做一个
for
我希望有所帮助。