本文目錄一覽:
- 1、java輸出格式控制的問題
- 2、Java中如何控制右對齊輸出?
- 3、java如何控制數據的輸出格式。
- 4、java怎麼格式化輸出數字
- 5、Java編程,請問怎麼控制浮點數輸出格式,好像沒辦法在輸出里直接寫
java輸出格式控制的問題
package domain;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
public class Test1 {
public Test1() {
ListStudents stuList = new ArrayListStudents();
Students students = new Students();
students.setShowname(“姓名”);
students.setShownumber(“學號”);
students.setShowsex(“性別”);
students.setShowcollege(“學院”);
students.setShowbirthday(“生日”);
stuList.add(students);
students = new Students();
students.setShowname(“fangfang”);
students.setShownumber(“2008020308”);
students.setShowsex(“female”);
students.setShowcollege(“小北”);
students.setShowbirthday(“2008-08-08”);
stuList.add(students);
ListInteger cellLength = doGetCellWidth(stuList);
doPrintInfo(cellLength, stuList);
}
private ListInteger doGetCellWidth(ListStudents stuList) {
//列長度
int nameLong = 0;
ListInteger cellLength = new ArrayListInteger();
for (int i = 0; i 5; i++) {
nameLong = 0;
for (Students students : stuList) {
switch (i) {
case 0:
if (students.getShowname().length() nameLong) {
nameLong = students.getShowname().length();
}
break;
case 1:
if (students.getShownumber().length() nameLong) {
nameLong = students.getShownumber().length();
}
break;
case 2:
if (students.getShowsex().length() nameLong) {
nameLong = students.getShowsex().length();
}
break;
case 3:
if (students.getShowcollege().length() nameLong) {
nameLong = students.getShowcollege().length();
}
break;
case 4:
if (students.getShowbirthday().length() nameLong) {
nameLong = students.getShowbirthday().length();
}
break;
default:
break;
}
}
//列最長長度間隔2
cellLength.add(nameLong+2);
}
return cellLength;
}
private void doPrintInfo(ListInteger cellLength, ListStudents stuList) {
for (Students stu : stuList) {
doPrint(cellLength.get(0), stu.getShowname(), false);
doPrint(cellLength.get(1), stu.getShownumber(), false);
doPrint(cellLength.get(2), stu.getShowsex(), false);
doPrint(cellLength.get(3), stu.getShowcollege(), false);
doPrint(cellLength.get(4), stu.getShowbirthday(), true);
}
}
private void doPrint(int width, String name, boolean nextRow) {
String str = “”;
try {
name = new String(name.getBytes(“gbk”), “iso-8859-1”);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
int nameLength = width – name.length();
if (nameLength 0) {
for (int i = 0; i nameLength; i++) {
str += “#”;
}
}
try {
if (nextRow) {
System.out.println(new String(name.getBytes(“iso-8859-1”), “gbk”)+str.replace(“#”, ” “));
} else {
System.out.print(new String(name.getBytes(“iso-8859-1”), “gbk”)+str.replace(“#”, ” “));
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Test1();
}
}
最後說明一下,java後台輸出的話,漢字後面的空格會變短。
Java中如何控制右對齊輸出?
Java中控制右對齊輸出的方法有以下:
1、你可以把數字轉換成字符串,用 String.format(“% 4d”, number1); 可以補充空格。有個更好的方法用printf();這個給C中的方法差不多,很方便例如這樣:System.out.printf(“%-10s”,”abc”); //輸出10列,左對齊(-號表示左對齊);System.out.printf(“%8d”,23); //輸出8列, 右對齊。
2、你可以把數字轉換成字符串,用 String.format(“% 4d”, number1); 可以補充空格,這樣行
有個更好的方法用printf();這個給C中的方法差不多,很方便例如這樣:System.out.printf(“%-10s”,”abc”);System.out.printf(“%8d”,23);System.out.println();System.out.printf(“%-10s”,”ab”);System.out.printf(“%8d”,23); 就可以了,這只是個例子,具體情況怎麼實現看你自己。
java如何控制數據的輸出格式。
使用 System.out.printf(); 方法控制數據的輸出格式
如:
double number = 234.234 ;
System.out.printf(“%6.2f” , number);
程序運行結果 : 234.23
java怎麼格式化輸出數字
使用System.out.printf(格式化字符串,參數)
int a = 5;
數字的話System.out.printf(“%d”,a);
//”%”表示進行格式化輸出,”%”之後的內容為格式的定義。
System.out.printf(“%f”,d);//”f”表示格式化輸出浮點數。
System.out.println();
System.out.printf(“%9.2f”,d);//”9.2″中的9表示輸出的長度,2表示小數點後的位數。
System.out.println();
System.out.printf(“%+9.2f”,d);//”+”表示輸出的數帶正負號。
System.out.println();
System.out.printf(“%-9.4f”,d);//”-“表示輸出的數左對齊(默認為右對齊)。
System.out.println();
System.out.printf(“%+-9.3f”,d);//”+-“表示輸出的數帶正負號且左對齊。
System.out.println();
System.out.printf(“%d”,i);//”d”表示輸出十進制整數。
System.out.println();
System.out.printf(“%o”,i);//”o”表示輸出八進制整數。
System.out.println();
System.out.printf(“%x”,i);//”d”表示輸出十六進制整數。
System.out.println();
System.out.printf(“%#x”,i);//”d”表示輸出帶有十六進制標誌的整數。
System.out.println();
System.out.printf(“%s”,s);//”d”表示輸出字符串。
System.out.println();
System.out.printf(“輸出一個浮點數:%f,一個整數:%d,一個字符串:%s”,d,i,s);
//可以輸出多個變量,注意順序。
System.out.println();
System.out.printf(“字符串:%2$s,%1$d的十六進制數:%1$#x”,i,s);
//”X$”表示第幾個變量。
Java編程,請問怎麼控制浮點數輸出格式,好像沒辦法在輸出里直接寫
先將浮點數轉為字符串,再根據小數點分割成兩部分,最後根據兩部分的長度來決定前面和後面怎麼加0即可。
原創文章,作者:LFDH,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/142735.html