返回值不正常? (基础Java)

时间:2014-12-10 16:40:00

标签: java

在这个程序中,我必须有变量(N)的用户输入值。在方法1中,用户将10个数字输入到数组中。在方法2中,它将比较变量(N)并将大于该变量的所有数字保存到(greaterNums)变量。有一些返回和发送问题,即使我已经反复阅读章节。有人请指出我正确的方向!

问题1:方法2中的参数后,greaterNums变量值不正确。 问题2:greaterNums变量没有返回到要显示的main方法。

import javax.swing.JOptionPane;

public class Project6Walker
{
    public static void main(String[] args)
    {
        final int ARRAY_SIZE = 10;        //Establish array size
        int n;                            //Holds comparable value
        String input;                     //Holds user input

        int[] array = new int[ARRAY_SIZE]; //Establishes array

        input = JOptionPane.showInputDialog("Enter a number. Must not be a negative number.");
        n = Integer.parseInt(input);     //Gather value N

        while (n < 0)           //INPUT VALIDATION for N
        {
            input = JOptionPane.showInputDialog("Must not be a negative number. Please try again.");
            n = Integer.parseInt(input);
        }

        gatherArrayInformation(array, input);             //Calls method 1
        compareArrayInformation(array, n);                //Calls method 2
        JOptionPane.showMessageDialog(null, "The numbers in the array that are "+
                "greater than " +n+ " are (greaterNums goes here).");  //Final output of information

        /**
          This method will prompt the user to enter 10 numbers in the array
          that will be compared to the value N
         */
    }

    public static int[] gatherArrayInformation(int[] array, String input)
    {
        JOptionPane.showMessageDialog(null, "Enter series of " + array.length + "  numbers");
        for (int i= 0; i < array.length; i++)
        {
            input = JOptionPane.showInputDialog("Number " + (i + 1) + ":");
            array[i] = Integer.parseInt(input);

            while (array[i] < 0)
            {
                input = JOptionPane.showInputDialog("Number " + (i + 1) + " cannot be negative.      Try     Again.");
                array[i] = Integer.parseInt(input);
            }
            System.out.print(array[i] + " ");
        }
        return array;
    }

    /**
      This method will take the 10 numbers from method 1,
      and see which numbers are larger than N
      @return greaterNums
     */
    public static int compareArrayInformation(int[] array, int n)
    {
        int greaterNums = 0;
        for (int i= 1; i < array.length; i++)
        {
            if (array[i] > n)
                greaterNums = array[i];
            System.out.println(greaterNums);
        }
        return greaterNums;
    }
}

4 个答案:

答案 0 :(得分:0)

问题2:当您返回更大的内容时,正如您的代码当前所代表的那样,它只返回具有大于n的最高索引的数组成员。

答案 1 :(得分:0)

数组索引从0开始。

public static List<Integer> compareArrayInformation(int[] array, int n)
{
    List<Integer> greaterNums = new ArrayList<>();
    for (int i = 0; i < array.length; i++) // Count from 0
    {
        if (array[i] > n) {
            int greaterNum = array[i];
            System.out.println(greaterNum);
            greaterNums.add(greaterNum);
        }
     }
     return greaterNums;
}

要收集所有更大的数字,最好使用固定大小的数组int[],而不是List<Integer>

答案 2 :(得分:0)

这个程序将与我设置为0而不是一个,但它当然只会显示最后的大数字而不是nums希望这是你想要的。

 public static int compareArrayInformation(int[] array, int n)
{
int greaterNums = 0;

for (int i= 0; i < array.length; i++)//not one 
 {
 if (array[i] > n)

    greaterNums = array[i];
    System.out.println(greaterNums);

 }
 return greaterNums;
 }

也许你想要这个?

 public static int[] compareArrayInformation(int[] array, int n)
{
int[] greaternums=new int[array.length];
int upto=0;

for (int i= 0; i < array.length; i++)//not one 
 {
 if (array[i] > n)

    greaternums[upto] = array[i];
    System.out.println(array[i]);
    upto++;
 }
 return Arrays.copyOf(greaternums, upto);
 }

答案 3 :(得分:0)

将您的功能compareArrayInformation替换为

public static int compareArrayInformation(int[] array, int n)
{
  int[] greaterNums = new int[10];  

  for (int i= 0; i < array.length; i++)
   {
     if (array[i] > n)
      { 
      greaterNums.add(array[i]);
      }

   }
  return greaterNums;
}

并在您的main方法中更改以下行

compareArrayInformation(array, n); JOptionPane.showMessageDialog(null, "The numbers in the array that are "+                 "greater than " +n+ " are (greaterNums goes here).");

int[] greaterNumsArray = compareArrayInformation(array, n); 
StringBuffer sBuffer = new StringBuffer("");

for (int i=0; i<greaterNumsArray.length; i++){
  sBuffer.append(greaterNumsArray[i]);
}
JOptionPane.showMessageDialog(null, "The numbers in the array that are "+
            "greater than " +n+ " are "+sBuffer );