欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > 四、小白学JAVA-石头剪刀布游戏

四、小白学JAVA-石头剪刀布游戏

2025/3/26 17:38:51 来源:https://blog.csdn.net/p6448777/article/details/146407660  浏览:    关键词:四、小白学JAVA-石头剪刀布游戏

1、如何从控制台获取用户输入

import java.util.Scanner;public class Main {public static void main(String[] args) {// 石头剪刀布的思路// 1 2 3 石头 剪刀 布Scanner scanner = new Scanner(System.in);System.out.println("请出拳:1.石头  2.剪刀 3.布【请输入数字1或2或3】");int userInput = Integer.parseInt(scanner.nextLine());switch (userInput) {case 1:System.out.println("您出的是石头");break;case 2:System.out.println("您出的是剪刀");break;case 3:System.out.println("您出的是布");break;}}
}

2、设计电脑如何随机出石头剪刀布

int computerResult = (int) (Math.random() * 3 + 1);
switch (computerResult) {case 1:System.out.println("电脑出的是石头");System.out.println("游戏结果:您失败了");break;case 2:System.out.println("电脑出的也是剪刀");System.out.println("游戏结果:平局");break;case 3:System.out.println("电脑出的是布");System.out.println("游戏结果:您赢了");break;
}

3、如何实现游戏的多轮玩法

import java.util.Scanner;public class Main {public static void main(String[] args) {// 石头剪刀布的思路// 1 2 3 石头 剪刀 布while (true) {System.out.println("新一轮游戏开始**************************************************************************************");int computerResult = (int) (Math.random() * 3 + 1);// System.out.println(computerResult);Scanner scanner = new Scanner(System.in);System.out.println("请出拳:1.石头  2.剪刀 3.布【请输入数字1或2或3】  退出游戏请输入q");String quitResult = scanner.nextLine();if (quitResult.equals("q")) {System.out.println("退出游戏成功,欢迎下次再来!");break;}int userInput = Integer.parseInt(quitResult);switch (userInput) {case 1:System.out.println("您出的是石头");switch (computerResult) {case 1:System.out.println("电脑出的也是石头");System.out.println("游戏结果:平局");break;case 2:System.out.println("电脑出的也是剪刀");System.out.println("游戏结果:您赢了");break;case 3:System.out.println("电脑出的也是布");System.out.println("游戏结果:您失败了");break;}break;case 2:System.out.println("您出的是剪刀");switch (computerResult) {case 1:System.out.println("电脑出的是石头");System.out.println("游戏结果:您失败了");break;case 2:System.out.println("电脑出的也是剪刀");System.out.println("游戏结果:平局");break;case 3:System.out.println("电脑出的是布");System.out.println("游戏结果:您赢了");break;}break;case 3:System.out.println("您出的是布");switch (computerResult) {case 1:System.out.println("电脑出的是石头");System.out.println("游戏结果:您赢了");break;case 2:System.out.println("电脑出的是剪刀");System.out.println("游戏结果:您失败了");break;case 3:System.out.println("电脑出的也是布");System.out.println("游戏结果:平局");break;}break;}}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词