数组列表中的元素出现在新行上
Elements in Array List to appear on a new line
我的 ArrayList 每次循环时都会添加一个 String
,但是它显示在一条大线上。如何使每个 String
在循环时出现在 JOptionPane 中的新行上?
这是我的举报方式:
public String report()
{
return(name + "\t" + height + " inches\t" + weight + " pounds\tBMI: "
+ df.format(bmiValue()) + "\t" + bmiStatus() );
}
这里是 ArrayList 的代码:
ArrayList a = new ArrayList();
if(JFileChooser.APPROVE_OPTION)
{
File f = choose.getSelectedFile();
Scanner s = new Scanner(new FileInputStream(f));
int l = scanner.nextInt();
for(int i = 0; i < l; i++)
{
int height = s.nextInt();
int weight = s.nextInt();
String name = s.nextLine();
BmiRecord r = new BmiRecord(name, height, weight);
a.add(r.report());
}
confirm = JOptionPane.showConfirmDialog(null, a, "BMI Calc",
JOptionPane.YES_NO_OPTION);
你的 report() 函数应该是:
public String report()
{
return(name + "\t" + height + " inches\t" + weight + " pounds\tBMI: "
+ df.format(bmiValue()) + "\t" + bmiStatus() + "\n");
}
注意我添加了 \n
,它在 report() 函数返回的字符串末尾添加了一个新行。
另外 ArrayList a = new ArrayList();
应更改为 ArrayList<String> a = new ArrayList<String>();
使用 ArrayList 的 String 参数可确保类型安全,因为 ArrayList 只能容纳 String 对象,而 ArrayList 是原始类型,可以容纳任何对象并且类型不安全。
我的 ArrayList 每次循环时都会添加一个 String
,但是它显示在一条大线上。如何使每个 String
在循环时出现在 JOptionPane 中的新行上?
这是我的举报方式:
public String report()
{
return(name + "\t" + height + " inches\t" + weight + " pounds\tBMI: "
+ df.format(bmiValue()) + "\t" + bmiStatus() );
}
这里是 ArrayList 的代码:
ArrayList a = new ArrayList();
if(JFileChooser.APPROVE_OPTION)
{
File f = choose.getSelectedFile();
Scanner s = new Scanner(new FileInputStream(f));
int l = scanner.nextInt();
for(int i = 0; i < l; i++)
{
int height = s.nextInt();
int weight = s.nextInt();
String name = s.nextLine();
BmiRecord r = new BmiRecord(name, height, weight);
a.add(r.report());
}
confirm = JOptionPane.showConfirmDialog(null, a, "BMI Calc",
JOptionPane.YES_NO_OPTION);
你的 report() 函数应该是:
public String report()
{
return(name + "\t" + height + " inches\t" + weight + " pounds\tBMI: "
+ df.format(bmiValue()) + "\t" + bmiStatus() + "\n");
}
注意我添加了 \n
,它在 report() 函数返回的字符串末尾添加了一个新行。
另外 ArrayList a = new ArrayList();
应更改为 ArrayList<String> a = new ArrayList<String>();
使用 ArrayList 的 String 参数可确保类型安全,因为 ArrayList 只能容纳 String 对象,而 ArrayList 是原始类型,可以容纳任何对象并且类型不安全。