在java中如何在while循环中存储字符串

在java中如何在while循环中存储字符串,java,while-loop,joptionpane,Java,While Loop,Joptionpane,我正在尝试将while循环中创建的数据存储在list变量中,以便稍后在JOptionPane.INFORMATION_消息中使用它。 我尝试使用数组来存储数据,但它不起作用。 请让我知道我错过了什么 package loops; import java.text.DecimalFormat; import java.util.ArrayList; import javax.swing.JOptionPane; public class Statistics { public st

我正在尝试将while循环中创建的数据存储在list变量中,以便稍后在JOptionPane.INFORMATION_消息中使用它。 我尝试使用数组来存储数据,但它不起作用。 请让我知道我错过了什么

package loops;

import java.text.DecimalFormat;
import java.util.ArrayList;

import javax.swing.JOptionPane;

public class Statistics
{

    public static void main(String[] args)
    {
        int observations = 1,
            num = 0,
            sum = 0,
            max = Integer.MIN_VALUE,
            min = Integer.MAX_VALUE;
        double mean = 0.0;

        String userEntry = "",
               result,
               list = " ",
               seperator = "\n***********\nYou entered the following observations: ";

        DecimalFormat twoDigits = new DecimalFormat ("0.00");

        userEntry = JOptionPane.showInputDialog ("Enter observation # " + observations + 
                 " (or \"end\" to quit) ");
        //num = Integer.parseInt(userEntry);

        String[] list2 = new String[num];

        while(!userEntry.equalsIgnoreCase("end"))
        {       
            num = Integer.parseInt(userEntry);          
            observations ++;
            userEntry = JOptionPane.showInputDialog ("Enter observation #" + observations + 
                         " (or \"end\" to quit) ");
            //num = Integer.parseInt(userEntry);
            //num = Integer.parseInt(userEntry);
            //list = "\n" + num;
            //list = list.toString();
            sum += num;     

            if(num > max)
            {
                max = num;
            }

            if(num < min)
            {
                min = num;
            }
            //Integer.toString(num);
            //ArrayList<String> list = new ArrayList<String>();
            //list.add(num);

            //list = "\n" + Integer.toString(num);
            //String[] list2 = new String[num];
            //for(String list1 : list2)
            //{
            list  = "\n" + num;
            //}

        }
        observations --;
        mean = sum / (double)observations;
        //twoDigits.format(mean);

        if(observations == 0)
        {
            result = "no observations selected";
        }

        else
        {
            result = "You entered " + observations + 
                      (observations == 1 ? " observation" : " observations");
            result = result + "\nThe minimum is " + min;
            result = result + "\nThe maximum is " + max;
            result = result + "\nThe sum is " + sum;
            result = result + "\nThe mean is " + twoDigits.format(mean);
            result = result + seperator;
            result = result + list;
        }
        JOptionPane.showMessageDialog(null, result, 
                "Results", JOptionPane.INFORMATION_MESSAGE);


        //observations --;

        System.exit(0);
    }

}
包循环;
导入java.text.DecimalFormat;
导入java.util.ArrayList;
导入javax.swing.JOptionPane;
公共课统计
{
公共静态void main(字符串[]args)
{
int观测值=1,
num=0,
总和=0,
最大值=整数。最小值,
最小值=整数。最大值;
双平均值=0.0;
字符串userEntry=“”,
结果,,
列表=”,
separator=“\n************\n您输入了以下观察结果:”;
DecimalFormat两位数=新的DecimalFormat(“0.00”);
userEntry=JOptionPane.showInputDialog(“输入观察值”#“+观察值”+
“(或“结束”退出)”;
//num=Integer.parseInt(userEntry);
String[]list2=新字符串[num];
而(!userEntry.equalsIgnoreCase(“end”))
{       
num=Integer.parseInt(userEntry);
观察++;
userEntry=JOptionPane.showInputDialog(“输入观察值”#“+观察值”+
“(或“结束”退出)”;
//num=Integer.parseInt(userEntry);
//num=Integer.parseInt(userEntry);
//list=“\n”+num;
//list=list.toString();
sum+=num;
如果(数值>最大值)
{
max=num;
}
if(num
尝试使用ArrayList而不是[]。下面显示了基于您的代码的示例

package loops;

import java.text.DecimalFormat;
import java.util.ArrayList;

import javax.swing.JOptionPane;

public class Statistics {

    public static void main(String[] args) {
        // method local variables
        int observations = 0;
        int num = 0;
        int sum = 0;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        double mean = 0.0;
        String userEntry = "";
        String result;
        String list = " "; 
        String seperator = "\n***********\nYou entered the following observations: ";
        DecimalFormat twoDigits = new DecimalFormat("0.00");
        ArrayList<Integer> intList = new ArrayList<Integer>();
        // get user entry from JOptionPane
        userEntry = JOptionPane.showInputDialog("Enter observation # " + observations + " (or \"end\" to quit) ");
        // iterate until user enters end
        while (userEntry.equalsIgnoreCase("end") == false) {
            observations++;
            num = Integer.parseInt(userEntry);
            userEntry = JOptionPane.showInputDialog("Enter observation #" + observations + " (or \"end\" to quit) ");
            sum = sum + num;
            // change number to max if > max
            if (num > max) {
                max = num;
            }
            // change number to min if < min
            if (num < min) {
                min = num;
            }
            // add the number to the string and to the list
            list = list + "\n" + num;
            intList.add(num);
        }
        echoList(intList);
        mean = sum / (double) observations;
        if (observations == 0) {
            result = "no observations selected";
        }
        else {
            result = "You entered " + observations + (observations == 1 ? " observation" : " observations");
            result = result + "\nThe minimum is " + min;
            result = result + "\nThe maximum is " + max;
            result = result + "\nThe sum is " + sum;
            result = result + "\nThe mean is " + twoDigits.format(mean);
            result = result + seperator;
            result = result + list;
        }
        // show results and exit
        JOptionPane.showMessageDialog(null, result, "Results", JOptionPane.INFORMATION_MESSAGE);
        System.exit(0);
    }

    private static void echoList(ArrayList<Integer> intList) {
        System.out.println("------------------------------");
        System.out.println("Got " + intList.size() + " integers.");
        for(Integer i : intList) {
            System.out.println("\t" + i);
        }
        System.out.println("------------------------------");
    }

}
包循环;
导入java.text.DecimalFormat;
导入java.util.ArrayList;
导入javax.swing.JOptionPane;
公共课统计{
公共静态void main(字符串[]args){
//方法局部变量
int观测值=0;
int num=0;
整数和=0;
int max=整数的最小值;
int min=整数最大值;
双平均值=0.0;
字符串userEntry=“”;
字符串结果;
字符串列表=”;
字符串分隔符=“\n************\n您输入了以下观察结果:”;
DecimalFormat两位数=新的DecimalFormat(“0.00”);
ArrayList intList=新的ArrayList();
//从JOptionPane获取用户条目
userEntry=JOptionPane.showInputDialog(“输入观察结果”+“(或“结束”退出)”);
//迭代直到用户进入结束
while(userEntry.equalsIgnoreCase(“end”)==false){
观察++;
num=Integer.parseInt(userEntry);
userEntry=JOptionPane.showInputDialog(“输入观察结果”+“(或“结束”退出)”);
sum=sum+num;
//如果>最大值,则将数字更改为最大值
如果(数值>最大值){
max=num;
}
//如果<最小值,则将数字更改为最小值
if(num
JOptionPane.showInputDialog()
将返回用户输入的
字符串,如果用户点击ok,,否则返回null

因此,如果用户点击cancel,您的
while
条件(
userEntry.equalsIgnoreCase(“end”)==false
)将被删除
List<Integer> userEntries = new ArrayList<>();
while (userEntry.equalsIgnoreCase("end") == false) 
{
    observations++;
    num = Integer.parseInt(userEntry);
    userEntries.add(num);
    ...
}