java - 无论如何要压缩我当前的代码,使其不是一长行(不制作单独的行)
问题描述
我目前正在为一个 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");
}
}
解决方案
- 最好将数据表示为(类的)对象。
- 可以根据需要打印对象
下面的代码将
- 定义一个新类
Schedule
来表示记录 - 计划可以覆盖
toString
方法来打印其内部状态 - 如果需要
toString
可以使用Formatter
- 创建多个
Schedule
对象并使用toString
- 在打印实际对象之前打印列(这是我的实现稍微偏离轨道的地方)
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);
}
}
推荐阅读
- scala - Kubernetes kafka - lagom - 消费者在超时后因 WakeupException 而中断。消息:空
- ios - view.addSubview(: ) Not working in swift playgrounds
- asp.net-core - Automapper v8 不忽略 EF(实体框架)类的导航属性
- arrays - 在 VBA 中存储字符串并输出到多个单元格
- sql - 将商或更长的表达式分配给局部(实)变量 T SQL
- json - 快速解析 JSON。解析json文件的最佳结构是什么
- javascript - 单击按钮添加到 field_tag rails jQuery
- facebook-graph-api - 评论计数 API Facebook Restfb Java
- firebase - 使用 GCP VPN 保护 Firebase 托管网站
- c# - 使用 PrintDialog 打印功能区