第一次在这里发布一个非常烦人的问题。我决定用 Java 创建一个基于文本的 RPG,以巩固我所学的知识。对于所有冗长的游戏,保存/加载系统是必要的。这就是我的问题出现的地方。
我创建了一个菜单系统,效果很好。但是,当我决定开始一个新游戏时,我会在左侧、右侧和中间抛出 NumberFormatExceptions。我的源代码在下面,非常感谢任何帮助。我已将每个重要部分分成单独的 Java 文件。
//Imports
import java.io.IOException;
import java.io.File;
import java.io.*;
import java.util.Scanner;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
//'Choices' - By Yemi Oladimeji/YemZeee
class Choices{
public static void main(String [] args) throws IOException{
Scanner input = new Scanner(System.in);
String startChoice = null;
//Introduction begin
GameMethods.space();
System.out.println("######## ## ## ######## ######## ######## ######## ########");
System.out.println("## ## ## ## ## ## ## ## ## ");
System.out.println("## ## ## ## ## ## ## ## ## ");
System.out.println("## ######## ## ## ## ## ###### ###### ");
System.out.println("## ## ## ## ## ## ## ## ##");
System.out.println("## ## ## ## ## ## ## ## ##");
System.out.println("######## ## ## ######## ######## ######## ######## ########");
GameMethods.space();
System.out.println("Greetings, and welcome to Choices, a 2D text-based RPG!");
System.out.println("What would you like to do?");
System.out.println("<P>lay :: <A>bout :: <Q>uit");
startChoice = input.nextLine();
GameMethods.sChoice(startChoice);
}
}//End Choices.java
这是 GameMethods.java 文件的开头
import java.util.Scanner;
import java.io.*;
import java.io.IOException;
public class GameMethods extends ChoiceSaveMethods{
static Scanner input = new Scanner(System.in);
static String startChoice;
static String loadChoice;
public static void buffer(){
try{
System.in.read();
} catch(Exception e){
}
}
public static void space(){
System.out.println(" ");
}
public static void quit(){
System.exit(1);
}
public static void menuCall(){
System.out.println("<P>lay :: <A>bout :: <Q>uit");
startChoice = input.nextLine();
GameMethods.sChoice(startChoice);
}
public static void sChoice(String startChoice){
if(startChoice.equals("A")){
GameMethods.space();
System.out.println("This is a text-based RPG, created by Yemi Oladimeji.");
System.out.println("It focuses on the story and said story changes depend on \nthe choices you make in this world.");
System.out.println("As it is quite story driven, there will be the option at \nthe end of your journey to output your story to a text file for you to save and keep.");
System.out.println("Now that you know what the game is about, do you want to go back to the menu? \n(Y/N)");
String aChoice = input.nextLine();
if(aChoice.equals("Y")){
GameMethods.menuCall();
} else if(aChoice.equals("N")){
System.out.println("Or would you like to quit the game?");
String qChoice = input.nextLine();
if(qChoice.equals("Y")){
System.exit(1);
} else if(qChoice.equals("N")){
GameMethods.menuCall();
}
}
}
if(startChoice.equals("Q")){
GameMethods.quit();
}
if(startChoice.equals("P")){
try{
BufferedReader saveFile = new BufferedReader(new FileReader("C:\\Users\\Yemi\\Desktop\\TheGame\\SaveFiles\\TextSave1.txt"));
areaMarkers[0] = Integer.parseInt(saveFile.readLine());
areaMarkers[1] = Integer.parseInt(saveFile.readLine());
areaMarkers[2] = Integer.parseInt(saveFile.readLine());
areaMarkers[3] = Integer.parseInt(saveFile.readLine());
saveFile.close();
for(int x = 0; x < 4; x++){
if(areaMarkers[x] == 1){
System.out.println("A save game has been located. Would you like to load it?(Y/N)");
loadChoice = input.nextLine();
if(loadChoice.equals("Y")){
System.out.println("The save/load feature works.");
} else if(loadChoice.equals("N")){
System.out.println("Starting new game...");
GameMethods.gameStart();
}
}
}
} catch(IOException ex){
ex.printStackTrace();
}
forestArea();
}
}
public static void gameStart(){
//Introduction begin
GameMethods.space();
System.out.println("Welcome to 'Choices'. You influence this world with your decisions. You react \nhow you wish...");
GameMethods.space();
System.out.println("This game was built to immerse you in a hooking storyline with no graphics, \nand only your imagination can help with that. \nThat and some audio. For that purpose, I recommend that you use headphones \nwhilst playing this game.");
System.out.println("I require two pieces of information. Your name. What is it?\n(This is what you will be called in game)");
System.out.println("Your name: ");
String name1 = input.nextLine();
System.out.println("And the name for this user account\n(So the program can locate the required files): ");
String userName = input.nextLine();
System.out.println("There are two rules...");
System.out.println("This (*) means a flashback is occurring.");
System.out.println("And when there is a pause, it means that you \nshould press Enter/Return to continue.");
System.out.println("You get that? Good.");
System.out.println("Now. Let us begin your game and remember. \nYour choices affect your path.");
//Introduction end
}
}
这是最后一个 Java 文件,包含区域和保存方法:
import java.io.*;
import java.util.Scanner;
import java.io.IOException;
public class ChoiceSaveMethods{
//Area variables:
static int[] areaMarkers = new int[4];
static int[] areaChoices = new int[4];
static Scanner input = new Scanner(System.in);
public static void forestArea(){
GameMethods.space();
System.out.println("Area 1: Forest. What would you like to do here?: ");
System.out.println("1 - Save the game?");
System.out.println("2 - View a description of the area?");
System.out.println("3 - Progress to the next area?");
System.out.println("4 - Exit to the main menu?");
areaChoices[0] = input.nextInt();
if(areaChoices[0] == 1){
try{
FileWriter saveFile1 = new FileWriter("C:\\Users\\Yemi\\Desktop\\TheGame\\SaveFiles\\TextSave1.txt");
areaMarkers[0] = 1;
saveFile1.write(1);
System.out.println("Game has been saved successfully.");
saveFile1.close();
forestArea();
} catch (IOException ex){
ex.printStackTrace();
}
} else if(areaChoices[0] == 2){
System.out.println("This is a forest. It has trees.");
forestArea();
} else if(areaChoices[0] == 3){
System.out.println("Progressing to next area...");
cityArea();
} else if(areaChoices[0] == 4){
System.out.println("Returning to main menu.");
GameMethods.menuCall();
}
}
public static void cityArea(){
GameMethods.space();
System.out.println("Area 2: City. What would you like to do here?: ");
System.out.println("1 - Save the game?");
System.out.println("2 - View a description of the area?");
System.out.println("3 - Progress to the next area?");
System.out.println("4 - Exit to the main menu?");
areaChoices[1] = input.nextInt();
if(areaChoices[1] == 1){
try{
FileWriter saveFile2 = new FileWriter("C:\\Users\\Yemi\\Desktop\\TheGame\\SaveFiles\\TextSave1.txt");
areaMarkers[1] = 1;
saveFile2.write(areaMarkers[1]);
System.out.println("Game has been saved successfully.");
saveFile2.close();
cityArea();
} catch(IOException ex){
ex.printStackTrace();
}
} else if(areaChoices[1] == 2){
System.out.println("This is a city. There are alot of people.");
cityArea();
} else if(areaChoices[1] == 3){
System.out.println("Progressing to next area...");
magmaArea();
} else if(areaChoices[1] == 4){
System.out.println("Returning to main menu.");
GameMethods.menuCall();
}
}
public static void magmaArea(){
GameMethods.space();
System.out.println("Area 3: Magma Pool. What would you like to do here?: ");
System.out.println("1 - Save the game?");
System.out.println("2 - View a description of the area?");
System.out.println("3 - Progress to the next area?");
System.out.println("4 - Exit to the main menu?");
areaChoices[2] = input.nextInt();
if(areaChoices[2] == 1){
try{
FileWriter saveFile3 = new FileWriter("C:\\Users\\Yemi\\Desktop\\TheGame\\SaveFiles\\TextSave1.txt");
areaMarkers[2] = 1;
saveFile3.write(areaMarkers[2]);
System.out.println("Game has been saved successfully.");
saveFile3.close();
magmaArea();
} catch(IOException ex){
ex.printStackTrace();
}
} else if(areaChoices[2] == 2){
System.out.println("A blazing pool of molten rock. Very hot.");
magmaArea();
} else if(areaChoices[2] == 3){
System.out.println("Progressing to next area.");
hospitalArea();
} else if(areaChoices[2] == 4){
System.out.println("Returning to main menu.");
GameMethods.menuCall();
}
}
public static void hospitalArea(){
GameMethods.space();
System.out.println("Area 4: Hospital. What would you like to do here?: ");
System.out.println("1 - Save the game?");
System.out.println("2 - View a description of the area?");
System.out.println("3 - Exit to the main menu.");
areaChoices[3] = input.nextInt();
if(areaChoices[3] == 1){
try
{
FileWriter saveFile4 = new FileWriter("C:\\Users\\Yemi\\Desktop\\TheGame\\SaveFiles\\TextSave1.txt");
areaMarkers[3] = 1;
saveFile4.write(areaMarkers[3]);
System.out.println("Game has been successfully saved.");
saveFile4.close();
hospitalArea();
} catch(IOException ex){
ex.printStackTrace();
}
} else if(areaChoices[3] == 2){
System.out.println("A disused, abandoned hospital. It has an eerie feel about it.");
hospitalArea();
} else if(areaChoices[3] == 3){
System.out.println("Returning to the main menu...");
GameMethods.menuCall();
}
}
}
抛出的异常是这样的:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at GameMethods.sChoice(GameMethods.java:59)
at Choices.main(Choices.java:32)
我意识到这是很多要求,特别是对于第一次发布海报,但我在这里严重缺乏想法。随意复制并运行代码。任何答案都将非常感激。再次谢谢你。