2013-03-30 27 views
1

我已經創建了一個課程,可以做Paper Rock Scissors。創建一個新課程來運行遊戲

public class RPS { 

    private char cAns; 

    public RPS() 
    { 
     reset(); 
    } 

    public String promptShoot() 
    { 
     return "Rock, Paper, Scissors, Shoot! (r/p/s)\n"; 
    } 

    public void AI() 
    { 
     double temp = Math.random(); 
     int num = (int)(temp * 2.99); 

     switch(num) 
     { 
      case 0: 
       cAns = 'r'; 
       break; 
      case 1: 
       cAns = 'p'; 
       break; 
      case 2: 
       cAns = 's'; 
       break; 
     } 
    } 

該課程隨機爲計算機選擇。然後它使用掃描儀詢問人的輸入。

public int shoot(char hAns) 
    { 
     if(hAns == cAns) 
      return 0; 
     else if((cAns == 'r' && hAns == 'p') 
       || (cAns == 'p' && hAns == 's') 
       || (cAns == 's' && hAns == 'r')) 
      return 1; 
     else 
      return -1; 
    } 

一旦雙方都選擇了他們的選擇,它宣告勝出者:

public String winner(int won) 
    { 
     if(won == 1) 
      return "The human won!!! All hail the human!!!"; 
     else if(won == -1) 
      return "The computer won!!! Humans must die!!!"; 
     else 
      return "Tie!"; 
    } 

    public void reset() 
    { 
     cAns = 'a'; 
    } 
} 

我想這樣的作品和所有,但我沒有意識到我需要創建一個單獨的類來運行這個RPS類。

我有這個迄今爲止

import java.util.Scanner; 

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

     RPS choice = new RPS(); 

     choice.AI(); 

     System.out.print(choice.promptShoot()); 
     choice.hAns(scan.nextInt()); 

     scan.close(); 
    } 
} 

我認爲這將初始化遊戲,但它不工作。任何建議或我能指出正確的方向?

回答

0

static加到winnershoot方法中。在結束在主 將這個:

System.out.println(winner(shoot(scan.nextLine().charAt(0)))); 

刪除choice.hAns(scan.nextInt());

+0

我試過了,但它仍然是行不通的 – user2227728

+0

它被編輯。現在應該工作。 –

+0

我讓我的教授幫助我參加RPS課程,他表示我不應該改變任何事情。它應該按照他給我的方式工作。我只是想創建一個運行遊戲的類。我只是困惑,因爲我新來的Java .. – user2227728