所以,当我运行这个时,我得输入值,但在输入值后,它不会执行switch语句来告诉我谁获胜。我尚未提出所有案例,但我想测试它并没有发生任何事情。我已经分配了一个随机值,但是没有工作,所以我只是把它作为人工输入。我最终必须把它放在人类正在玩电脑的地方。
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 :(得分:2)
经过一些更正后工作: - 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;
}
}
}
}
答案 1 :(得分:1)
您使用的是字符,而不是数字。
当你这样做时
case '1': // uses a character, which has an ascii (thus integer) value of 49
与此类似int x = 1; // integer value of 1
case 1: // uses an integer with value of 1
char
可以自动投射到int
,这就是这里发生的事情。在{{1}一种类型和switch
在另一种类型上发出警告似乎很方便,但是c' est la vie。