如何将输出格式化为 Java 中的 table?
How to format outputs as a table in Java?
我正在尝试制作一份 table 成绩单,应该有 5 列,即 Class、描述、学分、成绩和成绩点。
但是,我无法使输出相互对齐。
我只是简单地使用“\t”。
有没有办法在 Java 中将输出设为 table?
import java.util.*;
public class Project1_Trial2 {
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
//Input the term
System.out.println("Please enter the term of your grade calculation (for example, Fall 2015): ");
String term = scanner.nextLine();
//Input the number of courses that the student is enrolled in
System.out.println("Please enter the number of courses that you are enrolled in "+term+": ");
int numberofcourses = scanner.nextInt();
//Declaration
String ClassName[] = new String[numberofcourses];
String Description[] = new String[numberofcourses];
String Grade[] = new String[numberofcourses];
int Units[] = new int[numberofcourses];
//Arrays for class number, description, units, grade, grades point
//Here, input class number, description, units, and grades
for(int i = 0; i < numberofcourses; i++)
{
scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class name: ");
ClassName[i] = scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class description: ");
Description[i] = scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class units: ");
Units [i] = scanner.nextInt();
System.out.println("Please enter your #"+(i+1)+" class grade: ");
Grade[i] = scanner.nextLine();
}
System.out.println("Class Grades - "+term+" Term");
System.out.println("Office Grades");
System.out.println("Class \t \t Description \t \t Units \t \t Grade \t \t Grade Points");
for(int i = 0; i < numberofcourses; i++)
{
System.out.println(ClassName[i]+"\t \t"+Description[i]+"\t \t"+Units[i]+"\t \t"+Grade[i]);
}
}
}
有多种方法可以对齐您的输出,使它们均匀对齐....一种方法是替换您放置的位置:
System.out.println(ClassName[i]+"\t \t"+Description[i]+"\t \t"+Units[i]+"\t \t"+Grade[i]);
与:
System.out.printf("%-15s %-15s %-15s ",ClassName[i],Description[i],String.format("%5d", Units[i]),Grade[i]);
而不是使用 System.out.println()
使用 System.out.printf()
这样你可以填充字符串并在 printf()
函数中使用 String.format()
用空格填充整数......你可以调整我的填充到任何你觉得合适的
这是一种比不断使用制表符更有效的方法
我正在尝试制作一份 table 成绩单,应该有 5 列,即 Class、描述、学分、成绩和成绩点。 但是,我无法使输出相互对齐。 我只是简单地使用“\t”。 有没有办法在 Java 中将输出设为 table?
import java.util.*;
public class Project1_Trial2 {
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
//Input the term
System.out.println("Please enter the term of your grade calculation (for example, Fall 2015): ");
String term = scanner.nextLine();
//Input the number of courses that the student is enrolled in
System.out.println("Please enter the number of courses that you are enrolled in "+term+": ");
int numberofcourses = scanner.nextInt();
//Declaration
String ClassName[] = new String[numberofcourses];
String Description[] = new String[numberofcourses];
String Grade[] = new String[numberofcourses];
int Units[] = new int[numberofcourses];
//Arrays for class number, description, units, grade, grades point
//Here, input class number, description, units, and grades
for(int i = 0; i < numberofcourses; i++)
{
scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class name: ");
ClassName[i] = scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class description: ");
Description[i] = scanner.nextLine();
System.out.println("Please enter your #"+(i+1)+" class units: ");
Units [i] = scanner.nextInt();
System.out.println("Please enter your #"+(i+1)+" class grade: ");
Grade[i] = scanner.nextLine();
}
System.out.println("Class Grades - "+term+" Term");
System.out.println("Office Grades");
System.out.println("Class \t \t Description \t \t Units \t \t Grade \t \t Grade Points");
for(int i = 0; i < numberofcourses; i++)
{
System.out.println(ClassName[i]+"\t \t"+Description[i]+"\t \t"+Units[i]+"\t \t"+Grade[i]);
}
}
}
有多种方法可以对齐您的输出,使它们均匀对齐....一种方法是替换您放置的位置:
System.out.println(ClassName[i]+"\t \t"+Description[i]+"\t \t"+Units[i]+"\t \t"+Grade[i]);
与:
System.out.printf("%-15s %-15s %-15s ",ClassName[i],Description[i],String.format("%5d", Units[i]),Grade[i]);
而不是使用 System.out.println()
使用 System.out.printf()
这样你可以填充字符串并在 printf()
函数中使用 String.format()
用空格填充整数......你可以调整我的填充到任何你觉得合适的
这是一种比不断使用制表符更有效的方法