JavaSE之简单猜拳游戏

来源:互联网 发布:淘宝详情页下载工具 编辑:程序博客网 时间:2024/04/29 08:06

小游戏比较简单,一共有三个类,Human,Computer,game,用人和电脑直接返回一个数字给game,然后再game中取判断游戏的结果!代码如下

Human类:

<span style="font-family:Microsoft YaHei;font-size:18px;">import java.util.Scanner;public class Human {public int Human(){Scanner in=new Scanner(System.in);int q=in.nextInt();String gg="";if(q==1){gg="石头";}else if(q==2){gg="剪刀";}else if(q==3){gg="布";}System.out.println("您出的为"+gg);return q;}}</span>
Computer类:

<span style="font-family:Microsoft YaHei;font-size:18px;">import java.util.Random;public class Computer {public int Computer(){Random r=new Random();int com=r.nextInt(3)+1;String gg=""; if(com==1){gg="石头";}else if(com==2){gg="剪刀";}else if(com==3){gg="布";}System.out.println("电脑出的为"+gg);return com;}}</span>
game类:

<span style="font-family:Microsoft YaHei;font-size:18px;">import java.util.Scanner;public class Game {public static void main(String[] args) {System.out.println("********石头剪刀布游戏********");Computer computer=new Computer();Human human=new Human();int com;int hu;hu=human.Human();com=computer.Computer();if(hu==1&&com==1||hu==2&&com==2||hu==3&&com==3){System.out.println("你们一样厉害,居然平局了。");}else if(hu==1&&com==3||hu==2&&com==1||hu==3&&com==2){System.out.println("不好意思,你输了。");}else if(com==1&&hu==3||com==2&&hu==1||com==3&&hu==2){System.out.println("恭喜你赢了");}else{System.out.println("很抱歉,您输入错误");}}}</span>






0 0
原创粉丝点击