首页 > 解决方案 > 无论如何要压缩我当前的代码,使其不是一长行(不制作单独的行)

问题描述

我目前正在为一个 Java 类项目工作,我们必须打印出我们的日程安排。我已经让我的代码看起来像我想要的最终代码的样子。我们只能使用一个 System.out.print() 这样我们就可以练习使用转义序列了。但是,我的老师说我需要压缩我的代码行并找到一种不让它拖下去的方法。任何帮助,将不胜感激。

这是我当前的代码:

class Main {
    public static void main(String[] args) {
        System.out.println("Period\t\t\t\tClass\t\t\t\t\tComment\n1\t\t\t\t\tPrecalculus\t\t\t\tBoring\n2\t\t\t\t\tJava\t\t\t\t\tCool\n3\t\t\t\t\tHonors Chinese\t\t\tInteresting\n4\t\t\t\t\tGym\t\t\t\t\t\tIrrelevant\n5\t\t\t\t\tEnglish\t\t\t\t\tEasy\n6\t\t\t\t\tAP World\t\t\t\tHard\n7\t\t\t\t\tAP Physics\t\t\t\tAlso Hard\n8\t\t\t\t\tVoices\t\t\t\t\tExciting");
    }
}

标签: javaprintln

解决方案


  1. 最好将数据表示为(类的)对象。
  2. 可以根据需要打印对象

下面的代码将

  1. 定义一个新类Schedule来表示记录
  2. 计划可以覆盖toString方法来打印其内部状态
  3. 如果需要toString可以使用Formatter
  4. 创建多个Schedule对象并使用toString
  5. 在打印实际对象之前打印列(这是我的实现稍微偏离轨道的地方)
import java.util.ArrayList;
import java.util.List;

public class ScheduleFormatter {

    static class Schedule {
        public static final String FORMAT = "%-15s %-20s %s";
        private final int period;
        private final String className;
        private final String comment;

        public Schedule(int period, String className, String comment) {
            this.period = period;
            this.className = className;
            this.comment = comment;
        }

        @Override
        public String toString() {
            return String.format(FORMAT, this.period, this.className, this.comment);
        }
    }

    public static void main(String[] args) {
        List<Schedule> schedules = new ArrayList<>();
        schedules.add(new Schedule(1, "Precalculus", "Boring"));
        schedules.add(new Schedule(2, "Java", "Cool"));
        System.out.println(String.format(Schedule.FORMAT, "Period", "Class", "Comment"));
        schedules.forEach(System.out::println);
    }
}

甚至 List 也可以包含在另一个对象中,并且该容器对象可以覆盖toString.

import java.util.ArrayList;
import java.util.List;

public class ScheduleFormatter {

    static class Schedule {
        public static final String FORMAT = "%-15s %-20s %s\n";
        private final int period;
        private final String className;
        private final String comment;

        public Schedule(int period, String className, String comment) {
            this.period = period;
            this.className = className;
            this.comment = comment;
        }

        @Override
        public String toString() {
            return String.format(FORMAT, this.period, this.className, this.comment);
        }
    }

    static class Schedules {
        private String[] headers;
        private List<Schedule> schedules;

        Schedules(String[] headers) {
            this.headers = headers;
            this.schedules = new ArrayList<>();
        }

        public void add(Schedule schedule) {
            this.schedules.add(schedule);
        }

        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append(String.format(Schedule.FORMAT, headers));
            schedules.forEach(builder::append);
            return builder.toString();
        }
    }

    public static void main(String[] args) {
        Schedules schedules = new Schedules(new String[] {"Period", "Class", "Comment"});
        schedules.add(new Schedule(1, "Precalculus", "Boring"));
        schedules.add(new Schedule(2, "Java", "Cool"));
        System.out.println(schedules);
    }
}

推荐阅读