我制作了一个java计算游戏,源码如下(java:1.8):
import java.util.Scanner;
import java.util.Random;
public class GameMain {
public static void main(String [] args){
Scanner a = new Scanner(System.in);
System.out.print("欢迎来到数学小游戏");//输出
Wait();
cls();
while(true) {
System.out.print("请输入难度(1=简单,2=中等,3=困难,4=退出) />");
int b = a.nextInt();
if (b == 1) {
System.out.print("正在为您切换为 简单");
easy();
} else if (b == 2) {
System.out.print("正在为您切换为 中等");
zhongdeng();
} else if (b == 3) {
System.out.print("正在为您切换为 困难");
hard();
} else if (b == 4) {
System.out.print("正在退出中...");
Wait();
Exit();
}
}
}
public static void easy(){
int score = 0;
Scanner sc = new Scanner(System.in);
Random ra = new Random();
int a,b,c,d;
cls();
System.out.print("简单共有20题,为50以下的加法");
for(int i = 1;i < 20;i++){
a = ra.nextInt(50);
b = ra.nextInt(50);
System.out.print(a + "+" + b + "=");
c = (a + b);
d = ra.nextInt();
ln();
if(d == c){
score = (score + 10);
System.out.print("正确!加10分!");
}else{
System.out.print("哎,别灰心,答案是" + c);
}
}
cls();
ln();
System.out.println("一共有200分,你获得了" + score + "分");
System.out.print("10秒后自动退出...");
Wait10s();
Exit();
}
public static void zhongdeng(){
System.out.print("中等共有50题,为500下的加法");
int score = 0;
Scanner sc = new Scanner(System.in);
Random ra = new Random();
int a,b,c,d;
cls();
for(int i = 1;i < 50;i++){
a = ra.nextInt(500);
b = ra.nextInt(500);
System.out.print(a + "+" + b + "=");
c = (a + b);
d = ra.nextInt();
ln();
if(d == c){
score = (score + 10);
System.out.print("正确!加10分!");
}else{
System.out.print("哎,别灰心,答案是" + c);
}
}
System.out.println("一共有500分,你获得了" + score + "分");
System.out.print("10秒后自动退出...");
Wait10s();
Exit();
}
public static void hard(){
System.out.print("困难共有100题,为5000以下的加法");
int score = 0;
Scanner sc = new Scanner(System.in);
Random ra = new Random();
int a,b,c,d;
cls();
for(int i = 1;i < 100;i++){
a = ra.nextInt(5000);
b = ra.nextInt(5000);
System.out.print(a + "+" + b + "=");
c = (a + b);
d = ra.nextInt();
ln();
if(d == c){
score = (score + 10);
System.out.print("正确!加10分!");
}else{
System.out.print("哎,别灰心,答案是" + c);
}
}
System.out.println("一共有1000分,你获得了" + score + "分");
System.out.print("10秒后自动退出...");
Wait10s();
Exit();
}
private static void Wait(){//等待
try {
Thread.sleep(2000);
} catch (Exception var1) {
System.exit(0);
}
}
private static void ln(){
System.out.println();//换行
}
public static void cls(){
for(int i = 1;i < 50;i++){
System.out.println();
}
}
private static void Exit(){
System.exit(0);
}
private static void Wait10s(){//等待
try {
Thread.sleep(10000);
} catch (Exception var1) {
System.exit(0);
}
}
}
但是它的输出却是这样的:
求解,实在感谢!