英文:
arraylist in java, getting all the data print out
问题
我现在正在学习Java,我试图在我的项目中使用`ArrayList`,但是它不起作用。请帮忙...
public class TestCalculator {
public static void main(String[] args){
ArrayList<BMICalculator> bmilist = new ArrayList<>();
//ArrayList<Double> bmilist = new ArrayList<Double>();
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);
for(double i = 0; i < bmilist.size(); i++){
//System.out.println(i);
JOptionPane.showMessageDialog(null, "List:" + i);
}
JOptionPane.showMessageDialog(null, "Bmi: " + String.format("%s", bmi.toString()));
} while(getUserAnswer() == 'J');
}
public static double getLength(String message){
String lengte = JOptionPane.showInputDialog(message);
return Double.parseDouble(lengte);
}
public static double getGewicht(String message){
String gewicht = JOptionPane.showInputDialog(message);
return Double.parseDouble(gewicht);
}
public static char getUserAnswer(){
String answer = JOptionPane.showInputDialog("Wil je opnieuw: j/n?");
return Character.toUpperCase(answer.charAt(0));
}
}
我无法从那个列表中获得任何输出。每次有人计算他的BMI时,它应该将它们放入一个`ArrayList`,当他们点击“继续”时,不...然后它应该返回他们计算过的所有BMI。
英文:
I am learning Java right now and I am trying to use ArrayList
in my project, but it doesn't work. Help please...
public class TestCalculator {
public static void main(String[]args){
ArrayList<BMICalculator> bmilist = new ArrayList<>();
//ArrayList<Double> bmilist = new ArrayList<Double>();
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);
for(double i = 0;i < bmilist.size(); i++){
//System.out.println(i);
JOptionPane.showMessageDialog(null, "List:" + i);
}
JOptionPane.showMessageDialog(null, "Bmi: " + String.format("%s", bmi.toString()));
} while(getUserAnswer() == 'J');
}
public static double getLength(String message){
String lengte = JOptionPane.showInputDialog(message);
return Double.parseDouble(lengte);
}
public static double getGewicht(String message){
String gewicht = JOptionPane.showInputDialog(message);
return Double.parseDouble(gewicht);
}
public static char getUserAnswer(){
String answer = JOptionPane.showInputDialog("Wil je opnieuw: j/n?");
return Character.toUpperCase(answer.charAt(0));
}
}
I cant get anything print out of that list. Every time someone calculates his BMI, it should put them in an Arraylist
and when they click "continue", no... then it should return all the BMI's they calculated.
答案1
得分: 1
你错误地在一个列表结构中使用了循环:
for (int i = 0; i < bmilist.size(); i++) {
JOptionPane.showMessageDialog(null, "List:" + bmilist.get(i).getBmi());
}
英文:
You misused a loop with a list structure
for (int i = 0; i < bmilist.size(); i++) {
JOptionPane.showMessageDialog(null, "List:" + bmilist.get(i).getBmi());
}
答案2
得分: 0
尝试使用int变量进行for循环
do {
double lengte = getLength("请输入长度(以米为单位):");
double gewicht = getGewicht("请输入重量(以千克为单位):");
BMICalculator bmi = new BMICalculator(lengte, gewicht);
bmi.setBmi(bmi.calculateBMI());
bmilist.add(bmi);
// 使用int变量代替for循环中的double
for(int i = 0; i < bmilist.size(); i++){
JOptionPane.showMessageDialog(null, "列表:" + bmilist.get(i));
}
} while(getUserAnswer() == 'J');
英文:
Try the for loop with int variable
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);
// int variable instead of double inside for loop
for(int i = 0;i < bmilist.size(); i++){
JOptionPane.showMessageDialog(null, "List:" + bmilist.get(i));
}
}while(getUserAnswer() == 'J');
答案3
得分: 0
ArrayList<BMICalculator> bmilist = new ArrayList<>();
do {
double length = getLength("Enter the length in Meters:");
double weight = getWeight("Enter the weight in Kg:");
BMICalculator bmi = new BMICalculator(length, weight);
bmi.setBmi(bmi.calculateBMI());
bmilist.add(bmi);
JOptionPane.showMessageDialog(null, String.format("%s", bmi.toString()));
} while(getUserAnswer() == 'J');
System.out.println("Length Weight BMI");
for(int i = 0; i < bmilist.size(); i++)
{
System.out.printf("%-10s%-10s%s\n", bmilist.get(i).getLength(), bmilist.get(i).getWeight(), String.format("%.2f", bmilist.get(i).getBmi()));
}
}
英文:
ArrayList<BMICalculator> bmilist = new ArrayList<>();
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');
System.out.println("Lengte Gewicht BMI");
for(int i = 0;i < bmilist.size(); i++)
{ System.out.printf("%-10s%-10s%s\n",bmilist.get(i).getLengte(),bmilist.get(i).getGewicht(),String.format("%.2f", bmilist.get(i).getBmi()));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论