猜数小游戏基础版思路图:
猜数小游戏三次机会版本思路:
猜数小游戏游戏币版本思路:
这里我三个版本写在了一起,但有注释,用的是面向对象
首先搭建平台
import java.util.Random;
import java.util.Scanner;
public class game {
//扫描仪
Scanner input = new Scanner(System.in);
//定义玩家对象
user user = new user();
//定义Npc对象
Npc Npc = new Npc();
//定义裁判对象
CaiPan caiPan = new CaiPan();
//定义随机数
Random random = new Random();
//定义游戏币
int coin = 40;
//搭建平台
public void showgame() {
//显示欢迎语句
showwelcome();
//初始化数据
showname();
//询问是否开始游戏
System.out.print("请问要开始游戏吗?(y 开始游戏 / 其他字符结束)");
String usergame = input.next();
if (usergame.equals("y")) {
System.out.println("正在进入游戏");
System.out.println("\t\t\t\tLoading...");
} else {
System.out.println("正在退出...");
return;
}
//一局三次
//实例化对象 Npc
//用一个值来接送Npc随机的值
//定义一局
int tamp = 0;
boolean loop = true;
while (loop) {
//判断用户的游戏币是否足够这一局
System.out.println(coin);
if (coin < 10) {
System.out.println("您的游戏币不足");
return;
}
tamp++;
System.out.println("第" + tamp + "局:");
coin = gamecycle(Npc.getNpcNum());
System.out.print("请问要继续游戏吗?((y 开始游戏 / 其他字符结束))");
String zifu = input.next();
if (!zifu.equals("y")){
showresult();
return;
}
}
}
//欢迎语句
public void showwelcome() {
System.out.println("* * * * * * * * * * * * * * * * * * * * * * ");
System.out.println("欢迎来到,青鸟猜数小游戏");
System.out.println("* * * * * * * * * * * * * * * * * * * * * * ");
}
//录入玩家姓名和Npc姓名
public void showname() {
//用户输入昵称
System.out.print("请输入玩家昵称:");
user.username = input.next();
//用户输入NPC昵称
System.out.print("请输入NPC昵称:");
Npc.npcname = input.next();
}
//猜数小游戏三次机会
public void showsangame(){
//定义布尔类型的值,用作游戏循环
boolean loop = true;
//定义一个值来记录局数
int tamp = 0;
//定义一个值用来接收 Npc 的随机数
int npc = Npc.getNpcNum();
while (loop){
tamp++;
int userNum = user.getnum();
if (userNum == npc){
System.out.println("猜对了");
loop = false;
break;
}else if (userNum > npc){
System.out.println("大了");
}else if (userNum<npc){
System.out.println("小了");
}
if (tamp == 3){
System.out.println("对不起,游戏次数已用完!");
loop = false;
break;
}
}
}
//猜数小游戏无尽版
public void show(){
//游戏循环
//实现猜中为止
boolean loop = true;
//定义局数
int tampju = 0;
int npcNum = random.nextInt(11);
while (loop){
tampju++;
System.out.println("请输入您猜的数:");
int userNum = input.nextInt();
if (userNum == npcNum){
System.out.println("猜对了!");
loop = false;
}else if (userNum > npcNum){
System.out.println("大了");
}else if (userNum < npcNum){
System.out.println("小了");
}
}
//输出用户用了多少局
System.out.println("用了"+tampju+"局才猜中!");
}
// 游戏币版本
//游戏循环 实现一局游戏玩三次
public int gamecycle(int npcNum) {
//游戏循环
boolean loop = true;
//设置游戏循环
//定义局数
int tampju = 0;
while (loop) {
//定义一个局数
tampju++;
//玩一局 游戏 扣 10 游戏币
coin -= 10;
//用一个值来接收用户输入的值
int getnum = user.getnum();
//实例化裁判对象
//这里拿个值接受用户猜对的奖励
int gameboin = caiPan.showjudge(getnum, npcNum);
//游戏币 += 奖励
coin += gameboin;
if (tampju == 3) {
loop = false;
}
}
return coin;
}
//输出结果
public void showresult(){
System.out.println("下次继续加油!");
}
}
平台搭建完后,进行用户代码的编写
import java.util.Scanner;
public class user {
//用户名
String username;
//玩家的胜场
int scres;
public int getnum(){
//扫描仪
Scanner input = new Scanner(System.in);
//让用户输入数字 并返回
System.out.print("请输入您猜想的数:");
//返回值
return input.nextInt();
}
}
NPC
import java.util.Random;
public class Npc {
// NPC 姓名
String npcname;
public int getNpcNum(){
//新建随机数对象
Random random = new Random();
//设置一个值来接收 这个随机的数
//这里有两种写法 一种是上面说的 一种是直接返回 random.nextInt(11)
return random.nextInt(11);
}
}
裁判
public class CaiPan {
//判断 用户 输入的值和 Npc 随机出来的值是否相等
//新建 user 对象
public int showjudge(int user, int Npc) {
int gamecoin = 0;
if (user == Npc) {
System.out.println("恭喜您答对了!");
gamecoin += 30;
} else if (user > Npc) {
System.out.println("您输入的值过大!");
} else if (user < Npc) {
System.out.println("请输入的值太小");
}
return gamecoin;
}
}
开始
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//新建 game 对象
game game = new game();
System.out.println("请选择猜数小游戏版本!");
System.out.println("\t\t\t1.猜数小游戏无尽版");
System.out.println("\t\t\t2.猜数小游戏三次机会");
System.out.println("\t\t\t3.猜数小游戏游戏币版");
//文字录用
System.out.print("请输入您要想玩的版本:");
int num = input.nextInt();
switch (num){
case 1:
//实例化 show 对象
game.show();
break;
case 2:
//实例化 showsangame 对象
game.showsangame();
break;
case 3:
//实例化 showgame 对象
game.showgame();
break;
}
}
}