找一个java游戏
答案:1 悬赏:10 手机版
解决时间 2021-03-04 19:54
- 提问者网友:爱了却不能说
- 2021-03-04 14:32
找一个java游戏
最佳答案
- 五星知识达人网友:毛毛
- 2021-03-04 14:48
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();
}
}
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯