所以我的程序正常工作,但唯一的问题是当信息保存到.txt文件时,没有空格,例如文本文件说“paul214”时应该说“paul 2 1 4”,并且有人知道每次新玩家玩游戏时都要停止写文件吗?
import java.util.Random;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
public class GuessingGame3 {
public static void main(String[] args)
{
Random generator = new Random(); //This is were the computer selects the Target
int guess;
int count = 0;
int Target;
String userName;
String playagain;
boolean play = true;
int session = 0;
int sessions = 0;
int lowestScore = 6;
Scanner consoleIn = new Scanner(System.in);
Scanner name = new Scanner(System.in);
System.out.println("Hello! Please enter your name:\n");
userName= name.nextLine();//This is were the user enters his/her name
System.out.println("Hello "+ userName + " :) Welcome to the game!\n");
while (play == true) //This is where the game starts up
{
session++; //This counts the number of goes the player has
Target = generator.nextInt(100) + 1;
System.out.println("Can you guess the number i'm thinking off? You will have 6 attempts to guess the correct number"); //This is where the computer asks the user to guess the number and how many guesses they will have
do {
guess = consoleIn.nextInt();
count++;
if (guess > Target)
System.out.println("Sorry! Your guess was too high! Guess again :)"); //This is to help the player get to the answer
else
if (guess < Target)
System.out.println("Sorry! Your guess was too low! Guess again :)"); //This is to help the player get to the answer
}
while(guess != Target && count <6);
if(guess == Target) {
System.out.println("Congratulations "+ userName + ", it took you "+ count +" attempts to guess correctly!"); //This tells the player that they got the correct answer and how many attempts it took
sessions++;
}
else
{
System.out.println("Sorry "+ userName + ", You've used up all of your guesses! The correct answer was "+ Target + "!"); //This tells the player that they failed to find the number and then tells them what the correct answer
}
while ( count < lowestScore)
{
lowestScore = count;
}
{
Scanner answer = new Scanner(System.in);
System.out.println("Would you like to play again "+ userName +"? [Y/N]");//This asks the player if they would like to play again
playagain = answer.nextLine();
if(playagain.equalsIgnoreCase("Y"))//This is what happens if the player opts to play again
{
play = true;
count = 0;
} else if(playagain.equalsIgnoreCase("N"))//This is what happens if the player opts to exit the game
{
play = false;
System.out.println("Thanks for playing "+ userName +"! :) Please come back soon!");
try {//This is where the program writes the information about the player to a .txt file
BufferedWriter writer =
new BufferedWriter ( new FileWriter(".\\Records.txt"));
writer.write(userName);
writer.write(String.valueOf(session));
writer.write(String.valueOf(sessions));
writer.write(String.valueOf(lowestScore));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
//This displays the information saved to the .txt file to the player.
System.out.println("The number of goes you had: "+ session +"");
System.out.println("The number of times you guessed correctly: "+ sessions +"");
System.out.println("Your lowest score was: "+ lowestScore +"!");
break;
}
}
}
}
}
答案 0 :(得分:2)
如果您希望它们在那里,您需要显式添加空格:
writer.write(userName);
writer.write(" ");
writer.write(String.valueOf(session));
writer.write(" ");
writer.write(String.valueOf(sessions));
writer.write(" ");
writer.write(String.valueOf(lowestScore));
至于你的第二个问题,你可以像这样附加到文件中:
BufferedWriter writer = new BufferedWriter ( new FileWriter(".\\Records.txt", true));
writer.append("your content");
第二个参数true
将FileWriter
设置为追加模式。
答案 1 :(得分:2)
如果需要空格,则需要在文本文件中写入空格。示例:
writer.write(userName + " ");
如果您不希望覆盖文本文件,请在实例化FileWriter对象时将append设置为true:
new FileWriter(".\\Records.txt", true)
答案 2 :(得分:0)
您可以使用\t
在文件中创建标签。
writer.write(userName+"\t");
................
output = new BufferedWriter(new FileWriter(my_file_name, true));//you have to open the file in append mode
答案 3 :(得分:0)
有没有人知道每次新的时候都要停止写文件 玩家玩游戏?
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
// Gallery
case Keys.PICTURE_REQ_CODE:
Uri fileUri = data.getData();
cropIntent = new Intent(this,CropActivity.class);
cropIntent.setData(fileUri);
startActivityForResult(cropIntent,Keys.CROP_REQ_CODE);
break;
}
}
}
答案 4 :(得分:0)
没有空格的原因是因为您没有指定添加它们。变量最后没有空格,因此它们不会这样写。这样做:
try {//This is where the program writes the information about the player to a .txt file
BufferedWriter writer = new BufferedWriter ( new FileWriter(".\\Records.txt"));
writer.write(userName + " ");
writer.write(String.valueOf(session) + " ");
writer.write(String.valueOf(sessions) + " ");
writer.write(String.valueOf(lowestScore));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
或者,您可以使用字符串连接来编写它:
try {//This is where the program writes the information about the player to a .txt file
BufferedWriter writer = new BufferedWriter ( new FileWriter(".\\Records.txt"));
writer.write(userName + " " + String.valueOf(session) + " " + String.valueOf(sessions) + " " + String.valueOf(lowestScore));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}