Samoy的小窝

千里之行,始于足下 万行代码,始于指下

一只默默工作的程序猿


欢迎光临Samoy的小窝

Java控制台小游戏------猜拳游戏

先来看一下效果图: 效果图 首先我们创建一个Person类,这个类有name和score两个属性,有play这个方法,源代码如下:

package com.samoy;

public class Person {
    public String name;
    public int score;

    public Person(String name, int score) {
        this.name = name;
        this.score = score;
    }

    public String play(int i) {
        return i == 1?"剪刀":(i == 2?"石头":"布");
    }
}

接下来是主程序入口:

package com.samoy;

import java.util.Random;
import java.util.Scanner;

public class JavaTest {

    public static void main(String[] args) {

        int num = 0;

        System.out.println("*****************\n****猜拳, 开始****\n*****************");
        System.out.println("出拳规则:1.剪刀 2.石头 3.布");
        System.out.println("请选择对方角色<1:刘备 2:孙权 3:曹操>");

        Scanner scanner = new Scanner(System.in);

        //new一个对手
        Person opponent = new Person("",0);
        int nameIndex = scanner.nextInt();
        while (true){
            if (nameIndex == 1){
                opponent.name = "刘备";
                break;
            }else if (nameIndex == 2){
                opponent.name  = "孙权";
                break;
            }else if (nameIndex == 3){
                opponent.name = "曹操";
                break;
            }else {
                System.out.println("输入错误!输入对方角色<1:刘备 2:孙权 3:曹操>");
                nameIndex = scanner.nextInt();
            }
        }

        System.out.println("请输入您的姓名:");
        Scanner scanner1 = new Scanner(System.in);
        Person self = new Person(scanner1.next(),0);
        System.out.println(self.name + " VS " +opponent.name);
        System.out.println("要开始游戏吗?<y/n>");

        Scanner scanner2 = new Scanner(System.in);
        String option = scanner2.next();
        while (option.equals("y")){
            System.out.println("请出拳:1、剪刀 2、石头 3、布");
            Scanner scanner3 = new Scanner(System.in);
            int select = scanner3.nextInt();
            String result1;
            if (select!=1&&select!=2&&select!=3){
                System.out.println("输入错误!请输入出拳规则:1、剪刀 2、石头 3、布");
                select = scanner3.nextInt();
            }
            result1 = self.play(select);
            System.out.println(self.name+"出:"+result1);
            String result2 = opponent.play(new Random().nextInt(3)+1);
            System.out.println(opponent.name+"出:"+result2);


            if (result1.equals(result2)){
                System.out.println("和局");
            }else if (result1.equals("剪刀")&&result2.equals("布")||result1.equals("石头")&&result2.equals("剪刀")||result1.equals("布")&&result2.equals("石头")){
                System.out.println("你赢了! "+self.name+"赢!");
                self.score += 1;
            }else {
                System.out.println("你输了! "+opponent.name+"赢!");
                opponent.score += 1;
            }
            num++;
            System.out.println("需要进行下一局吗?<y/n>");
            option = scanner2.next();
            if (option.equals("n")){
                System.out.println("=================");
                System.out.println("共进行"+num+"局\n"+self.name+"得分为"+self.score+","+opponent.name+"得分为"+opponent.score +"\n");
                if (self.score>opponent.score){
                    System.out.println(self.name+"赢!"+self.name+"好棒!");
                }else if (self.score == opponent.score){
                    System.out.println("平局!");
                }else {
                    System.out.println(opponent.name+"赢!"+self.name+"笨蛋!");
                }
                System.out.println("感谢你的使用!");
            }else if (!option.equals("y")){
                System.out.println("输入错误,请重新输入!");
                System.out.println("需要进行下一局吗?<y/n>");
                option = scanner2.next();
            }
        }
    }
}

源代码下载(提取码:7hwi)

最近的文章

如何解决"Adb not responding"的问题

今天在写Demo的时候,发现程序包名不合适,就Rename了一下,然而却出现了题目中所提到的问题,Genymotion模拟器加载不出来,初步以为是模拟器路径不对,更改了一下路径,然而并没什么卵用。重启了Android Studio后,初始化adb时,出现了"adb connection error"问题,方知是adb出现了问题。于是上网查了资料,进过研究,是由于其他程序占用了5037端口。将其杀死即可。具体解决方法如下:1.关闭Android Studio;2.打开终端,键入sudo ls...…

更早的文章

一个简单的Java命令行添加/删除联系人程序(仿C语言)

声明:Java初学者适用演示效果:联系人Class:import java.util.ArrayList;/** * Created by Samoy on 16/5/13. */public class Contact { private String name; private String number; private ArrayList<Contact> contacts; public ArrayList<Contact> get...…