当前位置:网站首页>Write a simple examination program to complete the interaction of questions and answers on the console. Questions are divided into single choice and multi choice.
Write a simple examination program to complete the interaction of questions and answers on the console. Questions are divided into single choice and multi choice.
2022-04-22 09:24:00 【Protinx】
Write a simple test program , Complete the problem in the console 、 Interaction of answer questions . test questions (Question) Divided into single choice (SingleChoice) And multiple choice ( MultiChoice) Two kinds of . among , Single choice questions and multiple-choice questions inherit the test questions , As shown in the figure below .
requirement :
1) stay MultiChoice The implementation parameters in the class are ( String text, String[] options, char[] answers) Construction method of . stay SingleChoice Implementation parameters are ( String text, String[] options ,char answer) Construction method of .
2) stay MutiChie and SingleChoice Class Question class chek0 Method , Realize the verification answer method of multiple topics and the verification answer method of single topic respectively .
3) Design test class for examination 、 Answer the questions .
for example , Initialize the following questions :
new MultiQuestion(" Who are the three wonders in the romance of the Three Kingdoms ?",new String[]{"A. Cao Cao ","B. Liu bei ","C. Guan yu ","D. Zhugeliang "},new char[]{'A','C',D''});
new SingleQuestion(" Who first recommended Zhuge Liang to Liu Bei ?",new String[]{"A. Xu Shu ","B. Sima Wei ","C. Lu Su ","D. Guan yu "},'B');
The running effect of the exam is as follows :
Who are the three wonders in the romance of the Three Kingdoms ?
A. Cao Cao B. Liu bei C. Guan yu D. Zhugeliang
Please select :cad
Congratulations , bingo !
Who first recommended Zhuge Liang to Liu Bei ?
A. Xu Shu B. Sima Wei C. Lu Su D. Guan yu
Please select :A
We have to work hard
explain :
1) When the user enters an answer , Case should support . Tips : You can use Character. toUpperCase() Method converts the answer characters entered by the user into uppercase characters, and then compares them ( The standard answer is capital letters ).
2) When judging whether a single choice question is right or wrong , If the user does not answer the question , Or there are more options than 1 Both are considered to be wrong .
3) When judging the correctness of multiple-choice questions , If the user does not answer the question , More options or less than standard answers , Or wrong choices are regarded as wrong answers ( Multiple choice answers need to be entered continuously , There must be no spaces between options 、 Enter and other characters ).
4) Multiple choice questions allow users to enter answers in different order from standard answers , That is, as long as all options are selected correctly .
Tips : You can use Arrays. binarySearch() Method to query whether there is a user's option in the answer array .
import java.util.Scanner; import java.util.Arrays; abstract class Question { String text; String[] options = new String[4]; Question() { } Question(String text, String[] options) { this.text = text; this.options = options; } abstract boolean check(char[] answers); void print() { System.out.println(text); System.out.println(options[0] + " " + options[1] + " " + options[2] + " " + options[3]); System.out.println(" Please choose the right option according to the question :"); } } class SingleQuestion extends Question { char answer; SingleQuestion(String text, String[] options, char answer) { this.text = text; this.options = options; this.answer = answer; } boolean check(char[] answer) { if (answer.length == 1) { if (Character.toUpperCase(answer[0]) == this.answer) return true; else return false; } else return false; } } class MultiQuestion extends Question { char[] answer = new char[4]; MultiQuestion(String text, String[] options, char[] answer) { this.text = text; this.options = options; this.answer = answer; } boolean check(char[] answer) { int count = 0; for (int i = 0; i < answer.length; i++) { int x = Arrays.binarySearch(this.answer, Character.toUpperCase(answer[i])); if (x < 0) return false; else count++; } if (count == 3) return true; else return false; } } public class Test{ public static void main(String[] args) { SingleQuestion s = new SingleQuestion(" Who first recommended Zhuge Liang to Liu Bei ?", new String[] { "A. Xu Shu ", "B. Sima Wei ", "C. Lu Su ", "D. Guan yu " }, 'A'); MultiQuestion m = new MultiQuestion(" Who are the three wonders in the romance of the Three Kingdoms ?", new String[] { "A. Cao Cao ", "B. Liu bei ", "C. Guan yu ", "D. Zhugeliang " }, new char[] { 'A', 'C', 'D' }); m.print(); Scanner sc1 = new Scanner(System.in); String t1 = null; t1 = sc1.nextLine(); char[] a = t1.toCharArray(); if (m.check(a)) System.out.println(" Congratulations , bingo !"); else System.out.println(" We have to work hard !"); s.print(); Scanner sc2 = new Scanner(System.in); String t2 = null; t2 = sc2.nextLine(); char[] b = t2.toCharArray(); if (s.check(b)) System.out.println(" Congratulations , bingo !"); else System.out.println(" We have to work hard !"); } }
版权声明
本文为[Protinx]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220903565782.html
边栏推荐
猜你喜欢

Intersecting linked list (set, double pointer)

网络抖动工具clumsy

【图像隐写】Fixed Neural Network Steganography: Train the images, not the network 整理

Open3D点云处理

The penultimate node in the linked list (sequential search, speed pointer)

Playing with kubernetes - Basic Concepts

Flink流处理引擎系统学习(二)

Halo 开源项目学习(一):项目启动

(CVPR-2014)通过预测 10,000 个类别的深度学习人脸表示

微搭低代码零基础入门课
随机推荐
RHCSA第一天作业
Selection and evolution of microservices under cloud native architecture
Wonderful review | the sixth issue of "source" - open source economy and industrial investment
如何保障缓存和数据库的一致性(超详细案例)
Flink流处理引擎系统学习(三)
Starting from the needs of popular science, doctor training and promotion of innovative medical equipment products, how does "baiyihua" layout medical visualization SaaS services?
【谈思生物直播课】——湖景生物王子元博士关于基因治疗乙肝的探索
Mysql-advanced-5 storage engine
Stream API 优化代码
idea中如何将Services调出并将启动类显示在Services中
在线YAML转Properties工具
Halo 开源项目学习(一):项目启动
Neo4j: merge [create if it doesn't exist, modifiable if it already exists]
Open source, not just coding
[image steganography] fixed neural network steganography: train the images, not the network
详解JS原型与原型链
Raspberry pie Kali system HDMI modifies screen resolution
从科普、医生培训及创新医械产品推广需求出发,「佰医绘」如何布局医学可视化SaaS服务?
Baby naming artifact applet source code_ Support multiple traffic master modes
The request client is not a secure context and the resource is in more-private address ...
