java-石头剪刀布蜥蜴Spock游戏无法正常工作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-石头剪刀布蜥蜴Spock游戏无法正常工作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3200字,纯文字阅读大概需要5分钟。
内容图文
![java-石头剪刀布蜥蜴Spock游戏无法正常工作](/upload/InfoBanner/zyjiaocheng/668/26752e62528848e192b558e3c3096ad4.jpg)
因此,当我运行此命令时,我要输入值,但是输入值后,它不会执行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-石头剪刀布蜥蜴Spock游戏无法正常工作全部内容,希望文章能够帮你解决java-石头剪刀布蜥蜴Spock游戏无法正常工作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】