2016-12-05 7 views
0

返信には何を書いて、勝ったのか、結んだのか、紛失したのかを返すものですか?私はすべてを考えようとしましたが、私はそれを理解できないようです。私は何を返すために置くのですか?

編集: ここに、すべてのコードが必要です。

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=CChoice; 
    this.PChoice=PChoice; 
} 

public void setPlayer(int p) 
{ 
    CChoice = 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; 
     } 
    } 
    } 
    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; 

} 

} 
+0

は(条件)場合は、「」=この 'retrunStringようにしてみてくださいelseチェーンかなり - また、あなたはあなたのifを減らすことができます – emotionlessbananas

答えて

0

このコードを試してください。

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) { 
     CChoice = 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) { 
      this.PChoice = 1; 
      PChoice = "Rock"; 
     } else if (PPChoice == 2) { 
      this.PChoice = 2; 
      PChoice = "Paper"; 
     } else if (PPChoice == 3) { 
      this.PChoice = 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; 
     System.out.println(CCChoice); 
     String CChoice = null; 
     if (CCChoice == 1) { 
      this.CChoice = 1; 
      CChoice = "Rock"; 
     } else if (CCChoice == 2) { 
      this.CChoice = 2; 
      CChoice = "Paper"; 
     } else if (CCChoice == 3) { 
      this.CChoice = 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 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()); 
    } 
} 

コードについて少し変更があります。

+0

私はそれを試しましたが、私がメインでテストしたときに、あなたが勝った代わりにヌルを返すか、またはあなたがectを失います –

+0

(返り値: "someVal")else if(条件){returnString = "他の値"} return returnString "上記のコードで 'if'文をチェックしようとすると、nullが出力されません。私は' '' 'のように空文字列を出力します。 tempStrを '' ''にして ''ヌル ''にしないでください。 – msagala25

+0

私はあなたのやり方を試してみましたが、何もプリントアウトしませんでした –

0

「???」あなたのswitch文がどれも満たされない場合のデフォルトの場合です。この場所で私はあなたの声明がこの点までに決定に達しているはずであるので、例外をスローすることを提案します。

あなたのprintlnステートメントは、出力値が返される場所です。このコードはちょっと見えるかもしれませんが、おそらくあなたは自分の正気のためにそれを少し再編成することを検討するかもしれません! 幸運!

0

Iは、pおよびc選択肢の様々な組み合わせに対する結果を含む2次元配列を作成することを好むだろう。

public class yourClass { 
    private static final int[][] results = new int[3][3]; 
    results[0][0] = 0; 
    results[0][1] = -1; 
    results[0][2] = 1; 
    results[1][0] = 1; 
    results[1][1] = 0; 
    results[1][2] = -1; 
    results[2][0] = -1; 
    results[2][1] = 1; 
    results[2][2] = 0; 

    public String determineWinner(int pChoice, int cChoice) { 
     int result = results[pChoice-1][cChoice-1]; 
     String label = ""; 

     switch (result) { 
      case -1: 
       label = "You Lose"; 
       break; 
      case 0: 
       label = "You Have Tied"; 
       break; 
      case 1: 
       label = "You Win"; 
       break; 
      default: 
       label = "Move cannot be determined"; 
       break; 
     } 

     return label; 
    } 
} 
0

Enumは、このような場合には、良好な最適解です。すべてがそのような結果を決定するために使用することができ

enum Outcome { 
    WIN("You won"), 
    LOST("You lost"), 
    TIE("You tied"); 

    private String msg; 

    private Outcome(String msg) { 
     this.msg = msg; 
    } 

    public String getMsg() { 
     return msg; 
    } 
} 

public Outcome determineWinner(int pChoice, int cChoice) { 
    Outcome outcome = null; 
    int diff = PChoice - CChoice; 
    switch (diff) { 
     case -1: 
     case 2: 
      outcome = Outcome.LOST; 
      break; 
     case 1: 
     case -2: 
      outcome = Outcome.WIN; 
      break; 
     case 0: 
      outcome = Outcome.TIE; 
      break; 
    } 
    return outcome; 

} 

... 
Outcome outcome = determineWinner(pChoice, cChoice); 
System.out.println(outcome.getMsg()); 
... 
関連する問題