JAVA的那个蜥蜴是什么_java-石头剪刀布蜥蜴Spock游戏无法正常工作

因此,当我运行此命令时,我要输入值,但是输入值后,它不会执行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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值