gpt4 book ai didi

java - Rock Paper Scissor Lizard Spock 游戏无法运行

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:34:05 25 4
gpt4 key购买 nike

因此,当我运行它时,我开始输入值,但在输入值后它不会执行 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;
}
}
}

}

最佳答案

一些修正后的作品:-1)你不需要多次使用 switch(x) 。2) x 和 y 是 int 所以 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;
}

}

}
}

关于java - Rock Paper Scissor Lizard Spock 游戏无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46897360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com