用java编写一个猜字游戏程序,答案不限
1个回答
推荐于2018-03-26
展开全部
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int guessTime = 8;
boolean restart = true;
while (restart) {
System.out.println("请选择下列选项!");
System.out.println("1 ----开始猜数");
System.out.println("2 ----游戏参数设置");
System.out.println("9 ----退出");
boolean flag = true;
String choose = null;
Scanner in = null;
while (flag) {
in = new Scanner(System.in);
choose = in.next();
if (!choose.matches("[129]")) {
System.out.println("输入错误,请重新输入");
continue;
} else {
break;
}
}
if ("2".equals(choose)) {
boolean chooseLevel = false;
System.out.println("请选择游戏难度!");
System.out.println("1 ----难(4次)");
System.out.println("2 ----一般(6次)");
System.out.println("3 ----容易(8次)");
while (!chooseLevel) {
String s = in.next();
if (s.matches("[123]")) {
switch (Integer.parseInt(s)) {
case 1:
guessTime = 4;
chooseLevel = true;
break;
case 2:
guessTime = 6;
chooseLevel = true;
break;
case 3:
guessTime = 8;
chooseLevel = true;
break;
default:
System.out.println("你输入的选择不存在(请输入1、2、3)");
break;
}
} else {
System.out.println("你输入的选择不存在(请输入1、2、3)");
}
}
continue;
}
if ("9".equals(choose)) {
System.exit(0);
}
if ("1".equals(choose)) {
String number = String.valueOf(Math.round(Math.random() * 100) + 1);
System.out.println(number);
boolean getIt = false;
int count = 0;
while (!getIt && ++count <= guessTime) {
System.out.println("请输入你猜的值(1-100)");
String s = in.next();
if (s.matches("\\d+")) {
try {
if (Integer.parseInt(s) == Integer.parseInt(number)) {
getIt = true;
break;
} else if (Integer.parseInt(s) > Integer.parseInt(number)) {
System.out.println("你输入的数字大了!");
} else {
System.out.println("你输入的数字小了!");
}
} catch (NumberFormatException e) {
System.out.println("你输入的数据超过Integer的最大范围!");
continue;
}
} else {
System.out.println("你输入的数据不合法!");
continue;
}
}
if (getIt) {
System.out.println("恭喜你猜对了,你的战斗力是" + Math.round((1 - count * 1.0 / guessTime) * 100) + "%");
} else {
System.out.println("超过次数,尚需努力");
}
in.nextLine();
in.nextLine();
}
}
}
}
public class Test {
public static void main(String[] args) {
int guessTime = 8;
boolean restart = true;
while (restart) {
System.out.println("请选择下列选项!");
System.out.println("1 ----开始猜数");
System.out.println("2 ----游戏参数设置");
System.out.println("9 ----退出");
boolean flag = true;
String choose = null;
Scanner in = null;
while (flag) {
in = new Scanner(System.in);
choose = in.next();
if (!choose.matches("[129]")) {
System.out.println("输入错误,请重新输入");
continue;
} else {
break;
}
}
if ("2".equals(choose)) {
boolean chooseLevel = false;
System.out.println("请选择游戏难度!");
System.out.println("1 ----难(4次)");
System.out.println("2 ----一般(6次)");
System.out.println("3 ----容易(8次)");
while (!chooseLevel) {
String s = in.next();
if (s.matches("[123]")) {
switch (Integer.parseInt(s)) {
case 1:
guessTime = 4;
chooseLevel = true;
break;
case 2:
guessTime = 6;
chooseLevel = true;
break;
case 3:
guessTime = 8;
chooseLevel = true;
break;
default:
System.out.println("你输入的选择不存在(请输入1、2、3)");
break;
}
} else {
System.out.println("你输入的选择不存在(请输入1、2、3)");
}
}
continue;
}
if ("9".equals(choose)) {
System.exit(0);
}
if ("1".equals(choose)) {
String number = String.valueOf(Math.round(Math.random() * 100) + 1);
System.out.println(number);
boolean getIt = false;
int count = 0;
while (!getIt && ++count <= guessTime) {
System.out.println("请输入你猜的值(1-100)");
String s = in.next();
if (s.matches("\\d+")) {
try {
if (Integer.parseInt(s) == Integer.parseInt(number)) {
getIt = true;
break;
} else if (Integer.parseInt(s) > Integer.parseInt(number)) {
System.out.println("你输入的数字大了!");
} else {
System.out.println("你输入的数字小了!");
}
} catch (NumberFormatException e) {
System.out.println("你输入的数据超过Integer的最大范围!");
continue;
}
} else {
System.out.println("你输入的数据不合法!");
continue;
}
}
if (getIt) {
System.out.println("恭喜你猜对了,你的战斗力是" + Math.round((1 - count * 1.0 / guessTime) * 100) + "%");
} else {
System.out.println("超过次数,尚需努力");
}
in.nextLine();
in.nextLine();
}
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询