码迷,mamicode.com
首页 > 编程语言 > 详细

java学习第十一天2020/7/16

时间:2020-07-17 09:30:05      阅读:54      评论:0      收藏:0      [点我收藏+]

标签:false   text   pack   console   next   util   class   继承   div   

一.

今天大多在写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]+"\t");
		}
		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("不正确");
		}
	}

}

 二.思路还没理清

三.复习.看下一章

java学习第十一天2020/7/16

标签:false   text   pack   console   next   util   class   继承   div   

原文地址:https://www.cnblogs.com/qiangini/p/13326523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!