我是一个才学习java的小菜鸟:
最近做一个猜数字游戏!
当游戏被才对时:游戏者回车确认后,回到主界面;如果输入次数超过8次,则提示“超过次数,尚需努力”,游戏者回车确认后,回到主界面。以下是我的代码:
import java.util.Random;
import java.util.Scanner;public class Guess {
public static void main(String[] args){
System.out.println("***欢迎进入猜字游戏***");
System.out.println("1.开始游戏 ");
System.out.println("2.退出");
//产生随机数
Random r = new Random();
int num = r.nextInt(100);
Scanner scanner= new Scanner(System.in);
int command=scanner.nextInt();
// 这个当输入1时开始:2时退出
if(command==2){
System.exit(0);
}
while(command==1){
int guessNum=0;
System.out.println("请输入你的猜测");
// 下面是用键盘输入 存在sc中
Scanner sc = new Scanner(System.in);
guessNum=sc.nextInt();
int compare=guessNum-num;
if(compare>0){
System.out.println("你的猜测偏大");
}
if(compare<0){
System.out.println("你的猜测偏小");
}
if(compare==0){
System.out.println("猜测结果正确,游戏结束!");
System.exit(0);
}
}
最近做一个猜数字游戏!
当游戏被才对时:游戏者回车确认后,回到主界面;如果输入次数超过8次,则提示“超过次数,尚需努力”,游戏者回车确认后,回到主界面。以下是我的代码:
import java.util.Random;
import java.util.Scanner;public class Guess {
public static void main(String[] args){
System.out.println("***欢迎进入猜字游戏***");
System.out.println("1.开始游戏 ");
System.out.println("2.退出");
//产生随机数
Random r = new Random();
int num = r.nextInt(100);
Scanner scanner= new Scanner(System.in);
int command=scanner.nextInt();
// 这个当输入1时开始:2时退出
if(command==2){
System.exit(0);
}
while(command==1){
int guessNum=0;
System.out.println("请输入你的猜测");
// 下面是用键盘输入 存在sc中
Scanner sc = new Scanner(System.in);
guessNum=sc.nextInt();
int compare=guessNum-num;
if(compare>0){
System.out.println("你的猜测偏大");
}
if(compare<0){
System.out.println("你的猜测偏小");
}
if(compare==0){
System.out.println("猜测结果正确,游戏结束!");
System.exit(0);
}
}
import java.util.Random;
import java.util.Scanner;public class GuessGame {
public static void main(String[] args) { while (true) { int command = getIndexChoice();
; // 这个当输入1时开始:2时退出
System.out.println(command);
if (command == 2)
System.exit(0); else {
int count = 0;//已经进行过0次
while (true) {
boolean flag = playGames();
if (flag == true) {
System.out.println("继续玩游戏,返回主界面");
break;
} else {
count++;//次数加1
if (count == 8) {
System.out.println("超过次数,尚需努力");
break;
} }
}
;
}
}
} static int getIndexChoice() {//获取主页中选项
System.out.println("***欢迎进入猜字游戏***");
System.out.println("1.开始游戏 ");
System.out.println("2.退出");
// 产生随机数
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
} static boolean playGames() {//,输出为 是否猜中
Random r = new Random();
int num = r.nextInt(100); //随机产生一个数据
int guessNum = 0;
System.out.println("请输入你的猜测");
// 下面是用键盘输入 存在sc中
Scanner sc = new Scanner(System.in);
guessNum = sc.nextInt();
int compare = guessNum - num;
if (compare > 0) {
System.out.println("你的猜测偏大");
return false;
} else if (compare < 0) {
System.out.println("你的猜测偏小");
return false;
}
// if (compare == 0) {
else {
//System.out.println("猜测结果正确,游戏结束!");
//System.exit(0);
System.out.println("猜测结果正确,回到主界面!");
return true;
}
}
}
虽然当时写起来很方便,但日后你要升级系统功能,要维护你的代码,你去哭吧
int num = r.nextInt(100); //随机产生一个数据
改为 Random r = new Random();
int num = r.nextInt(100); //随机产生一个数据
System.out.println("num = " + num);然后你c重新试试,我的测试结果:
/*
***欢迎进入猜字游戏***
1.开始游戏
2.退出
1
1
num = 88
请输入你的猜测
88
猜测结果正确,回到主界面!
继续玩游戏,返回主界面
***欢迎进入猜字游戏***
1.开始游戏
2.退出
1
1
num = 63
请输入你的猜测
64
你的猜测偏大
num = 69
请输入你的猜测
68
你的猜测偏小
num = 93
请输入你的猜测
93
猜测结果正确,回到主界面!
继续玩游戏,返回主界面
***欢迎进入猜字游戏***
1.开始游戏
2.退出
2
2
*、
import java.util.Random;
import java.util.Scanner;public class GuessGame {
private static int GUESS_NUM;
private static int GUESS_ALL_NUM=8;
public static void main(String[] args) {
while (true) {
GUESS_NUM=initGuessNumber();
int command = getIndexChoice();
; // 这个当输入1时开始:2时退出
System.out.println(command);
if (command == 2)
System.exit(0); else {
int count = 0;// 已经进行过0次
while (true) {
boolean flag = playGames();
count++;
System.out.println("已经猜了"+count+"次,还有"+(GUESS_ALL_NUM-count)+"次机会");
if (flag == true) {
System.out.println("继续玩游戏,返回主界面");
break;
} else {
// 次数加1
if (count == GUESS_ALL_NUM) {
System.out.println("超过次数,尚需努力");
break;
} }
}
;
}
}
}
static int initGuessNumber(){
Random r = new Random();
int num = r.nextInt(100); // 随机产生一个数据
return num;
} static int getIndexChoice() {// 获取主页中选项
System.out.println("***欢迎进入猜字游戏***");
System.out.println("1.开始游戏 ");
System.out.println("2.退出");
// 产生随机数
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
} static boolean playGames() {// ,输出为 是否猜中
int guessNum = 0;
System.out.println("请输入你的猜测");
// 下面是用键盘输入 存在sc中
Scanner sc = new Scanner(System.in);
guessNum = sc.nextInt();
int compare = guessNum - GUESS_NUM;
if (compare > 0) {
System.out.println("你的猜测偏大");
return false;
} else if (compare < 0) {
System.out.println("你的猜测偏小");
return false;
}
// if (compare == 0) {
else {
// System.out.println("猜测结果正确,游戏结束!");
// System.exit(0);
System.out.println("猜测结果正确,回到主界面!");
return true;
}
}
}
我只是想说明num是在0到99之间,而且,如果用户输入的数大于num时就会显示大于,如果用户输入的数小于num时就会显示小于,如果用户输入的数等于num时就会显示等于,再返回主界面在主界面中,只能输入1,2,输入2表示退出
如果没有在主界面,也就是在运行中,你输入1,2,3,....都是不会退出的,你自己测试一下吧
import java.util.Random;
import java.util.Scanner;public class Test_0014 {
public static void main(String[] args){
test();
}
public static void test(){
int times = 0;
System.out.println("***欢迎进入猜字游戏***");
System.out.println("1.开始游戏 ");
System.out.println("2.退出");
// 产生随机数
Random r = new Random();
int num = r.nextInt(100); System.out.println(num); Scanner scanner = new Scanner(System.in);
int command = scanner.nextInt();
// 这个当输入1时开始:2时退出
if (command == 2) {
System.exit(0);
}
while (command == 1) {
int guessNum = 0;
System.out.println("请输入你的猜测");
// 下面是用键盘输入 存在sc中
Scanner sc = new Scanner(System.in);
guessNum = sc.nextInt(); int compare = guessNum - num;
if (compare > 0) {
System.out.println("你的猜测偏大");
}
if (compare < 0) {
System.out.println("你的猜测偏小");
}
if (compare == 0) {
System.out.println("猜测结果正确,游戏结束!");
test();
} else
times++;
if(times>=8){
System.out.println("超过次数,尚需努力");
test();
}
}
}
}