石头剪刀布蜥蜴人斯波克游戏无法运行

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;
                    }
            }
        }

    }

{btsdaf} - Andreas
1
{btsdaf} - Mateusz
{btsdaf} - luk2302
2个回答

2

经过一些修改后,代码可以正常运行: 1)不需要多次使用switch(x)。 2)x和y是整数,因此case语句应该像case 1而不是case '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;
                    }

        }

    }
}

1

您正在使用字符,而不是数字。

当您这样做时

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

这与这个不同,类似于int x = 1; // 整数值为1

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

char 可以自动转换为 int,这就是这里发生的事情。在一个类型上进行 switch,在另一个类型上进行 case,似乎会很方便,但这就是生活。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接