對於此代碼,我想模擬投幣式遊戲機並將遊戲的用戶名和分數放置到標題爲scores.txt的文本文件中,以便如果用戶選擇選項2,可以查看他們的分數。將數據附加到Java文件中的正確方法
但是,我在我的FileWriter語句(行朝向最後並標註了註釋)上收到了許多錯誤,特別是我不明白用於編碼CP1252的不可映射字符。從我檢查的任何地方,當有人使用不同的字符(如日文字符)時,我會看到這個錯誤 - 那麼爲什麼會出現這樣的錯誤呢?我已經看過代碼的例子,但是我還沒有學過流,嘗試和捕獲,或緩衝。
使用的FileWriter和PrintWriter的可有人向我解釋如何創建一個FileWriter對象,它正確地傳遞給PrintWriter對象,以及如何從文件(scores.txt)正確讀取數據。非常感謝,如果這是一個簡單的錯誤,很抱歉。
的問題的具體領域:
File file = new File(「scores.txt」); //illegal start of expression
if (!file.exists())
{
file.createNewFile();
}
Scanner inputFile = new Scanner(file);
String line = inputReader.nextLine();
FileWriter fwriter = new FileWriter(「scores.txt」, true); //this is where the error CP1252,
PrintWriter outputWriter = new PrintWriter(file);
outputFile.println(username);
outputFile.println(userFinalTotal);
}
else if (option == 2)
{
if (file.exists())
{
while (inputFile.hasNext())
{
username = inputFile.nextLine();
System.out.println ("Name\n------\n" + name + "\n");
userFinaltotal = inputFile.nextDouble();
System.out.printf("Scores\n------\n$%.2f\n", userFinalTotal);
System.out.println();
inputReader.close();
}
下面是完整的程序,看看那裏的變量從何而來。
import java.util.Scanner;
import java.util.Random;
import java.io.*;
public class SlotsMachine
{
public static void main(String[] args) throws IOException
{
int number;
System.out.println ("Welcome to the Slot Machine Simulator!");
System.out.println ("\nActions\n1. Start a new game\n2. Scores\n3. Exit");
System.out.print ("\nPlease select an action: ");
Scanner keyboard = new Scanner(System.in);
int option = keyboard.nextInt();
while (option != 1 && option != 2 && option != 3)
{
System.out.print ("\nThat is not an option. Please select an item number between 1-3: ");
option = keyboard.nextInt();
break;
}
if (option == 1)
{
String username;
double startingTotal = 100.0;
double userTotal = startingTotal;
System.out.print ("\nBefore the game begins, please enter your name: ");
username = keyboard.next();
System.out.print ("\nGame start! You will begin with $100.00. Enter a negative value to quit the game. Good luck, " + username + "!");
do
{
double bet = keyboard.nextDouble();
bet = 0.0;
userTotal = startingTotal - bet;
System.out.print ("You currently have: " + startingTotal + "\nHow much would you like to bet?");
double winnings = 0.0;
double userFinalTotal = 0.0;
if ((bet < 0) || (userFinalTotal <= 0))
{
break;
}
while (bet > userFinalTotal)
{
System.out.print("\nYour bet is greater than your current total. Please enter a valid amount: ");
bet = keyboard.nextDouble();
}
Random generator = new Random();
int slot1 = generator.nextInt(6);
keyboard.nextLine();
int slot2 = generator.nextInt(6);
int slot3 = generator.nextInt(6);
String firstSlot = "";
switch (slot1)
{
case 0:
firstSlot = "Cherries";
break;
case 1:
firstSlot = "Oranges";
break;
case 2:
firstSlot = "Plums";
break;
case 3:
firstSlot = "Bells";
break;
case 4:
firstSlot = "Melons";
break;
case 5:
firstSlot = "Bars";
break;
}
String secondSlot = "";
switch (slot2)
{
case 0:
secondSlot = "Cherries";
break;
case 1:
secondSlot = "Oranges";
break;
case 2:
secondSlot = "Plums";
break;
case 3:
secondSlot = "Bells";
break;
case 4:
secondSlot = "Melons";
break;
case 5:
secondSlot = "Bars";
break;
}
String thirdSlot = "";
switch (slot3)
{
case 0:
thirdSlot = "Cherries";
break;
case 1:
thirdSlot = "Oranges";
break;
case 2:
thirdSlot = "Plums";
break;
case 3:
thirdSlot = "Bells";
break;
case 4:
thirdSlot = "Melons";
break;
case 5:
thirdSlot = "Bars";
break;
}
System.out.println ("-------------------------------");
System.out.println ("" + firstSlot + " " + secondSlot + " " + thirdSlot);
System.out.print ("-------------------------------");
if (slot1 == slot2 && slot1 == slot3)
{
winnings = bet * 3;
userFinalTotal = userTotal + winnings;
System.out.printf ("\nNumber of matches: 3. You win: $%.2f", winnings);
System.out.printf ("\nYou currently have: $%.2f", userFinalTotal);
}
else if ((slot1 == slot2 && slot2 != slot3) || (slot1 == slot3 && slot1 != slot2) || (slot2 == slot3 && slot3 != slot1))
{
winnings = bet * 2;
userFinalTotal = userTotal + winnings;
System.out.printf ("\nNumber of matches: 2. You win: $%.2f", winnings);
System.out.printf ("\nYou currently have: $%.2fn", userFinalTotal);
}
else
{
System.out.printf ("\nNumber of matches: 0. You win: $%.2f", winnings);
System.out.printf ("\nYou currently have: $%.2f", userFinalTotal);
}
} while (userTotal > 0);
File file = new File(「scores.txt」); //illegal start of expression
if (!file.exists())
{
file.createNewFile();
}
Scanner inputFile = new Scanner(file);
String line = inputReader.nextLine();
FileWriter fwriter = new FileWriter(「scores.txt」, true); //this is where the error CP1252
PrintWriter outputWriter = new PrintWriter(file);
outputFile.println(username);
outputFile.println(userFinalTotal);
}
else if (option == 2)
{
if (file.exists())
{
while (inputFile.hasNext())
{
username = inputFile.nextLine();
System.out.println ("Name\n------\n" + name + "\n");
userFinaltotal = inputFile.nextDouble();
System.out.printf("Scores\n------\n$%.2f\n", userFinalTotal);
System.out.println();
inputReader.close();
}
}
else
{
System.out.println("There are no scores to display at this time.");
}
System.out.println("Actions:");
System.out.print("1. Start a new game\n2. View scores\n3. Exit ");
System.out.println("Please select an action: ");
option = keyboard.nextInt();
}
else if (number == 3)
{
System.out.print ("\nGoodbye!");
System.exit(0);
}
}
}
你得到什麼錯誤? – HedonicHedgehog
請嘗試通過http://ericlippert.com/2014/03/05/how-to-debug-small-programs/工作 - 大多數人不會通過這樣荒謬漫長的代碼來尋找你的針草垛。如果你孤立了你的問題,你將能夠更好地表達你的問題,因此幫助你更容易。 –
@VucarTimnärakrul因爲它包含了代碼中的東西,所以我想包含代碼,以便可以看到如果這是一個錯誤需要包含哪些變量。我在有錯誤的行旁邊放置了一條註釋,指出錯誤:編碼CP1252的不可映射字符,以及非法表達式開始並說我需要一個;在哪裏。我將嘗試從現在開始將代碼的特定部分添加到頂部,這要感謝輸入 – mrkrebs