2016-12-06 60 views
0

我正在做一個rock-paper-scissors程序,並且在我的代碼中,如果向下滾動到方法determineWinner(),我已經設置了它,但在方法main()中,當我調用它的時候,它不會打印你是贏了,還是輸了,還是輸了。當我執行它時,我的程序沒有打印任何東西

這裏是我的主要方法:

public class RockPaperScissorsMain { 

    public static void main(String[] args) { 
     RockPaperScissorsClass rc = new RockPaperScissorsClass(); 
     System.out.println("Player is: " + rc.getPlayer()); 
     System.out.println("Computer is: " + rc.getComputer()); 
     System.out.println(rc.determineWinner()); 
    } 
} 

這裏是我的課。

public class RockPaperScissorsClass { 

private int wins; 
private int losses; 
private int ties; 
private int CChoice; 
private int PChoice; 

public RockPaperScissorsClass(int wins, int losses, int ties, int computerPick, int playerPick) { 
    this.wins=wins; 
    this.losses=losses; 
    this.ties=ties; 
    this.CChoice=CChoice; 
    this.PChoice=PChoice; 
} 
public String getPlayer() { 

    Scanner in = new Scanner(System.in); 
    System.out.println("Enter Choice(1=Rock, 2=Paper, 3=Scissors)-->"); 
    int PPChoice = in.nextInt(); 
    String PChoice = null; 
    if(PPChoice==1) { 
     PChoice="Rock"; 
    } 
    else if(PPChoice==2) { 
     PChoice="Paper"; 
    } 
    else if(PPChoice==3) { 
     PChoice="Scissors"; 
    } 
    else { 
     while(true) { 
     System.out.println("You have entered an invalid choice. Please try again."); 
     System.out.println("Enter Choice(1=Rock, 2=Paper, 3=Scissors)-->"); 
     PPChoice = in.nextInt(); 
     if(PPChoice==1) { 
      PChoice="Rock"; 
      break; 
     } 
     else if(PPChoice==2) { 
      PChoice="Paper"; 
      break; 
     } 
     else if(PPChoice==3) { 
      PChoice="Scissors"; 
      break; 
     } 
    } 
    return PChoice; 
} 

public String getComputer() { 
    Random rand = new Random(); 
    int CCChoice = rand.nextInt(3)+1; 
    String CChoice = null; 
    if(CCChoice==1) { 
     CChoice="Rock"; 
    } 
    else if(CCChoice==2) { 
     CChoice="Paper"; 
    } 
    else if(CCChoice==3) { 
     CChoice="Scissors"; 
    } 
    return CChoice; 
} 
public String determineWinner() { 
    String detWinner = ""; 
    if(PChoice==1 && CChoice==2) { 
     detWinner = "You Lose"; 
    } 
    else if(PChoice==1 && CChoice==3) { 
     detWinner = "You Win"; 
    } 
    else if(PChoice==2 && CChoice==3) { 
     detWinner = "You Lose"; 
    } 
    else if(PChoice==2 && CChoice==1) { 
     detWinner = "You Win"; 
    } 
    else if(PChoice==3 && CChoice==1) { 
     detWinner = "You Lose"; 
    } 
    else if(PChoice==3 && CChoice==2) { 
     detWinner = "You Win"; 
    } 
    else if(PChoice==1 && CChoice==1){ 
     detWinner = "You Have Tied"; 
    } 
    else if(PChoice==2 && CChoice==2) { 
     detWinner = "You Have Tied"; 
    } 
    else if(PChoice==3 && CChoice==3){ 
     detWinner = "You Have Tied"; 
    } 
    return detWinner; 
} 

public RockPaperScissorsClass() {this(0,0,0,0,0);} 
public void setPlayer(int p) {CChoice = p;} 

} 
+0

intermWinner()做PChoice和CChoice是指this.PCChoice和this.CChoice還是本地變量? –

+0

[可變範圍](http://www.java2s.com/Tutorial/Java/0020__Language/VariableScope.htm)。 CPU不壞。您正在引用其範圍之外的變量。 – MikeJRamsey56

+0

我編輯了你的問題 - 我刪除了不必要的行。 – xenteros

回答

0

您與局部變量

例如陰影場

int PPChoice = in.nextInt(); 

變化

PPChoice = in.nextInt(); 

編輯

其實這更糟糕的是,你與其他類型的變量陰影。局部變量是字符串。 IMO完全擺脫了絃樂,甚至沒有使用過。

嘗試一些類似的代碼

Scanner in = new Scanner(System.in); 

    PChoice = 0; 
    while (PChoice == 0) { 

     System.out.println("Enter Choice(1=Rock, 2=Paper, 3=Scissors)-->"); 
     PChoice = in.nextInt(); 
    } 
0

你應該同時設置了玩家和計算機的選擇,當你調用get方法。修正它在下面。

import java.util.Scanner; 
import java.util.Random; 
public class RockPaperScissorsClass { 

private int wins; 
private int losses; 
private int ties; 
private int CChoice; 
private int PChoice; 

public RockPaperScissorsClass() 
{ 
    this(0,0,0,0,0); 
} 

public RockPaperScissorsClass(int wins, int losses, int ties, int computerPick, int playerPick) 
{ 
    this.wins=wins; 
    this.losses=losses; 
    this.ties=ties; 
    this.CChoice=computerPick; 
    this.PChoice=playerPick; 
} 

public void setPlayer(int p) 
{ 
    PChoice = p; 
} 

public String getPlayer() 
{ 

    Scanner in = new Scanner(System.in); 
    System.out.println("Enter Choice(1=Rock, 2=Paper, 3=Scissors)-->"); 
    int PPChoice = in.nextInt(); 
    String PChoice = null; 
    if(PPChoice==1) 
    { 
     PChoice="Rock"; 
    } 
    else if(PPChoice==2) 
    { 
     PChoice="Paper"; 
    } 
    else if(PPChoice==3) 
    { 
     PChoice="Scissors"; 
    } 
    else 
    { 
     while(true) 
     { 
     System.out.println("You have entered an invalid choice. Please try again."); 
     System.out.println("Enter Choice(1=Rock, 2=Paper, 3=Scissors)-->"); 
     PPChoice = in.nextInt(); 
     if(PPChoice==1) 
     { 
      PChoice="Rock"; 
      break; 
     } 
     else if(PPChoice==2) 
     { 
      PChoice="Paper"; 
      break; 
     } 
     else if(PPChoice==3) 
     { 
      PChoice="Scissors"; 
      break; 
     } 
    } 
    } 
    this.setPlayer(PPChoice); 
    return PChoice; 
} 

public void setComputer(int c){ 
    CChoice = c; 
} 

public String getComputer() 
{ 
    Random rand = new Random(); 
    int CCChoice = rand.nextInt(3)+1; 
    String CChoice = null; 
    if(CCChoice==1) 
    { 
     CChoice="Rock"; 
    } 
    else if(CCChoice==2) 
    { 
     CChoice="Paper"; 
    } 
    else if(CCChoice==3) 
    { 
     CChoice="Scissors"; 
    } 
    this.setComputer(CCChoice); 
    return CChoice; 
} 




public String determineWinner() 
{ 
    String detWinner = ""; 
    if(PChoice==1 && CChoice==2) 
    { 
     detWinner = "You Lose"; 
    } 
    else if(PChoice==1 && CChoice==3) 
    { 
     detWinner = "You Win"; 
    } 
    else if(PChoice==2 && CChoice==3) 
    { 
     detWinner = "You Lose"; 
    } 
    else if(PChoice==2 && CChoice==1) 
    { 
     detWinner = "You Win"; 
    } 
    else if(PChoice==3 && CChoice==1) 
    { 
     detWinner = "You Lose"; 
    } 
    else if(PChoice==3 && CChoice==2) 
    { 
     detWinner = "You Win"; 
    } 
    else if(PChoice==1 && CChoice==1) 
    { 
     detWinner = "You Have Tied"; 
    } 
    else if(PChoice==2 && CChoice==2) 
    { 
     detWinner = "You Have Tied"; 
    } 
    else if(PChoice==3 && CChoice==3) 
    { 
     detWinner = "You Have Tied"; 
    } 
    return detWinner; 


} 

} 
+0

順便說一句,你的構造函數應該使用傳入的參數,而不是指向自身錯誤=>'this.CChoice = CChoice; this.PChoice = PChoice;' – Jerry

相關問題