java版 石头剪刀布游戏制作

电脑:

import java.util.Random;

public class Computer {

String name;

double score;

//构造方法

public Computer(double score){

this.score = score;

}

public Computer () {

}

int play(){

Random rand = new Random();

int num = rand.nextInt(3)+1;

switch (num) {

case 1:{

System.out.println(this.name +"请出拳:石头");

break;

}

case 2:{

System.out.println(this.name +"请出拳:剪刀");

break;

}

default:{

System.out.println(this.name +"请出拳:布子");

break;

}

}

return num;

}

}

人:

import java.util.Scanner;

public class Person {

String name;

double score;

// 构造方法

public Person() {

}

public Person(double score) {

this.score = score;

}

// 方法 玩

int play() {

Scanner sc = new Scanner(System.in);

int num = sc.nextInt();

switch (num) {

case 1: {

System.out.println(this.name +"请出拳:石头");

break;

}

case 2: {

System.out.println(this.name +"请出拳:剪刀");

break;

}

default: {

System.out.println(this.name +"请出拳:布子");

break;

}

}

return num;

}

}

菜单类:

import java.util.Scanner;

public class Menu {

Computer computer;

Person person;

// 初始化方法

void init() {

computer = new Computer(0);

System.out.println("请输入电脑名:");

Scanner scanner = new Scanner(System.in);

computer.name = scanner.next();

person = new Person(0);

System.out.println("请输入玩家姓名:");

person.name = scanner.next();

}

// 开始游戏

void start() {

System.out.println("请出拳(1 石头  2 剪刀  3 布子)");

int PNum = person.play();

int cNum = computer.play();

cal(PNum, cNum);

System.out.println("是否退出(exit)");

Scanner sc = new Scanner(System.in);

String str = sc.next();

System.out.println("退出成功");

if ("exit".equals(str)) {

System.out.println(this.person.name + "得分为:" + this.person.score);

System.out.println(this.computer.name + "得分为:"

+ this.computer.score);

return;

} else {

start();

}

}

void cal(int num, int num1) {

if (num == num1) {

System.out.println("平局");

}

if ((num == 1 && num1 == 3) || (num == 1 && num1 == 3)

|| (num == 1 && num1 == 3)) {

System.out.println(this.person.name + "赢");

person.score++;

} else {

System.out.println(this.computer.name + "赢");

computer.score++;

}

}

}

测试类:(调用方法)

public class Test {

public static void main(String[] args) {

Menu menu = new Menu();

menu.init();

menu.start();

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,305评论 18 399
  • 第一章 初识javaJAVA 第一讲:什么是程序?:为了让计算机执行某些操作或解决某个问题而编写的一系列有序指令的...
    人子日月几点阅读 3,570评论 0 1
  • 小类型向大类型转换 不同类型的数据经常出现相互转换的现象. 1, 在Java中小类型向大类型的转换会自动完成, 即...
    o0寳贝阅读 3,421评论 0 0
  • Java经典问题算法大全 /*【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子...
    赵宇_阿特奇阅读 5,993评论 0 2
  • 如果春风可以珍藏 我想储上四月最温暖的一缕 莺飞草长 芳菲竞妍 抑或是烟笼翠柳 雨打海棠 春风如恋人的双手 抚平所...
    王海426阅读 1,247评论 0 0

友情链接更多精彩内容