24小时热门版块排行榜    

CyRhmU.jpeg
查看: 1584  |  回复: 17
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

茶家美 Aimee

新虫 (初入文坛)

[交流] 能帮我解决小学生测验编程不?已有12人参与

能帮我看看这个编程题不?谢谢啦

能帮我解决小学生测验编程不?


发自小木虫Android客户端
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

lbstorm

新虫 (初入文坛)

★ ★ ★
小木虫: 金币+0.5, 给个红包,谢谢回帖
jjdg: 金币+2, 辛苦了 2017-05-19 22:34:14
//一共三个java类。写得不好,不足的地方欢迎指正。
// 1.
package com.test.muchong.pupil;

import java.util.Scanner;

/**
* Created by Lieber on 2017/5/19.
*/
public class Check {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int totalScore = 0;
        int questions = 10;
        int answer = 0;

        for (int i = 0; i < questions; i++) {
            int times = 3;
            Question question = new Question();
            DisplayResult.showEquation(question.getEquation());
            DisplayResult.showInput();
            answer = scanner.nextInt();
            boolean flag = false;
            while (! flag) {
                if (times <=3 && times > 1) {
                    flag = question.check(answer);
                    if (!flag) {
                        times--;
                        DisplayResult.showError(times);
                        answer = scanner.nextInt();
                    }
                } else {
                    DisplayResult.showRightAnswer(question.getResult());
                    break;
                }
            }
            totalScore += Check.score(times);

        }
        DisplayResult.showTotalScore(totalScore);

    }

    public static int score(int sco) {
        int result = 0;
        switch (sco) {
            case 3:
                result = 10;
                break;
            case 2:
                result = 7;
                break;
            case 1:
                result = 5;
                break;
            default:
                result = 0;
                break;
        }
        return result;
    }
}


//2.
package com.test.muchong.pupil;

import java.util.Random;

/**
* Created by Lieber on 2017/5/19.
*
* 每一道题:
* 1,生成公式。
* 2,接受输入
* 3,检查结果
* 4,
*/


public class Question {
    private int numberOne;
    private int numberTwo;
    private enum  Symbol { plus, minus};
    private int result;

    private static Random random = new Random();
    private Symbol symbol;

    /**
     * 生成随机整数 (0至50)
     * @return int
     */
    public int getRandomNumber() {
        int number = Math.abs(random.nextInt() % 50);
        return number;
    }


    public void setRandomSymbol() {
        this.symbol = Question.getRandomEnum(Symbol.class);
    }

    /**
     * 生成随机数
     */
    public void init() {
        numberOne = getRandomNumber();
        numberTwo = getRandomNumber();
    }

    public String getEquation() {
        boolean flag = true;
        while (flag) {
            init();
            setRandomSymbol();
            if (symbol.equals(Symbol.plus)) {
                this.result = numberOne + numberTwo;
                if (result > 50) {
                    flag = true;
                } else {
                    flag = false;
                }
            } else {
                this.result = numberOne - numberTwo;
                if ( result> 0 && result < 50) {
                    flag = false;
                } else {
                    flag = true;
                }
            }
        }
        return getString();
    }

    /**
     * 获取公式的表达式
     * @return String
     */
    public String getString() {
        if (symbol.equals(Symbol.plus)) {
            String temp = numberOne + " + " + numberTwo + " = ";
            return temp;
        } else {
            String temp = numberOne + " - " + numberTwo + " = ";
            return temp;
        }
    }

    /**
     * 生成随机符 +号或-号
     */
    public static <T extends Enum<?>> T getRandomEnum(Class<T> clazz) {
        int x = random.nextInt(clazz.getEnumConstants().length);
        return clazz.getEnumConstants()[x];
    }

    /**
     * 获取正确答案
     * @return
     */
    public int getResult() {
        return this.result;
    }

    /**
     * 检查输入
     * @param answer
     * @return
     */
    public boolean check(int answer) {
        if (answer == result) {
            return true;
        } else {
            return false;
        }
    }
}

//3.

package com.test.muchong.pupil;

/**
* Created by Lieber on 2017/5/19.
*
* 显示输出结果:
* 1,输出错误时,提示重新输入
* 2,三次机会用完,给出正确答案
* 3,总分输出。
*/
public class DisplayResult {


    public static void showError(int times) {
            System.out.println("You have given a wrong answer," +
                    "please try again(You have )" + times + " times left:";
    }

    public static void showRightAnswer(int result) {
        System.out.println("The right answer is : " + result);
    }

    public static void showTotalScore(int total) {
        if (total > 90) {
            System.out.println("SMART!";
        } else if (total > 80 && total < 90) {
            System.out.println("GOOD!";
        } else if (total < 80 && total > 70) {
            System.out.println("OK";
        } else if (total < 70 && total > 60){
            System.out.println("PASS";
        } else {
            System.out.println("TRY AGAIN";
        }
    }

    public static void showInput() {
        System.out.println("Please input the answer: ";
    }

    public static void showEquation(String equation) {
        System.out.println(equation);
    }
}
能帮我解决小学生测验编程不?-1
Image1.png


能帮我解决小学生测验编程不?-2
Image2.png


能帮我解决小学生测验编程不?-3
Image3.png

» 本帖已获得的红花(最新10朵)

9楼2017-05-19 14:57:27
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 18 个回答

smitest

木虫 (小有名气)

2楼2017-05-18 23:34:31
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

ioadong

木虫 (著名写手)


小木虫: 金币+0.5, 给个红包,谢谢回帖
你应该到CSDN之类的专业讨论程序设计的论坛。
以科学的理性代替天然的非理性。
3楼2017-05-19 08:37:54
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

ioadong

木虫 (著名写手)

★ ★ ★
小木虫: 金币+0.5, 给个红包,谢谢回帖
jjdg: 金币+2, 说的很对 2017-05-19 22:33:49
忍不住,还是要说下,现实中没有人喜欢你这种一点点自己的想法都没有,直接伸手要答案的。
你真心想找人帮忙也应该自己拿出点可以讨论的东西出来。空手而来只会空手而去。
以科学的理性代替天然的非理性。
4楼2017-05-19 08:54:09
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
普通表情 高级回复(可上传附件)
信息提示
请填处理意见