簡體   English   中英

Java(控制台)列,每列具有一種以上的數據類型

[英]Java (console) columns with more than one data type per column

我首先要說這是一個家庭作業項目(幾周前提交),從頭開始。 我的成績不錯,但我想知道我是否可以做得更好。

我們必須以以下格式輸出員工的姓名,年齡,工資和獎金:

Name        Age     Salary      Bonus
Xxxxxx      xx      $ xxxxx.xx  $ xxxx.xx

我們不允許使用循環,而只能輸出一名員工。

現在這就是我的輸出方式:

String employeeSalary = String.format("%.2f", myEmployee.getSalary());
String employeeBonus = String.format("%.2f", myEmployee.calculateBonus());

System.out.printf("\f%-10s %-10s %-12s %-10s\n", "Name", "Age", "Salary", "Bonus");
System.out.printf("%-10s %-10s %-12s %-10s", myEmployee.getName(), myEmployee.getAge(), "$ " + employeeSalary, "$ " + employeeBonus);

謝謝,

邁克:)

您的示例表明您已經了解String類中的format方法。 因此,您可以使用它來格式化整個輸出,而不是同時使用formatprintf方法。

System.out.println(String.format("%-10s %-10s $ %-12.2f $ %-10.2f", 
                    myEmployee.getName(), myEmployee.getAge(), 
                    myEmployee.getSalary(), myEmployee.calculateBonus()));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM