猪骰子游戏人机交互Java程序

猪骰子游戏人机交互Java程序,java,Java,我已经为这个项目工作了大约一周了,我想我已经准备好了。当我在游戏开始时问用户(人机对抗)时,每次我运行程序时,它都会再次问我的名字。以下是我迄今为止所做的: import java.util.Scanner; public class Assignment { Scanner usersName; Boolean humanTurn = true; Boolean computerTurn = true; int dice; int humanTur

我已经为这个项目工作了大约一周了,我想我已经准备好了。当我在游戏开始时问用户(人机对抗)时,每次我运行程序时,它都会再次问我的名字。以下是我迄今为止所做的:

 import java.util.Scanner;  

 public class Assignment  
 {  
 Scanner usersName;  
 Boolean humanTurn = true;  
 Boolean computerTurn = true;  
 int dice;  
 int humanTurnPoints, computerTurnPoints;  
 int humanTotalPoints = 0;
 int computerTotalPoints = 0;
 private Scanner keyboard;
 private Scanner key;   

 //System.out.print("Please enter your name: ");
 //usersName = new Scanner(System.in);  
 //setStart(usersName.nextLine());

 public void roll()  
     {  
     dice = (int)(Math.random()*6) + 1;                   
     }      

 public int humanTurnScore()  
     {  
         {  
             humanTurnPoints = dice + humanTurnPoints;  
             System.out.println("You threw: " + dice);
             System.out.println("You have scored: " + humanTurnPoints + " in your turn.");  
         } return humanTurnPoints;  
     } 
 public void humanTurnZero()
 {
    humanTurnPoints = 0;
 } 

 public int computerTurnScore()  
     {  
         {  
             computerTurnPoints = dice + computerTurnPoints;  
             System.out.println("Computer has scored: " + computerTurnPoints + " in its turn.");  
         } return computerTurnPoints;  
     }     
  public void computerTurnZero()
     {
        computerTurnPoints = 0;
     } 

 public Assignment()  
 {  
     humanGame();  
     if(!humanTurn)  
     {  
         computerTurn();  
     }  
 }  
 public int humanGame()
    { 

     System.out.println("To start the game please press 'r'.");  
     key = new Scanner(System.in);  
     String start = key.nextLine();  
     if(!start.equalsIgnoreCase("R")) 
     {
            System.out.println("Make sure you are pressing 'r'.");
            humanGame();
     }

     if(start.equalsIgnoreCase("R"))
        {                          
                     System.out.println("You pressed 'r'.");  
                     System.out.println("Lets start.");           


         do{  
             roll();    


             if(dice == 1)  
             {  
                 System.out.println("You got 1 and you lost your turn.");
                 System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
                 humanTurnZero();            
                 computerTurn();  

             }  
             else if(dice != 1) 
             {

                 humanTotalPoints += dice;  
                    if(humanTotalPoints >= 100)    
                    {  
                        System.out.println("You threw: " + dice);
                        System.out.println("Your GRAND TOTAL score is: " + humanTotalPoints);
                        System.out.println("Congratulations, you win!");  
                        System.exit(0);  
                    } 
                 humanTurnScore();  
                 System.out.println("Your GRAND TOTAL score is: " + humanTotalPoints);  
                 System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
                 System.out.println("You can hold or roll again.");  
                 System.out.println("To roll again press 'r' or 'h' to hold.");  
                 keyboard = new Scanner(System.in);  
                 String choice = keyboard.nextLine();  

                 if(choice.equalsIgnoreCase("R"))                                  
                     {  
                       System.out.println("You pressed 'r'.");  
                       System.out.println("Lets roll again.");   
                       roll();           

                             if(!choice.equalsIgnoreCase("R"))
                                {
    System.out.println("You didn't press 'r'. To make sure the program is running correctly please press 'r' to roll or 'h' to hold.");
                                    humanGame();
                                }
                    }

                 if(choice.equalsIgnoreCase("h"))  
                     {                         
                     System.out.println("You pressed 'h' and loose your turn.");
                     System.out.println("Your Grand total is: " + humanTotalPoints);
                     humanTurnZero();  
                     computerTurn();  
                     }     

                }  

        }while(humanTurn);     

     }return dice;  
 }  
  public int computerTurn()  
  {  
 System.out.println("Now it's computer turn.");  

 do {  
     roll();  

     if(dice != 1)  
     {  
         computerTotalPoints += dice; 
            if(computerTotalPoints >=100)  
                {  
                    System.out.println("Computer threw: " + dice);
                    System.out.println("Computer's GRAND TOTAL score is: " +  computerTotalPoints);
                    System.out.println("Game Over! the computer wins");  
                    System.exit(0);  
                }   
         System.out.println("Computer threw: " + dice);
         System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
         System.out.println("Your Grand total is: " + humanTotalPoints);
         computerTurnScore();  
         roll();  
     }  

     if(dice == 1)   
     {  
         System.out.println("Computer thrown 1 therefore it's your turn now.");  
         computerTurnZero();  
         humanGame();  
     }  

     if(computerTurnPoints >= 20)  
     {  
         System.out.println("Computer scored already " + computerTurnPoints + " you'd  better start to focus.");  
         System.out.println("Please play again");  
         humanGame();  
     }   

 }while (computerTurn);  
 return dice;  

 }     


 public static void main(String[] args)  
 {  
 new Assignment();  
 }  
 }  
我在程序开始时问用户他们的名字,我把它注释掉了(接近程序顶部)。实际上我需要在所有的System.out.println中输入,其中显示“You”,我需要它显示usersName

有人能帮我做这个项目吗。我知道这里有人很好心帮我


提前谢谢你。

当然它每次都会问你这个问题
您需要将其保存在文件中,然后从中读取

    //Write
    PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
    writer.println(usersName.nextLine());
    writer.close();

    //Read
    BufferedReader br = new BufferedReader(new FileReader("the-file-name.txt"));
    try {
        while (line != null) {
            usersName = line;
            line = br.readLine();
        }
    } finally {
    br.close();
    }

请自己添加逻辑,以确定该文件中是否有某个内容。

好的,我认为您可能对变量的生命周期感到困惑。在java中,变量如
stringusername只存在于它们自己的作用域中

如果在方法中定义变量,则当程序存在于方法中时,将忽略该变量

如果您将它定义为类内的字段(您正在做的事情),那么它只会在类的实例存在时存在。一旦程序忘记了类的实例,它也会忘记附加到该实例的任何内容,包括它的变量

一旦程序关闭,计算机自然会忘记程序持有的所有对象,因此
用户名
不再存在


如果您想让程序在关机时记住一些值,则必须将该值存储在文件、数据库或其他任何地方。我建议使用一个文件,为它创建一个数据库,这将是过分的。我会让你参考Roberto一小时前的答案,以获取更多关于如何实现这一目标的信息。

将你的程序提炼为能够描述问题的最小示例,然后重新提交-你发布的99.9%的逻辑与问题无关你是否认为关闭程序时变量将其值保存在程序中/重新启动?我想在程序开始时问用户他们的名字是什么。这就是我的问题所在。我不能减少到最小的代码,因为我在编程方面相对较新,我正试图让它工作。无论如何,谢谢你。还有其他人可以帮我做这个程序吗?罗伯托,我不知道为什么我需要一个额外的文件?我对编程比较陌生,我以前没见过。你到底想做什么。请慢慢解释。