如何将joptionpane的输入保存为数组?

时间:2015-03-27 17:47:18

标签: java arrays

import javax.swing.JOptionPane;

    public class ArrayOperations
    {
       public static void main(String[] args)
       {

          String[] numbers;
          numbers = JOptionPane.showInputDialog("Enter your numbers: ");
          int numbers1 = Integer.parseInt(numbers);
          JOptionPane.ShowMessageDialog(null, "The sum of your numbers is: "
          + getTotal() + "\nThe average of your numbers is: " + getAverage()
          + "\nThe highest number was: " + getHighest + "The lowest number "
          + "was: " + getLowest()); 
       }
       public static double getTotal()
       {
          //Accumulate sum of elements in numbers1 array and return total 
          double total = 0.0;
          for (int index = 0; index < numbers1.length; index++)
             total += numbers1[index];

          return total;
       }
       public static double getAverage()
       {
          //Get average
          return getTotal() / numbers1.length;
       }   
       public static double getHighest()
       {
          //Find highest number entered
          double highest = numbers1[0];
          for (int index = 1; index < numbers1.length; index++)
          {
             if (numbers1[index] > highest)
                highest = numbers1[index];
          }
          return highest;
       }

       public static double getLowest()
       {
          //Find lowest number entered
          double lowest = numbers1[0];
          for (int index = 1; index < numbers1.length; index++)
          {
             if (numbers1[index] < lowest)
                lowest = numbers1[index];
          }
          return lowest;
       }
    }

所以...基本上我在第七章开始时都是用java书开始的,很多人的答案很容易使用我们尚未涵盖的方法......(我们刚刚学习数组)并且坦率地说我不知道​​如何在数组中保存用户输入...我真的很感激一些帮助。这段代码实际上有很多错误,但我想如果我弄清楚主要的错误,也许它会帮助我解决剩下的问题。

ArrayOperations.java:19: error: incompatible types: String cannot be     converted to String[]
      numbers = JOptionPane.showInputDialog("Enter your numbers: ");

1 个答案:

答案 0 :(得分:0)

我在你的代码中改变了很多东西。

01)以字符串作为输入并使用no空格创建数组,然后将字符串分解为int数组。

02)将数组传递给每个方法,以便计算所有元素。

代码:

import javax.swing.JOptionPane;

    public class ArrayOperations
    {
       public static void main(String[] args)
       {

          String numbers;
          numbers = JOptionPane.showInputDialog("Enter your numbers: ");


           int count = 0;
             for(int i = 0; i < numbers.length(); i++) {
                  if(Character.isWhitespace(numbers.charAt(i))) count++;
             }

             int [] numbers1 = new int [++count];

                for(int n = 0; n < count; n++) {                    
                    numbers1[n] = Integer.parseInt(numbers.split(" ")[n]);
            }


      JOptionPane.showMessageDialog(null, "The sum of your numbers is: "
          + getTotal(numbers1) + "\nThe average of your numbers is: " + getAverage(numbers1)
          + "\nThe highest number was: " + getHighest(numbers1) + "\nThe lowest number "
          + "was: " + getLowest(numbers1)); 
       }
       public static double getTotal(int[] numbers1)
       {
          //Accumulate sum of elements in numbers1 array and return total 
          double total = 0.0;
          for (int index = 0; index < numbers1.length; index++)
             total += numbers1[index];

          return total;
       }
       public static double getAverage(int[] numbers1)
       {
          //Get average
          return (getTotal(numbers1) / numbers1.length);
       }   
       public static double getHighest(int[] numbers1)
       {
          //Find highest number entered
          double highest = numbers1[0];
          for (int index = 1; index < numbers1.length; index++)
          {
             if (numbers1[index] > highest)
                highest = numbers1[index];
          }
          return highest;
       }

       public static double getLowest(int[] numbers1)
       {
          //Find lowest number entered
          double lowest = numbers1[0];
          for (int index = 1; index < numbers1.length; index++)
          {
             if (numbers1[index] < lowest)
                lowest = numbers1[index];
          }
          return lowest;
       }
    }

希望这就是你所需要的。

相关问题