一、源程序代码如下:
import java.util.Scanner;
public class Ks {
public static void main(String[] args) {
//初始化
Scanner scanner=new Scanner(System.in);
//定义字符串变量考生号no,姓名name,赋值为空
String no="";
String name="";
//定义整型变量成绩,赋值为0
int sum=0;
//1.用户输入准考证号、姓名
System.out.println(" 考 试 系 统 ");
System.out.println("请输入考生号:");
no=scanner.next();
if ( (no.indexOf("~")==-1) && (no.indexOf("!")==-1) && (no.indexOf("#")==-1) && (no.indexOf("-")==-1) && (no.indexOf("*")==-1)) {
System.out.println("请输入姓名:");
name = scanner.next();
//2.进入考试界面(1----数学,2----英语两门课)这种格式是命令行菜单
System.out.println("******************************");
System.out.println(" 考 试 系 统 ");
System.out.println(" 1----数学");
System.out.println(" 2----英语");
System.out.println("******************************");
//3.用户输入数学,语文或英语的标号
System.out.println("请选择考试科目对应的标号:");
String select_no = scanner.next();
//4.判断用户选择的标号内容
if (select_no.equals("1")) {
//4.系统出题,限定选择题
//5.用户输入答案,判断答案,对加5分,错不加分
System.out.println(" 考 试 开 始 ");
System.out.println("1、1+1=()\n"+"A.1\n"+"B.2\n"+"C.3\n"+"D.0\n");
System.out.print("请选择:");
String ans1 = scanner.next();
System.out.println("2、1+2=()\n"+"A.1\n"+"B.2\n"+"C.3\n"+"D.0\n");
System.out.print("请选择:");
String ans2 = scanner.next();
System.out.println("3、2-1=()\n"+"A.1\n"+"B.2\n"+"C.3\n"+"D.0\n");
System.out.print("请选择:");
String ans3 = scanner.next();
System.out.println("4、1-1=()\n"+"A.1\n"+"B.2\n"+"C.3\n"+"D.0\n");
System.out.print("请选择:");
String ans4 = scanner.next();
System.out.println("5、5+7=()\n"+"A.13\n"+"B.15\n"+"C.12\n"+"D.17\n");
System.out.print("请选择:");
String ans5 = scanner.next();
System.out.println("6、5+8=()\n"+"A.13\n"+"B.15\n"+"C.12\n"+"D.17\n");
System.out.print("请选择:");
String ans6 = scanner.next();
System.out.println("7、5+10=()\n"+"A.13\n"+"B.15\n"+"C.12\n"+"D.17\n");
System.out.print("请选择:");
String ans7 = scanner.next();
System.out.println("8、5+12=()\n"+"A.13\n"+"B.15\n"+"C.12\n"+"D.17\n");
System.out.print("请选择:");
String ans8 = scanner.next();
System.out.println("9、17-5=()\n"+"A.13\n"+"B.15\n"+"C.12\n"+"D.17\n");
System.out.print("请选择:");
String ans9 = scanner.next();
System.out.println("10、6+6=()\n"+"A.13\n"+"B.15\n"+"C.12\n"+"D.17\n");
System.out.print("请选择:");
String ans10 = scanner.next();
System.out.println("11、3-2=()\n"+"A.1\n"+"B.2\n"+"C.3\n"+"D.0\n");
System.out.print("请选择:");
String ans11 = scanner.next();
System.out.println("12、5-3=()\n"+"A.1\n"+"B.2\n"+"C.3\n"+"D.0\n");
System.out.print("请选择:");
String ans12 = scanner.next();
System.out.println("13、6-4=()\n"+"A.1\n"+"B.2\n"+"C.3\n"+"D.0\n");
System.out.print("请选择:");
String ans13 = scanner.next();
System.out.println("14、7-5=()\n"+"A.1\n"+"B.2\n"+"C.3\n"+"D.0\n");
System.out.print("请选择:");
String ans14 = scanner.next();
System.out.println("15、8+4=()\n"+"A.13\n"+"B.15\n"+"C.12\n"+"D.17\n");
System.out.print("请选择:");
String ans15 = scanner.next();
System.out.println("16、9+3=()\n"+"A.13\n"+"B.15\n"+"C.12\n"+"D.17\n");
System.out.print("请选择:");
String ans16 = scanner.next();
System.out.println("17、10+2=()\n"+"A.13\n"+"B.15\n"+"C.12\n"+"D.17\n");
System.out.print("请选择:");
String ans17 = scanner.next();
System.out.println("18、11+1=()\n"+"A.13\n"+"B.15\n"+"C.12\n"+"D.17\n");
System.out.print("请选择:");
String ans18 = scanner.next();
System.out.println("19、12-0=()\n"+"A.13\n"+"B.15\n"+"C.12\n"+"D.17\n");
System.out.print("请选择:");
String ans19 = scanner.next();
System.out.println("20、13-1=()\n"+"A.13\n"+"B.15\n"+"C.12\n"+"D.17\n");
System.out.print("请选择:");
String ans20 = scanner.next();
//6.考虑对英文字母的问题以及用户输入的大小写问题
if (ans1.toUpperCase().equals("B")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans2.toUpperCase().equals("C")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans3.toUpperCase().equals("A")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans4.toUpperCase().equals("D")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans5.toUpperCase().equals("C")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans6.toUpperCase().equals("A")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans7.toUpperCase().equals("B")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans8.toUpperCase().equals("D")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans9.toUpperCase().equals("C")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans10.toUpperCase().equals("C")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans11.toUpperCase().equals("A")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans12.toUpperCase().equals("B")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans13.toUpperCase().equals("B")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans14.toUpperCase().equals("B")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans15.toUpperCase().equals("C")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans16.toUpperCase().equals("C")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans17.toUpperCase().equals("C")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans18.toUpperCase().equals("C")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans19.toUpperCase().equals("C")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans20.toUpperCase().equals("C")) {
//选择正确加分5分
sum = sum + 5;
}
//7.输出成绩
System.out.println("******************************");
System.out.println(" 考 试 结 束 ");
System.out.println("考生号:" + no + ",姓名:" + name + ",成绩为:" + sum);
System.out.println("******************************");
}
else if (select_no.equals("2")) {
//4.系统出题,限定选择题
//5.用户输入答案,判断答案,对加5分,错不加分
System.out.println(" 考 试 开 始 ");
System.out.println("1、What is the capital of France?()\n"+"A.London\n"+"B.Paris\n"+"C.Berlin\n"+"D.Rome\n");
System.out.print("请选择:");
String ans1 = scanner.next();
System.out.println("2、How many continents are there?()\n"+ "A.5\n"+"B.6\n"+"C.7\n"+"D.8\n");
System.out.print("请选择:");
String ans2 = scanner.next();
System.out.println("3、What is the opposite of 'hot'?()\n"+ "A.Cold\n"+"B.Wet\n"+"C.Big\n"+"D.Small\n");
System.out.print("请选择:");
String ans3 = scanner.next();
System.out.println("4、Who wrote the play 'Romeo and Juliet'?()\n"+"A.William Shakespeare\n"+"B.Charles Dickens\n"+"C.Jane Austen\n"+"D.Mark Twain\n");
System.out.print("请选择:");
String ans4 = scanner.next();
System.out.println("5、What is the largest planet in our solar system?()\n"+"A.Mercury\n"+"B.Venus\n"+"C.Earth\n"+"D.Jupiter\n");
System.out.print("请选择:");
String ans5 = scanner.next();
System.out.println("6、What is the national language of China?()\n"+"A.English\n"+"B.Spanish\n"+"C.Mandarin\n"+"D.French\n");
System.out.print("请选择:");
String ans6 = scanner.next();
System.out.println("7、How many sides does a triangle have?()\n"+"A.2\n"+"B.3\n"+"C.4\n"+"D.5\n");
System.out.print("请选择:");
String ans7 = scanner.next();
System.out.println("8、 Who painted the Mona Lisa?()\n"+"A.Pablo Picasso\n"+"B.Vincent van Gogh\n"+"C.Leonardo da Vinci\n"+"D.Michelangelo\n");
System.out.print("请选择:");
String ans8 = scanner.next();
System.out.println("9、What is the chemical symbol for gold?()\n"+"A.Fe\n"+"B.Ag\n"+"C.Au\n"+"D.Cu\n");
System.out.print("请选择:");
String ans9 = scanner.next();
System.out.println("10、What is the tallest mountain in the world?()\n"+"A.Mount Everest\n"+"B.Mount Kilimanjaro\n"+"C.Mount Fuji\n"+"D.Mount McKinley\n");
System.out.print("请选择:");
String ans10 = scanner.next();
System.out.println("11、What is the opposite of 'big'?()\n"+"A.Small\n"+"B.Tall\n"+"C.Fast\n"+"D.Heavy\n");
System.out.print("请选择:");
String ans11 = scanner.next();
System.out.println("12、Who wrote the novel 'Pride and Prejudice'?()\n"+"A.Jane Austen\n"+"B.William Shakespeare\n"+"C.Charles Dickens\n"+"D.Mark Twain\n");
System.out.print("请选择:");
String ans12 = scanner.next();
System.out.println("13、What is the capital of Australia?()\n"+"A.Sydney\n"+"B.Melbourne\n"+"C.Canberra\n"+"D.Brisbane\n");
System.out.print("请选择:");
String ans13 = scanner.next();
System.out.println("14、How many colors are there in a rainbow?()\n"+"A.3\n"+"B.5\n"+"C.7\n"+"D.9\n");
System.out.print("请选择:");
String ans14 = scanner.next();
System.out.println("15、What is the largest ocean in the world?()\n"+"A.Atlantic Ocean\n"+"B.Indian Ocean\n"+"C.Arctic Ocean\n"+"D.Pacific Ocean\n");
System.out.print("请选择:");
String ans15 = scanner.next();
System.out.println("16、What is the opposite of 'good'?()\n"+"A.Bad\n"+"B.Nice\n"+"C.Happy\n"+"D.Sad\n");
System.out.print("请选择:");
String ans16 = scanner.next();
System.out.println("17、Who painted the 'Starry Night'?()\n"+"A.Pablo Picasso\n"+"B.Vincent van Gogh\n"+"C.Leonardo da Vinci\n"+"D.Michelangelo\n");
System.out.print("请选择:");
String ans17 = scanner.next();
System.out.println("18、What is the chemical symbol for silver?()\n"+"A.Fe\n"+"B.Ag\n"+"C.Au\n"+"D.Cu\n");
System.out.print("请选择:");
String ans18 = scanner.next();
System.out.println("19、What is the largest country in the world?()\n"+"A.China\n"+"B.Russia\n"+"C.United States\n"+"D.Canada\n");
System.out.print("请选择:");
String ans19 = scanner.next();
System.out.println("20、What is the national animal of Australia?()\n"+"A.Kangaroo\n"+"B.Koala\n"+"C.Emu\n"+"D.Platypus\n");
System.out.print("请选择:");
String ans20 = scanner.next();
//6.考虑对英文字母的问题以及用户输入的大小写问题
if (ans1.toUpperCase().equals("B")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans2.toUpperCase().equals("C")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans3.toUpperCase().equals("A")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans4.toUpperCase().equals("A")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans5.toUpperCase().equals("D")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans6.toUpperCase().equals("C")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans7.toUpperCase().equals("B")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans8.toUpperCase().equals("C")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans9.toUpperCase().equals("C")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans10.toUpperCase().equals("A")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans11.toUpperCase().equals("A")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans12.toUpperCase().equals("A")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans13.toUpperCase().equals("C")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans14.toUpperCase().equals("C")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans15.toUpperCase().equals("D")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans16.toUpperCase().equals("A")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans17.toUpperCase().equals("B")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans18.toUpperCase().equals("B")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans19.toUpperCase().equals("B")) {
//选择正确加分5分
sum = sum + 5;
}
if (ans20.toUpperCase().equals("A")) {
//选择正确加分5分
sum = sum + 5;
}
//7.输出成绩
System.out.println("******************************");
System.out.println(" 考 试 结 束 ");
System.out.println("考生号:" + no + ",姓名:" + name + ",成绩为:" + sum);
System.out.println("******************************");
}
else {
System.out.println("你选择了退出考试,没有参加任何一门课程的考试");
}
}
//因为if中的else有一个结束的语句,后面不要再有语句
}
}
二、运行结果为: