java 程序 选择题_Java代码,运行程序,出来一些选择题,并能输入选项,系统给出得分? 没有代码说说怎样做也可以的,谢谢...

public class ExamConsole { class Subject { String title; List answers = new ArrayList(); char correct; public Subject() { } public Subject(String title, char correctIdx, String... answers) { this.title = title; this.correct = correctIdx; for (String ss : answers) { this.answers.add(ss); } } } public static void main(String[] args) { new ExamConsole().test(); } public void test() { //定义题目 List subjects = new ArrayList(); subjects.add(new Subject("title1", 'A',// "String... answers1 ", "String... answers2", "String... answers3", "String... answers4")); subjects.add(new Subject("title2", 'B', // "String... answers1 ", "String... answers2", "String... answers3", "String... answers4")); Scanner sc = new Scanner(System.in); int sum = 0; int size = subjects.size(); for (int n = 0; n < size; n++) { Subject sub = subjects.get(n); System.out.println(" " + (n + 1) + ". " + sub.title); int anSize = sub.answers.size(); for (int i = 0; i < anSize; i++) { System.out.println(" " + ((char) ('A' + i)) + ". " + sub.answers.get(i)); } while (true) { System.out.print("请选择(A/B/../" + (char) ('A' + anSize - 1) + "):"); try { String an = sc.next(); if (an.length() < 1) { continue; } char c = an.toUpperCase().charAt(0); if (c < 'A' || c > 'A' + anSize - 1) { continue; } if (sub.correct == c) { sum++; } break; } catch (Exception ex) { sc = new Scanner(System.in); } } } System.out.print("一共" + size + "题,答对" + sum + "题,得分 " + (int) (1D * sum / size * 100) + "分"); }}

取消

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值