java学习第十一天2020/7/16
一.
今天大多在写c++的程序报告
此外通过一个程序再对继承有一个了解
public class Question { String text; String [] options; public void print() { System.out.println(this.text); for(int i=0;i<this.options.length;i++) { System.out.println(options[i]+" "); } System.out.println(); } public boolean check(char[] answers) { return false; } } public class SingleQuestion extends Question{ char answer; public SingleQuestion(String text,String[] options,char answer) { this.text=text; this.options=options; this.answer=answer; } public boolean check(char[] answer) { if(answer==null||answer.length!=1) return false; return this.answer==answer[0]; } } import java.util.Arrays; public class MultiQuestion extends Question{ char[] answers; public MultiQuestion(String text,String[] options,char[] answers) { this.text=text; this.options=options; this.answers=answers; } public boolean check(char[] answers) { return Arrays.equals(answers, this.answers); } } package main函数; import java.util.Scanner; public class PaperDemo { @SuppressWarnings("resource") public static void main (String[] args) { Question[] paper= {null,null}; paper[0]=new SingleQuestion("谁是司机?",new String[] {"A.小明","B.小李","C.小张","D.小红"},‘B‘); paper[0]=new MultiQuestion("谁是司机(DC)?",new String[] {"A.小明","B.小李","C.小张","D.小红"},new char[] {‘B‘,‘C‘}); Scanner console=new Scanner(System.in); for(int i=0;i<paper.length;i++) { Question q=paper[i]; q.print(); System.out.print("请选择:"); String str=console.nextLine(); char[] answers=str.toCharArray(); if(q.check(answers)) { System.out.println("正确"); } else System.out.println("不正确"); } } }