• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
戈瑾
博客园    首页    新随笔    联系   管理    订阅  订阅
Java入门——day14
练习

一、今日学习

1.综合实例

试卷选择题

 1 package com.wuzy;
 2 import java.util.Arrays;
 3 import java.util.Scanner;
 4 /**选择题*/
 5 class Question {
 6     /** 题干*/
 7     String text;
 8     /** 选项 */
 9     String[] options;
10     /** 打印当前题目*/
11     public void print() {
12         System.out.println(this.text);
13         for(int i=0;i<this.options.length;i++) {
14             System.out.print(options[i]+"\t");
15         }
16         System.out.println();
17     }
18     /**检查答案*/
19     public boolean check(char[] answers) {
20         return false;
21     }
22 }
23 /**单选题继承于选择题,单选题会继承选择题的属性和方法*/
24 class SingleQuestion extends Question{
25     /**唯一的单选标准答案*/
26     char answer;
27     /**构建一个单选题实例,依赖:题干,选项和一个标准答案*/
28     public SingleQuestion(String text,String[] options,char answer) {
29         this.text=text;
30         this.options=options;
31         this.answer=answer;
32     }
33     /**覆盖父类型的方法,提供具体的检查答案的过程*/
34     public boolean check(char[] answers) {
35         if(answers==null||answers.length!=1)
36             return false;
37         return this.answer==answers[0];
38     }
39 }
40 /**多项选择题*/
41 class MultiQuestion extends Question{
42     /**多选题标准答案*/
43     char[] answers;
44     public MultiQuestion(String text,String[] options,char[] answers) {
45         this.text=text;
46         this.options=options;
47         this.answers=answers;
48     }
49     /**覆盖检查答案的方法,实现多选题的具体检查用户答案逻辑*/
50     public boolean check(char[] answers) {
51         return Arrays.equals(answers, this.answers);
52     }
53 }
54 
55 public class PaperDemo {
56     public static void main(String[] args) {
57         //paper考卷,是选择题的集合(数组)
58         Question[] paper= {null,null};
59         paper[0]=new SingleQuestion("如何买火车票靠谱?",new String[]{"A.电话","B.网上","C.黄牛","D.画的"},'A');
60         paper[1]=new MultiQuestion("哪几位是歌手?",new String[] {"A.刘德华","B.张学友","C.郭富城","D.孙悟空"},new char[] {'A','B','C'});
61         Scanner console=new Scanner(System.in);
62         for(int i=0;i<paper.length;i++) {
63             Question q=paper[i];
64             q.print(); //打印试卷
65             System.out.print("请选择:");
66             String str=console.nextLine(); //等待用户答案
67             char[] answers=str.toCharArray(); //转化答案为数组
68             if(q.check(answers)) {
69                 System.out.println("给力呀!");
70             }else {
71                 System.out.println("亲,要努力呀!");
72             }
73         }
74         }
75 }

 

 

 

二、遇到问题

1.警告:The value of the local variable q is not used

查阅发现,编译器是实时判断代码是否有错的,当写到目前这个对象创建的语句时,如果还没有对这个对象进行操作,编译器认为这是浪费内存的行为,所以给出警告,如果在后续的代码中如果用到了这个对象的话,这个警告就会消失。

2.写了mian函数却仍然报错

 

 

三、明日学习

练习四五章

posted on 2020-07-19 21:32  戈瑾  阅读(150)  评论(2)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3