Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 文本文件未显示在文档中_Java - Fatal编程技术网

Java 文本文件未显示在文档中

Java 文本文件未显示在文档中,java,Java,我在文本文件中显示游戏分数时遇到问题,我有两个文本文件,一个用于显示玩家详细信息,效果非常好,但另一个用于显示分数的文本文件没有出现,在我输入分数后,分数文本文件中修改日期的时间与我完成游戏的时间相同,只是分数没有出现。这是我的密码: import java.util.Scanner; import java.io.*; public class Project { public static void main (String[]args) throws FileNotFoundE

我在文本文件中显示游戏分数时遇到问题,我有两个文本文件,一个用于显示玩家详细信息,效果非常好,但另一个用于显示分数的文本文件没有出现,在我输入分数后,分数文本文件中修改日期的时间与我完成游戏的时间相同,只是分数没有出现。这是我的密码:

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
import java.util.Scanner;
导入java.io.*;
公共类项目
{
publicstaticvoidmain(String[]args)抛出FileNotFoundException//这表明程序可能遇到不存在的文件。
{
字符串outputFileName=“players.txt”;
//存储文本文件的名称
PrintWriter writeToFile=新的PrintWriter(outputFileName);
//这允许我写入名为outputFileName的文本文件
Scanner playerStringInput=新扫描仪(System.in);
Scanner playerIntInput=新扫描仪(System.in);
//从键盘获取信息,一个用于整数,另一个用于字符串
字符串名称=”;
字符串昵称=”;
字符串=”;
int年龄=0;
int-previousGamesPlayed=0;
//声明用于存储程序输入的变量
int amtOfPlayers=2;//我们拥有的玩家数量
for(int i=0;i