2017-10-23 17 views
3

これを実行すると、値を入力するようになりますが、値が入力された後、誰が勝利したかを示すswitch文は実行されません。私はまだすべてのケースを入れていないが、私はそれを試してみたかったし、何も起こっていない。私はyがランダムな値で割り当てられていて、それは動作していないので、私はちょうどそれを人間の入力にしました。私は最終的に人間がコンピュータをプレイしている場所にしなければなりません。ロックペーパーはさみトカゲスポークゲームが動作しない

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

public class rock{ 

    public static void main(String[] args) { 
     int x; 
     int y; 
     Random randomGenerator = new Random(); 
     Scanner input= new Scanner(System.in); 

     System.out.print("Human player: Enter 0 for rock, 1 for scissors, 2 for paper, 3 for lizard, 4 for spock:"); 
     x=input.nextInt(); 
     System.out.print("Computer player: Enter 0 for rock, 1 for scissors, 2 for paper, 3 for lizard, 4 for spock:"); 
     y = input.nextInt(); 

     switch(x) 
      { 
       case '0': 
        switch (y) 
        { 
         case '1': 
          System.out.print("Human Wins computer chose scissors!"); 
          break; 
         case '2': 
          System.out.println("Human wins computer chose paper!"); 
          break; 
         case '0': 
          System.out.println("Draw!"); 
          break; 
         case '3': 
          System.out.println("Human Wins with Lizard!"); 
          break; 
         case '4': 
          System.out.println("Computer Wins with Spock!"); 
          break; 
        } 
      } 
      switch (x) 
      { 
       case '1': 
        switch(y) 
        { 
        case '1': 
         System.out.print("Human Wins computer chose scissors!"); 
         break; 
        case '2': 
         System.out.println("Human wins computer chose paper!"); 
         break; 
        case '0': 
         System.out.println("Draw!"); 
         break; 
        case '3': 
         System.out.println("Human Wins with Lizard!"); 
         break; 
        case '4': 
         System.out.println("Computer Wins with Spock!"); 
         break; 
        } 
      } 
      switch (x) 
      { 
       case '2': 
        switch (y) 
        { 
        case '1': 
         System.out.print("Human Wins computer chose scissors!"); 
         break; 
        case '2': 
         System.out.println("Human wins computer chose paper!"); 
         break; 
        case '0': 
         System.out.println("Draw!"); 
         break; 
        case '3': 
         System.out.println("Human Wins with Lizard!"); 
         break; 
        case '4': 
         System.out.println("Computer Wins with Spock!"); 
         break; 
        } 
      } 
     } 

    } 
+0

は、なぜあなたは3 'スイッチ(X)'ステートメントを持っているのですか?あなたは1つだけ持っているべきで、5つの場合があります。 – Andreas

+1

あなたは 'int x'を宣​​言しておき、' switch'ステートメントであなたのケースは 'char'値にあります。 'case 1:'を試してください。 – Mateusz

+0

ブレークポイントを使ってアプリケーションをデバッグし、 'x'と' y'の値を調べるか、少なくとも2つの 'System.out.println(...)'ステートメントを追加して実際に到達したステートメントを確認してください。 – luk2302

答えて

1

数字ではなく文字を使用しています。

この

case '1': // uses a character, which has an ascii (thus integer) value of 49 

の操作を行い、ここで起こっているものであるintに自動キャストすることができint x = 1; // integer value of 1

case 1: // uses an integer with value of 1 

charようになる。この異なっています。 1つのタイプでswitchを入力し、もう1つのタイプでcaseを入力すると警告を発するのが便利です。

2

いくつかの修正後に動作します: - 1)スイッチ(x)を複数回使用する必要はありません。 2)xとyはそうcase文は、ケース1とない場合のようにint型でなければなりませんされている '1'

public class Rock { 

    public static void main(String[] args) { 
     int x; 
     int y; 
     Random randomGenerator = new Random(); 
     Scanner input= new Scanner(System.in); 

     System.out.print("Human player: Enter 0 for rock, 1 for scissors, 2 for paper, 3 for lizard, 4 for spock:"); 
     x=input.nextInt(); 
     System.out.print("Computer player: Enter 0 for rock, 1 for scissors, 2 for paper, 3 for lizard, 4 for spock:"); 
     y = input.nextInt(); 

     switch(x) 
      { 
       case 0: 
        switch (y) 
        { 
         case 1: 
          System.out.print("Human Wins computer chose scissors!"); 
          break; 
         case 2: 
          System.out.println("Human wins computer chose paper!"); 
          break; 
         case 0: 
          System.out.println("Draw!"); 
          break; 
         case 3: 
          System.out.println("Human Wins with Lizard!"); 
          break; 
         case 4: 
          System.out.println("Computer Wins with Spock!"); 
          break; 
        } 

      case 1: 
        switch(y) 
        { 
        case 1: 
         System.out.print("Human Wins computer chose scissors!"); 
         break; 
        case 2: 
         System.out.println("Human wins computer chose paper!"); 
         break; 
        case 0: 
         System.out.println("Draw!"); 
         break; 
        case 3: 
         System.out.println("Human Wins with Lizard!"); 
         break; 
        case 4: 
         System.out.println("Computer Wins with Spock!"); 
         break; 
        } 

      case 2: 
        switch (y) 
        { 
        case 1: 
         System.out.print("Human Wins computer chose scissors!"); 
         break; 
        case 2: 
         System.out.println("Human wins computer chose paper!"); 
         break; 
        case 0: 
         System.out.println("Draw!"); 
         break; 
        case 3: 
         System.out.println("Human Wins with Lizard!"); 
         break; 
        case 4: 
         System.out.println("Computer Wins with Spock!"); 
         break; 
        } 

     } 

    } 
} 
関連する問題