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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章