簡體   English   中英

如何將joptionpane中的輸入保存為數組?

[英]how can i save input from joptionpane as an array?

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: ");

我在您的代碼中更改了許多內容。

01)將字符串作為輸入並使用無空格創建數組,然后將該字符串分解為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;
       }
    }

希望這就是您所需要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM