- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
对于这段代码,我想模拟一台老虎机,并将游戏的用户名和分数放入名为scores.txt 的文本文件中,这样如果用户选择选项 2,他们就可以查看他们的分数。
但是,我在 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);
}
}
}
最佳答案
现在可以编译了,唯一的问题是它多次打印分数。
导入java.util.Scanner; 导入 java.util.Random; 导入java.io.*;
public class Slot3
{
public static void main(String[] args) throws IOException
{
System.out.println ("Welcome to the Slot Machine Simulator!");
int option = 0;
//if the user selects a 1 or 2 (does not want to exit) then this loop will run
do
{
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);
option = keyboard.nextInt();
keyboard.nextLine();
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();
keyboard.nextLine();
}
//this will occur if the user selects 1 to play the game
if (option == 1)
{
double money = 100.00;
double bet = 0.00;
double winnings = 0.00;
double score = 0.00;
int count = 0;
System.out.print ("\nBefore the game begins, please enter your name: ");
String username = keyboard.nextLine();
System.out.print ("\nGame start! You will begin with $100.00. Enter a negative value to quit the game. Good luck, " + username + "!");
System.out.printf("\nYou currently have $%.2f.", 100.00);
do
{
System.out.printf("\n\nHow much would you like to bet? ");
bet = keyboard.nextDouble();
if ((bet < 0) || (money <= 0))
{
break;
}
while (bet > money)
{
System.out.print("\nYour bet is greater than your current total. Please enter a valid amount: ");
bet = keyboard.nextDouble();
}
//create random numbers
Random generator = new Random();
int slot1 = generator.nextInt(6);
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 ("\n-------------------------------");
System.out.printf ("%-12s%-10s%5s\n", firstSlot , secondSlot , thirdSlot);
System.out.print ("\n-------------------------------");
//check how many of the slots match to calculate the winnings
if (slot1 == slot2 && slot1 == slot3)
{
winnings = bet * 3;
money -= bet;
score = money + winnings;
System.out.printf ("\nNumber of matches: 3. You win: $%.2f", winnings);
System.out.printf("\nYou currently have: $%.2f", score);
}
else if ((slot1 == slot2 && slot2 != slot3) || (slot1 == slot3 && slot1 != slot2) || (slot2 == slot3 && slot3 != slot1))
{
winnings = bet * 2;
money -= bet;
score = money + winnings;
System.out.printf ("\nNumber of matches: 2. You win: $%.2f", winnings);
System.out.printf("\nYou currently have: $%.2f", score);
}
else
{
winnings = bet * 0;
money -= bet;
score = money + winnings;
System.out.printf ("\nNumber of matches: 0. You win: $%.2f", winnings);
System.out.printf("\nYou currently have: $%.2f", score);
}
} while ((bet > 0) && (money > 0));
FileWriter fwriter = new FileWriter("scores.txt", true);
PrintWriter outputWriter = new PrintWriter(fwriter);
outputWriter.printf("\n\n%1s%15s" , "Name" , "Score");
outputWriter.printf ("\n\n%1s%15s" , "----" , "-----");
outputWriter.printf ("\n\n%1s%15s" , username , score);
outputWriter.close();
System.out.println("\n\nGame over! Your score has been written to scores.txt, " + username + "!");
} //end of actions for select option 1
//option 2 user wants to read their scores
if (option == 2)
{
File myFile = new File("scores.txt");
//if there are no scores to read
if (!myFile.exists())
{
System.out.println("There are no scores to display at this time.");
continue;
}
File file = new File("scores.txt");
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext())
{
String username = inputFile.nextLine();
System.out.println(username);
}
inputFile.close();
} //close option 2
} while (option != 3); //close 1st do-while loop
if (option == 3)
{
System.out.print ("\nGoodbye!");
System.exit(0);
}
}
}
关于java - 在Java中将数据追加到文件的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26963635/
在 Python 中,我可以附加到一个空数组,例如: >>> a = [] >>> a.append([1,2,3]) >>> a.append([1,2,3]) >>> a [[1, 2, 3],
我正在阅读(并慢慢尝试)在 php 中与 txt 文件交互的方法。我已经尝试过追加,它将数据添加到txt文件的末尾但是 a+ 与 a 有何不同 在 w3schools 中它说: 一个 append 。
我想执行一个非常简单的操作:合并两个形状文件。具体来说,我有美国每个州的人口普查区域形状文件,我想将它们合并到一个形状文件中。最终,我想获取组合的形状文件并在一组经纬度坐标上执行叠加,以确定我的坐标属
当我们使用 append 和 cut 运算符时会出现什么问题? append2([],L,L):-!. append2([H|T],L,[H|TL]):-append2(T,L,TL).
我有一个函数处理程序: function handler(data) { console.log(`1. ${data}`); } 我想在相同的范围内附加或重新定义,如下所示: let old
我目前正在使用很多这样的内容来重构应用程序: StringBuffer buff1 = new StringBuffer(""); buff1.append("some value A"); buff
我正在编写一些代码来对不同类型的啤酒进行一些计算。我有一个使用 GUI 的主类,并有一个 JTextArea 来打印输出。在主类中调用追加工作得很好,但是当我尝试从外部类调用追加来写入文本区域时...
我有一个像这样的 jquery block 。渲染 html 后,我看到 标签立即打开和关闭,同样的方式,立即打开和关闭。我在他的代码中做错了什么吗?有更好的方法来实现这个吗? 谢谢 $.each(f
我在尝试克隆父 div 然后将其直接附加到其自身下方时遇到一个问题。只要最后一个节点是,我的函数就可以正常工作如此选择: A B C 将导致 A A.1
我正在尝试在现有 td 末尾附加一个 td。下面是以下代码(我在 jqgrid 中执行)。 $("#list_toppager_center tr:first td:eq(7)").append("C
我正在尝试在 jQuery 中的以下追加方法上设置超时。我尝试过的所有操作都不断返回Uncaught SyntaxError:意外的标识符 这是我的代码: setTimeout("$('#us
我想用 c 打开一个文件,然后向其中添加一些内容并关闭它。我只是想知道 fopen 中的 a+ 自动导航到文件的最后一个字符。 最佳答案 是的。 为什么不尝试一下,或者阅读一下手册呢? 这里是:
在我的代码中,我有一个输入字段,它是一个循环的值。 用户在第一个字段中输入所需的值。 用户单击按钮/徽章(单击我添加项目符号)以附加到模式。 根据字段中的输入值显示带有项目符号数的模态框。 例如,如果
是否可以使用 QUrlQuery 在不对 url 进行 strip 化的情况下 append 数据? 使用下面的代码将删除“?”之后的所有内容和结果是: https://foobar.com/Info
好吧,我正在为 iPhone 制作一个简单的聊天应用程序,我很幸运,它运行良好并且看起来很棒但是我有一些问题,一个这样的问题是我向用户显示富文本的方式.. 目前我有一个荒谬的系统,它是这样工作的 {发
在 C# 中格式化我做的字符串: string a = String.Format("/blah/blah/{0}_{1}/blah.html", int1, int2) 在Python中,它会自动将
我有一个 300 万行的 .txt 文件。该文件包含如下所示的数据: # RSYNC: 0 1 1 0 512 0 #$SOA 5m localhost. hostmaster.localhost.
我有一个问题。可以删除使用 javascript 附加添加的元素? 当我尝试删除添加的跨度时,什么也没有发生。 像这样: $(document).ready(function(){ $('#
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
到目前为止这是我的代码,我想做的是说用户输入 1 2 3 然后按 -1,他或她将被要求输入另一组数字,比如 9 8 7,我的程序是什么假设要做的是将它们显示为 1 2 3 9 8 7,而是像这样显示它
我是一名优秀的程序员,十分优秀!