因此,当我运行此命令时,我要输入值,但是输入值后,它不会执行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
来源: https://codeday.me/bug/20191110/2015478.html