大家早上好,
我被指派在java(初学者)上显示一个双面表。我能够获得表格的主体,但我在编码以打印表格标题时遇到问题。第一列应该有“Miles”作为标题,第二列应该有“公里”作为标题。如何打印?
下面是我为表输出所做的代码。
//This program will display 2 tables side by side
//With the use of for or while loop structure
import java.util.Scanner;
public class Lab5
{
public static void main(String[] args)
{
final double MILES_PER_KM = 1.61; // miles per km
double kilometers = MILES_PER_KM;
for (int miles = 1; miles <= 11; miles +=2)
{
double km = miles * MILES_PER_KM;
String s = String.format("%3d\t%3.2f", miles, miles*MILES_PER_KM);
System.out.println(s);
}
}//main
}//class
答案 0 :(得分:1)
我只会使用printf()
之类的东西,
final double MILES_PER_KM = 1.61; // miles per km
System.out.printf("%s\t%s%n", "Miles/hr", "Km/hr");
for (int miles = 1; miles <= 11; miles += 2) {
double km = miles * MILES_PER_KM;
System.out.printf("%3d\t\t%3.2f%n", miles, miles * MILES_PER_KM);
}