英文:
display data java from arraylist in column
问题
我想知道如何从一个数组列表中按列(依次排列)获取元素,就像这样:
BMI: 长度: 重量:
1.69 30 24.25
2.58 55 28.25
这是我的代码:
@Override
public String toString() {
return String.format("%-17.1f%-15.2f%.2f", lengte, gewicht, bmi);
}
public static void main(String[] args){
ArrayList<BMICalculator> bmilist = new ArrayList<>();
JOptionPane.showMessageDialog(null, String.format("length:%-9sgewicht:%-8sbmi:", "", "") );
do {
double lengte = getLength("Geef de lengte in Meters:");
double gewicht = getGewicht("Geef het gewicht in Kg:");
BMICalculator bmi = new BMICalculator(lengte, gewicht);
bmi.setBmi(bmi.calculateBMI());
bmilist.add(bmi);
JOptionPane.showMessageDialog(null, String.format("%s", bmi.toString()));
} while(getUserAnswer() == 'J');
JOptionPane.showMessageDialog(null, String.format("%s", bmilist.toString()));
System.out.println(bmilist);
}
这段代码给我这个结果:
长度: 重量: BMI:
1.7 80.00 28.01, 长度: 重量: BMI:
1.6 55.00 22.03
我不希望长度、重量和BMI一直重复出现...
帮帮我!
英文:
I would like to know how am I supposed to get the elements from a arraylist in a column(under each other) like this:
BMI: Length: gewicht:
1.69 30 24.25
2.58 55 28.25
This is my code:
@Override
public String toString() {
return String.format("%-17.1f%-15.2f%.2f",lengte, gewicht,bmi);
}
public static void main(String[]args){
ArrayList<BMICalculator> bmilist = new ArrayList<>();
JOptionPane.showMessageDialog(null, String.format("length:%-9sgewicht:%-8sbmi:","","") );
do {
double lengte = getLength("Geef de lengte in Meters:");
double gewicht = getGewicht("Geef het gewicht in Kg:");
BMICalculator bmi = new BMICalculator(lengte, gewicht);
bmi.setBmi(bmi.calculateBMI());
bmilist.add(bmi);
JOptionPane.showMessageDialog(null, String.format("%s", bmi.toString()));
} while(getUserAnswer() == 'J');
JOptionPane.showMessageDialog(null, String.format("%s", bmilist.toString()));
System.out.println(bmilist);
}
This code gives me this:
length: gewicht: Bmi:
1,7 80,00 28,01, length: gewicht: bmi:
1,6 55,00 22,03
I dont want that length, gewicht and bmi keeps repeating...
help!!!
答案1
得分: 1
你的标题和数据在String.format
掩码中合并在一起。这就是为什么标题会重复出现。
你需要将它们分开。所以在循环之前,使用String.format("length:%-9sgewicht:%-8sbmi:", "", "")
打印一次标题。对于每行数据,使用String.format("%-17.1f%-15.2f%.2f",lengte, gewicht,bmi)
。我认为这样你就会没问题。
但是,如果你想要将整个文本显示在同一个窗格中,可以像这样做:
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
ArrayList<BMICalculator> bmilist = new ArrayList<>();
sb.append(String.format("length:%-9sgewicht:%-8sbmi:\n", "", ""));
do {
double lengte = getLength("Geef de lengte in Meters:");
double gewicht = getGewicht("Geef het gewicht in Kg:");
BMICalculator bmi = new BMICalculator(lengte, gewicht);
bmi.setBmi(bmi.calculateBMI());
bmilist.add(bmi);
} while (getUserAnswer() == 'J');
for (BMICalculator item : bmilist) {
sb.append( item.toString() );
}
JOptionPane.showMessageDialog(null, sb.toString());
System.out.println(bmilist);
}
英文:
Your header and data are merged together in that String.format
mask. That's why the header keeps repeating.
You'll need to separate them. So use String.format("length:%-9sgewicht:%-8sbmi:","","")
to print the header once, before the loop. And for each line of data use String.format("%-17.1f%-15.2f%.2f",lengte, gewicht,bmi)
. That way I think you will be fine.
But, if what you want is the whole text into the same pane, it would be something like this:
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
ArrayList<BMICalculator> bmilist = new ArrayList<>();
sb.append(String.format("length:%-9sgewicht:%-8sbmi:\n", "", ""));
do {
double lengte = getLength("Geef de lengte in Meters:");
double gewicht = getGewicht("Geef het gewicht in Kg:");
BMICalculator bmi = new BMICalculator(lengte, gewicht);
bmi.setBmi(bmi.calculateBMI());
bmilist.add(bmi);
} while (getUserAnswer() == 'J');
for (BMICalculator item : bmilist) {
sb.append( item.toString() );
}
JOptionPane.showMessageDialog(null, sb.toString());
System.out.println(bmilist);
}
答案2
得分: 1
import java.util.*;
public class Main {
static class Friend {
String name;
int age;
String phone;
public Friend(String name, int age, String phone) {
this.name = name;
this.age = age;
this.phone = phone;
}
@Override
public String toString() {
return String.format("%-15s %-4s %10s", name, "" + age, phone);
}
}
public static void main(String[] args) {
ArrayList<Friend> friends = new ArrayList();
friends.add(new Friend("F11", 12, "phone11"));
friends.add(new Friend("F21", 21, "phone21"));
friends.add(new Friend("F321", 123, "phone321"));
printFriends(friends);
}
private static void printFriends(ArrayList<Friend> friends) {
printHeader();
friends.forEach(friend -> System.out.println(friend.toString()));
}
private static void printHeader() {
System.out.println(String.format("%-15s %-4s %10s", "Name", "Age", "Phone"));
}
}
输出:
Name Age Phone
F11 12 phone11
F21 21 phone21
F321 123 phone321
尝试调整字符串格式和间距以获得所需的列间距。
英文:
import java.util.*;
public class Main {
static class Friend {
String name;
int age;
String phone;
public Friend(String name, int age, String phone) {
this.name = name;
this.age = age;
this.phone = phone;
}
@Override
public String toString() {
return String.format("%-15s %-4s %10s", name, "" + age, phone);
}
}
public static void main(String[] args) {
ArrayList<Friend> friends = new ArrayList();
friends.add(new Friend("F11", 12, "phone11"));
friends.add(new Friend("F21", 21, "phone21"));
friends.add(new Friend("F321", 123, "phone321"));
printFriends(friends);
}
private static void printFriends(ArrayList<Friend> friends) {
printHeader() ;
friends.forEach(friend -> System.out.println(friend.toString()));
}
private static void printHeader() {
System.out.println(String.format("%-15s %-4s %10s","Name","Age", "Phone"));
}
}
Output:
Name Age Phone
F11 12 phone11
F21 21 phone21
F321 123 phone321
Play with String format and spacing to get desired column spacing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论