Java中的ArrayList,在获取所有数据并打印输出。

huangapple go评论68阅读模式
英文:

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&lt;BMICalculator&gt; bmilist = new ArrayList&lt;&gt;();
//ArrayList&lt;Double&gt; bmilist = new ArrayList&lt;Double&gt;();
do {
double lengte = getLength(&quot;Geef de lengte in Meters:&quot;);
double gewicht = getGewicht(&quot;Geef het gewicht in Kg:&quot;);
BMICalculator bmi = new BMICalculator(lengte, gewicht);
bmi.setBmi(bmi.calculateBMI());
bmilist.add(bmi);
for(double i = 0;i &lt; bmilist.size(); i++){
//System.out.println(i);
JOptionPane.showMessageDialog(null, &quot;List:&quot; + i);
}
JOptionPane.showMessageDialog(null, &quot;Bmi: &quot; + String.format(&quot;%s&quot;, bmi.toString()));
} while(getUserAnswer() == &#39;J&#39;);
}
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(&quot;Wil je opnieuw: j/n?&quot;);
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 &lt; bmilist.size(); i++) {
JOptionPane.showMessageDialog(null, &quot;List:&quot; + 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(&quot;Geef de lengte in Meters:&quot;);
double gewicht = getGewicht(&quot;Geef het gewicht in Kg:&quot;);
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 &lt; bmilist.size(); i++){
JOptionPane.showMessageDialog(null, &quot;List:&quot; + bmilist.get(i));
}
}while(getUserAnswer() == &#39;J&#39;);

答案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&lt;BMICalculator&gt; bmilist = new ArrayList&lt;&gt;();
do {
double lengte = getLength(&quot;Geef de lengte in Meters:&quot;);
double gewicht = getGewicht(&quot;Geef het gewicht in Kg:&quot;);
BMICalculator bmi = new BMICalculator(lengte, gewicht);
bmi.setBmi(bmi.calculateBMI());
bmilist.add(bmi);
JOptionPane.showMessageDialog(null, String.format(&quot;%s&quot;, bmi.toString()));
} while(getUserAnswer() == &#39;J&#39;);
System.out.println(&quot;Lengte    Gewicht   BMI&quot;);
for(int i = 0;i &lt; bmilist.size(); i++)
{            System.out.printf(&quot;%-10s%-10s%s\n&quot;,bmilist.get(i).getLengte(),bmilist.get(i).getGewicht(),String.format(&quot;%.2f&quot;, bmilist.get(i).getBmi()));
}
}

huangapple
  • 本文由 发表于 2020年9月26日 23:00:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64079137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定