首页 > 解决方案 > 在java中存储数据问题

问题描述

我刚刚编写了一些代码来在 java 中形成一些统计信息:

public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        byte[]  weekEval = new byte[18];
        String  weekMatch = "";
        byte    order = -1;

        while (++order < 18) {
            byte    tmpRes = 10;
            byte    count = 5;
            if (sc.hasNextByte())
                weekMatch += sc.nextByte();
            Scanner scInScope = new Scanner(weekMatch);

            weekMatch = scInScope.findInLine("Week \\d+");
            if (weekMatch == null) {
                System.err.println("Illegal Argument");
                sc.close();
                System.exit(-1);
            }
            String  orderMatch = "Week " + (order + 1);
            if (!orderMatch.equals(weekMatch)) {
                System.err.println("Illegal argument");
                sc.close();
                System.exit(-1);
            }
            weekMatch = "";
            for (byte readData = 0; readData < 5; ++readData) {
                if (sc.hasNext())
                    weekMatch += sc.next();
                else {
                    System.err.println("Illegal argument");
                    sc.close();
                    scInScope.close();
                    System.exit(-1);
                }
                weekMatch += ' ';
            }
            scInScope = new Scanner(weekMatch);
            weekMatch = scInScope.findInLine("\\d \\d \\d \\d \\d");
            if (weekMatch == null) {
                System.err.println("Illegal Argument");
                sc.close();
                System.exit(-1);
            }
            scInScope = new Scanner(weekMatch);
            while (count-- > 0) {
                tmpRes = scInScope.nextByte();
                if (weekEval[order] > tmpRes)
                    weekEval[order] = tmpRes;
            }
        }
        for (int i = 0; i < 18 && weekEval[i] != 10; ++i) {
            System.out.print("Week " + (i + 1) + ' ');
            while (weekEval[i]-- > 0)
                System.out.printf("=");
            System.out.println(">");
        }
        sc.close();

它有效,但我需要在没有数组的情况下完成任务。好的,我至少有字符串,对吧?错误的。禁止循环内的任何字符串连接。我被允许使用的唯一字符串方法是 string.equals。还有system.out、system.err和scanner(System.in)。我的想法用完了,我可以使用什么技巧......

标签: java

解决方案


好吧,首先,我想为没有发布主题和如此不清楚的问题而道歉。所以接下来的任务是:

客户将此进度评估为每周五次测试的最低等级。每个测试可以在 1 到 9 之间进行评分。

分析的最大周数为 18。一旦程序获得了每周的信息,它会在控制台上显示图表以显示特定周的最低分数。

我们一直假设 42 是输入数据限制。一周内确切保证的测试次数为 5。

但是,每周数据输入的顺序并不能保证,所以第 1 周的数据可以在第 2 周的数据之后输入。如果数据输入顺序错误,将显示 IllegalArgument 消息,并以 -1 代码关闭程序。

问题出在非常混乱的笔记中

笔记:

• 存储信息有很多选择,数组只是其中之一。应用另一种方法来存储有关学生测试的数据,而不使用数组。

• 字符串连接通常会导致意外的程序行为。如果单个变量的循环中有多次串联操作迭代,则应用程序可能会显着减慢。这就是为什么我们不应该在循环中使用字符串连接来生成结果。

在之前,'->' 代表用户输入。所以,总的来说,它应该看起来像:

-> Week 1
-> 4 5 2 4 2
-> Week 2
-> 7 7 7 7 6
-> Week 3
-> 4 3 4 9 8
-> Week 4
-> 9 9 4 6 7
-> 42
Week 1 ==>
Week 2 ======>
Week 3 ===>
Week 4 ====>

核心技巧隐藏在 java.utils.scanner 和一些非常方便的正则表达式公式中。最终解决方案如下

import java.util.Scanner;

public class Program {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        byte    roll = 0;
        long    multModif = 1;
        long    result = 0;

        sc.useDelimiter("\\u000A");
        while (roll < 18) {
            byte    tool = 0;

            if (sc.hasNext("(42)|(Week(\\s)(\\d))")) {
                sc.useDelimiter("\\s+");

                String verify = sc.next();
                if (verify.equals("42"))
                    break ;
                tool = sc.nextByte();
                sc.useDelimiter("\\u000A");
            }
            else {
                System.err.println("Illegal Argument");
                sc.close();
                System.exit(-1);
            }
            if (tool != ++roll) {
                System.err.println("Illegal Argument");
                sc.close();
                System.exit(-1);
            }
            tool = 10;
            if (sc.hasNext("(\\d)(\\s)(\\d)(\\s)(\\d)(\\s)(\\d)(\\s)(\\d)")) {
                sc.useDelimiter("\\s+");
                for (byte i = 0; i < 5; ++i) {
                    byte toolTmp = sc.nextByte();
                    if (tool > toolTmp)
                        tool = toolTmp;
                }
                sc.useDelimiter("\\u000A");
            } else {
                System.err.println("Illegal Argument");
                sc.close();
                System.exit(-1);
            }
            result += tool * multModif;
            multModif *= 10;
        }
        for (byte i = 0; i < roll; ++i) {
            byte weekRes;

            weekRes = (byte)(result % 10);
            result /= 10;
            System.out.print("Week ");
            System.out.print(i + 1);
            System.out.print(' ');
            for (byte j = 0; j < weekRes; ++j)
                System.out.print('=');
            System.out.println('>');
        }
    }
}

我通过了一些测试,但坦率地说,并不是 100% 确定我的解决方案的准确性,但它可以工作并满足上面提到的要求。你们寻求帮助,非常抱歉这个令人困惑的问题......


推荐阅读