2015-10-17 80 views
-2

所以我做了一個遊戲rok paper scissors的小程序,當我玩電腦時說idk aka返回值並且不管它總是說什麼idk/null:比如如果我在 「石頭」 進入它說 「你拿起石頭 CPU選擇IDK的 你贏 BUILD SUCCESSFUL(總時間:2秒)」rock paper sicssors遊戲計算機說null

package rpc; 

import static java.lang.System.console; 
import java.util.*; 


public class Rpc { 


    public static void main(String[] args) { 
     Scanner scan = new Scanner(System. in); 

     String choice = scan.nextLine(); 

     if (isValid(choice)) { 
      int aIChoice = (int)(Math.random() + 3); 
      int personChoice = getVal(choice); 
      //0=rock 1=paper 2=scissors 
      System.out.println("you picked " + choice); 
      System.out.println("cpu choose " + getAIChoice(aIChoice)); 
      System.out.println("you " + didPersonWin(personChoice, aIChoice)); 
     } else { 
      System.out.println("That is not a valid selection"); 
     } 
    } 
    public static String getAIChoice(int x) { 
     if (x == 0) { 
      return "rock"; 

     } 
     if (x == 1) { 
      return "paper"; 

     } 
     if (x == 2) { 
      return "sicssors"; 

     } 
     return "idk"; 
    } 

    public static String didPersonWin(int pChoice, int ComputerChoice) { 
     if (pChoice == 0) { 
      if (ComputerChoice != 1) { 
       if (ComputerChoice != 0) { 
        return "win"; 
       } 
       return "tie"; 
      } 
      return "loose"; 
     } 
     if (pChoice == 1) { 
      if (ComputerChoice != 2) { 
       if (ComputerChoice != 1) { 
        return "win"; 
       } 
       return "tie"; 
      } 
      return "loose"; 
     } 
     if (pChoice == 2) { 
      if (ComputerChoice != 0) { 
       if (ComputerChoice != 2) { 
        return "win"; 
       } 
       return "tie"; 
      } 
      return "loose"; 
     } 
     return "idk"; 
    } 

    public static boolean isValid(String string) { 
     if (string.equalsIgnoreCase("rock")) { 
      return true; 
     } 

     if (string.equalsIgnoreCase("paper")) { 
      return true; 
     } 

     if (string.equalsIgnoreCase("scissors")) { 
      return true; 
     } 
     return false; 

    } 
    public static int getVal(String string) { 
     if (string.equalsIgnoreCase("rock")) { 
      return 0; 
     } 
     if (string.equalsIgnoreCase("rock")) { 
      return 1; 
     } 
     if (string.equalsIgnoreCase("rock")) { 
      return 2; 
     } 
     return 0; 
    } 
} 
+0

您還擁有所有的語句來檢查'rock'。 – chrylis

+0

建議你看看[Math.random]的Javadoc(http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#random%28%29)。也看看你正在設置'aiChoice'的行。假裝你是電腦。 Math.random()具有哪些數據類型的值?現在'Math.random()+ 3'具有哪些數據類型的值?現在什麼樣的數據類型值,如果你把它轉換爲'int',你能得到什麼? – shoover

回答

2

,就應該替換該行:

int aIChoice = (int)(Math.random()+3); 

同這一個:

int aIChoice = (int)(Math.random()*3); 

第一個將永遠在你的getAIChoice方法返回3idk