2014-11-02 21 views
0

我遇到了這個程序的問題。我認爲問題出在我的回覆聲明上。我可以讓程序運行起來,直到確定勝利者然後崩潰。我只是一個初學者,任何幫助將不勝感激。我對這個程序有問題

這裏是我的RPS類

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package game; 

import java.util.Random; 
import java.util.Scanner; 

/** 
* 
* @author Owner 
*/ 
public class rPs { 
    Scanner in = new Scanner(System.in); 
    Random random = new Random(); 
    String userChoice = ""; 
    String cpuChoice; 
    String pickWinner; 
    String winner; 
    String tie; 
    String user; 
    String computer; 
    int compRand = 0; 

    //Get user choice 
    public String getUserChoice(){ 
    System.out.println("Rock, Paper, or Scissors?:"); 
    userChoice = in.next(); 

    //Get user choice 
    while(!"rock".equals(userChoice)&& !"paper".equals(userChoice) && 
     !userChoice.equals("scissors")) 
     { 
      System.out.println("Sorry, " + userChoice + " is not a valid entry"); 
      System.out.println("Rock, Paper, or Scissors?:"); 
      userChoice = in.next(); 
     }//end while for r,p,s check 
     return userChoice; 
    }//End Get user 

    //Get the cpu Choice!!!! 
    public String getCPUChoice(){ 


     compRand = random.nextInt(3); 

     if (compRand == 1) { 
      cpuChoice = "Rock"; 
     } 
     if (compRand == 2){ 
      cpuChoice = "Paper"; 
     } 
     else if (compRand == 3){ 
      cpuChoice = "Scissors"; 
     } 
     return cpuChoice; 
    }//End Get cpu choice 

    //Pick a winner by passing 2 strings together 
    public String pickWinner(String userChoice,String cpuChoice){ 

     //need a loop for ties 
     if (userChoice.equalsIgnoreCase(cpuChoice)) { 
       return winner = tie; 

     }//end tie if... 

     if (userChoice.equalsIgnoreCase("Rock")&& cpuChoice.equalsIgnoreCase 
       ("scissors")) 
     { 
      System.out.println("Computer chooses Scissors. You win!"); 
       return winner = user; 
     }//end if 1  
      if (userChoice.equalsIgnoreCase("Rock")&&cpuChoice.equalsIgnoreCase("Paper")){ 

       return winner = computer;  
     }//end if 2 


      if (userChoice.equalsIgnoreCase("Paper") && cpuChoice.equals 
       ("Scissors")) { 
       System.out.println("Computer chooses Scissors. You lose."); 
       return winner = computer; 
     }//end if 3 
      if (userChoice.equalsIgnoreCase("Paper") &&cpuChoice.equalsIgnoreCase 
       ("Rock")){ 
       System.out.println("Computer chooses Rock. You win!"); 
       return winner = user; 
     }//edn if 4 


      if(userChoice.equalsIgnoreCase("Scissors")&& 
       cpuChoice.equalsIgnoreCase("Paper")) { 
       System.out.println("Computer chooses Scissors. You win!"); 
       return winner = user; 
     }//end if 5 
      if (userChoice.equalsIgnoreCase("Scissors")&& 
       cpuChoice.equalsIgnoreCase("Rock")){ 
       System.out.println("Computer chooses Rock. You lose!"); 
       return winner = user; 
     }//end if 6 

     return winner; 


    }//END pick winner 

}//End rPs Class 

********************************** ************************************ 8 這是由講師創建的主要課程。

package game; 

import java.util.*; 

public class Game 
{ 

    public static void main (String[] args) 
    { 
     Scanner in = new Scanner (System.in); 
     rPs rps = new rPs(); //***Your class 

     int numGames = 0; 
     String userChoice = ""; 
     String cpuChoice = ""; 
     String winner = ""; 
     int userWins = 0; 
     int cpuWins = 0; 


     System.out.println("Welcome to Rock, Paper, Scissors!\n"); 

     //Get odd number of games 
     System.out.println("How many rounds would you like to play?"); 
     numGames = in.nextInt(); 

     while (numGames % 2 == 0) //Even number 
     { 
      System.out.println("Sorry, number of games must be odd. Please try again:"); 
      numGames = in.nextInt(); 
     } 

     //Flush the buffer 
     in.nextLine(); 

     //Play the game for the number of rounds the user entered 
     for (int i = 1; i <= numGames; i++) 
     { 
      //Get the user and computer choices 
      userChoice = rps.getUserChoice(); //***Your method 
      cpuChoice = rps.getCPUChoice(); //***Your method 

      System.out.println("Computer chooses " + cpuChoice); 

      //Pick winner 
      winner = rps.pickWinner(userChoice, cpuChoice); //***Your method 

      if (winner.equalsIgnoreCase("Tie")) 
      { 
       System.out.println("It's a tie! Play again."); 
       numGames++; 
      } 
      else 
      { 
       if (winner.equalsIgnoreCase("User")) 
       { 
        userWins++; 
       } 
       else if (winner.equalsIgnoreCase("Computer")) 
       { 
        cpuWins++; 
       } 
       else 
       { 
        System.out.println("Error in picking winner"); 
       } 

       System.out.println(winner + " wins!"); 
      } 

     } //end for 

     //Print results 
     System.out.println("\nUser wins: " + userWins); 
     System.out.println("Computer wins: " + cpuWins); 

     if (userWins > cpuWins) 
     { 
      System.out.println("\nThe user won!"); 
     } 
     if (cpuWins > userWins) 
     { 
      System.out.println("The computer won!"); 
     } 

     //Close game 
     System.out.println("\nThank you for playing!"); 

    } //end main 

} //end class 
+0

如果您發生車禍,如果您引用例外情況,診斷起來會更容易 – 2014-11-02 21:18:37

回答

1

根據你的main方法需要,你pickWinner方法返回值應該是「用戶」,「計算機」或「鐵」。

變化,如return winner = user;

return "User"; 

任何聲明目前您的收益報表分配持有空引用另一個變量拿着一個空引用變量,因此它們等同於return null;

因此,您的主要方法中的語句如winner.equalsIgnoreCase("Tie")會導致NullPointerException,因爲winner爲空。

+0

非常感謝Eran。我感覺好多了,因爲我知道我只是有點錯了。 – 2014-11-02 21:23:11