我在文本文件中显示我的游戏分数时遇到问题,我有两个文本文件,一个用于播放器详细信息,但是在我输入分数后,其他文本文件没有出现在得分文本文件中修改的日期与我完成游戏的时间相同,只是分数没有出现。这是我的代码:
import java.util.Scanner;
import java.io.*;
public class Project
{
public static void main (String[]args) throws FileNotFoundException // This shows that the programme might encounter a file which is not there.
{
String outputFileName ="players.txt";
//Storing the name of the text file
PrintWriter writeToFile = new PrintWriter(outputFileName);
//This allows me to write to the text file that I've called outputFileName
Scanner playerStringInput = new Scanner(System.in);
Scanner playerIntInput = new Scanner(System.in);
//Taking info from the keyboard, one is for Integers and the other is for String
String name = " ";
String nickName = " ";
String homeTown = " ";
int age = 0;
int previousGamesPlayed = 0;
//Declaring the variables that I'm going to use to store the inputs from the programme
int amtOfPlayers = 2; //The amount of players we have
for(int i = 0; i < amtOfPlayers; i++) //Loop for both players
{
System.out.println("1.Please Enter Name");//Prints out instructitions for user.
name = playerStringInput.nextLine();//Taking the input to store in name.
writeToFile.println(name); //Outputs the data into the text file.
System.out.println("2.Please Enter Nickname");//Printing out the nickname entered onto the screen.
nickName = playerStringInput.nextLine(); //Taking the input to store in name.
writeToFile.println(nickName); //Outputs the data into the text file.
System.out.println("3.Please Enter Home Towm");//Printing out the hometown entered onto the screen.
homeTown = playerStringInput.nextLine();//Taking the input to store in name.
writeToFile.println(homeTown);//Outputs the data into the text file.
System.out.println("4.Please Enter Age, this must be a number.");//Printing out the age entered onto the screen.
age = playerIntInput.nextInt();//Taking the input to store in name.
writeToFile.println(age);//Outputs the data into the text file.
System.out.println("5.Please Enter Previous Games Played");//Printing out the previous games played entered onto the screen.
previousGamesPlayed = playerIntInput.nextInt();//Taking the input to store in name.
writeToFile.println(previousGamesPlayed);//Outputs the data into the text file.
System.out.println("The name entered is: " + name); //Printing out the name entered onto the screen.
System.out.println("The nickname entered is: " + nickName);//Printing out the nickname entered onto the screen.
System.out.println("The hometown entered is: " + homeTown);//Printing out the hometown entered onto the screen.
System.out.println("Age entered is: " + age);//Printing out the age entered onto the screen.
System.out.println(previousGamesPlayed);//Printing out the name entered onto the screen.
}
writeToFile.close(); //Close the file writer so it can write the text into the file
Scanner scan = new Scanner(System.in);//Allows us to take input from game
String userInput1 = " ";
char player1Input;
String userInput2 = " ";
char player2Input;
//Declaring our variables for game
for(int i = 0; i < 7; i++) //Loop for game which will loop 7 times
{
System.out.println("Player 1: Please Enter e.g R for Rock:");
System.out.println("R.Rock");//Printing out Rock as choice for game
System.out.println("P.Paper");//Printing out Paper as choice for game
System.out.println("S.Scissors");//Printing out Scissors as choice for game
userInput1 = scan.next();
player1Input = userInput1.charAt(0);
System.out.println("Player 2: Please Enter e.g S for Scissors");
System.out.println("R.Rock");//Printing out Rock as choice for game
System.out.println("P.Paper");//Printing out Paper as choice for game
System.out.println("S.Scissors");//Printing out Scissors as choice for game
userInput2 = scan.next();
player2Input = userInput2.charAt(0);
switch(player1Input)//beginning of switch statement
{
case 'R': System.out.println ("Player 1:Rock");//
switch(player2Input)
{
//declaring possible outcomes if player 1 choses paper
case'R' : System.out.println("Player 2:Rock");//
System.out.println("The Game Result is Draw");
break;
case'P' :System.out.println("Player 2:Paper");
System.out.println("The Game Result is: Player 2 Wins");
break;
case'S' :System.out.println("Player 2:Scissors");
System.out.println("The Game Result is: Player 1 Wins");
break;
default: System.out.println("Invalid Option");
break;
}
break;//Terminating the loop before it goes onto the next statement
case 'P' : System.out.println ("Player 1:Paper");
switch(player2Input)
{
//declaring possible outcomes if player 1 choses Paper
case'R' : System.out.println("Player 2:Rock");
System.out.println("The game result is: Player 1 Wins");
break;
case'P' :System.out.println("Player 2:Paper");
System.out.println("The game result is: Draw");
break;
case'S' :System.out.println("Player 2:Scissors");
System.out.println("The Game Result is: Player 2 Wins2");
break;
default: System.out.println("Invalid Option");
break;
}
break;//Terminating the loop before it goes onto the next statement
//declaring possible outcomes if player 1 choses Scissors
case 'S' : System.out.println ("Player 1:Scissors");
switch(player2Input)
{
case'R' : System.out.println("Player 2:Rock");
System.out.println("The Game Result is: Player 2 Wins");
break;
case'P' :System.out.println("Player 2:Paper");
System.out.println("The game result is: Player 1 Wins");
break;
case'S' :System.out.println("Player 2:Scissors");
System.out.println ("The Game Result is: Draw");
break;
default: System.out.println("Invalid Option");
break;
}
break;//Terminating the loop before it goes onto the next statement
}//Ending of loop for game
for(int h = 0; i < 2;i++);//for loop for scores, I also needed to call this int h as I already declared int I as a variable in my main method string.
}
int [] arrPlayer = new int[2];//using arrays to gather players score
System.out.println("Player 1 Please Enter Your Score");//Player 1 printing out their score
arrPlayer[0]=scan.nextInt();
System.out.println("Player 2 Please Enter Your Score");//Player 2 printing out their score
arrPlayer[1]=scan.nextInt();
System.out.println("Player 1 : Score: " + arrPlayer[0]);//Player 1 score
System.out.println("Player 2 : Score: " + arrPlayer[1]);//Player 2 score
String outputFileName2 ="scores.txt";
//Storing the name of the text file
PrintWriter writeToFile2 = new PrintWriter(outputFileName2);
//This allows me to write to the text file that I've called outputFileName
writeToFile.close(); //Close the file writer so it can write the text into the file
}//End of method
}//End of class
答案 0 :(得分:2)
您的上一句话应该是writeToFile.close()
,而不是from unittest.mock import patch
from contextlib import contextmanager
@contextmanager
def use_mocked(method, cls, ret_value):
class MockedClass(cls):
pass
def func(cls):
return ret_value
def fullname(o):
return o.__module__ + "." + o.__name__
setattr(MockedClass, method, classmethod(func))
with patch(fullname(cls), MockedClass):
yield
。你正在关闭第一个文件两次。在程序结束之前,缓冲区没有被刷新到文件中。