程序不会输出最低的数字[Java]

时间:2014-04-02 22:06:33

标签: java

我正在创建一个程序,用户输入随机数,然后返回最高和最低的数字。我将highestNumber和lowestNumber初始化为数组中的第一个元素。然后我检查了数字是否大于或小于为各个变量分配的值。除了将索引的值与最小数字进行比较的if语句之外,一切都有效。我不确定为什么它不起作用。它与最高数字的if语句完全相反(顺便说一下)。对于最小的数字,程序始终返回0.0。这是我的主要方法:

private static String arrayLimit;
private static double highestNumber;
private static double lowestNumber;   

public static void main(String[] argument) throws IOException
{
    BufferedReader console =
        new BufferedReader(new InputStreamReader(System.in));

    System.out.print("How many numbers do you want to enter? ");
    arrayLimit = console.readLine();
    int limit = Integer.parseInt (arrayLimit);

    int[] number = new int[limit];

    highestNumber = number[0];
    lowestNumber = number[0];

    for (int index = 0; index < limit; index++)
    {
        System.out.print("Number " + (index + 1) + ": ");
        String input = console.readLine();
        number[index] = Integer.parseInt(input);

        if (number[index] > highestNumber)
        {
            highestNumber = number[index];
        }

        if (number[index] < lowestNumber)
        {
            lowestNumber = number[index];
        }
    } 
    System.out.println("Highest number: " + highestNumber);
    System.out.println("Lowest number: " + lowestNumber);
}

4 个答案:

答案 0 :(得分:3)

请注意您如何将最高和最低值初始化为number[0]。那时,它们都是0.当你浏览数字列表时,任何东西都不会低于0(假设你只输入正数),因此0将是你的输出。我建议将其初始化为Integer.MAX_VALUE - 根据定义,每个int都低于此值。同样,您可能希望将highestNumber = number[0]更改为highestNumber = Integer.MIN_VALUE - 没有任何值低于此值,这样您的程序也可以处理负数。

答案 1 :(得分:1)

  

我将highestNumberlowestNumber初始化为数组中的第一个元素。

不,你没有将这些变量初始化为数组的第一个元素:如果你这样做了,你的程序就可以了。问题是,你已经在之前完成了赋值number[0]元素得到了它的第一个有意义的值,而是存储了默认值。这就是为什么你的程序为最低数字生成零的原因。

您可以通过存储最高和最低数字的索引来解决此问题,而不是存储数字本身:

highestIndex = 0;
lowestIndex = 0;

for (int index = 0; index < limit; index++)
{
    System.out.print("Number " + (index + 1) + ": ");
    String input = console.readLine();
    number[index] = Integer.parseInt(input);

    if (number[index] > number[highestIndex])
    {
        highestIndex = index;
    }

    if (number[index] < number[lowestIndex])
    {
        lowestIndex = index;
    }
} 
System.out.println("Highest number: " + number[highestIndex]);
System.out.println("Lowest number: " + number[lowestIndex]);

答案 2 :(得分:1)

for (int index = 0; index < limit; index++) {

    System.out.print("Number " + (index + 1) + ": ");

    String input = console.readLine();

    number[index] = Integer.parseInt(input);

      if(index == 0){     // here you initialize highestNumber & lowestNumber 

            highestNumber = number[0];

            lowestNumber = number[0];
        }        

    if (number[index] > highestNumber)
    {
        highestNumber = number[index];
    }

    if (number[index] < lowestNumber)
    {
        lowestNumber = number[index];
    }
} 

**请注意,除非您在用户实际输入数字[0]的值之前将数字[0]初始化为highestNumber和lowestNumber,否则您的代码的一切都很好,所以请使用这些 循环中的初始化语句,当index = 0时,用户输入数字[0]的值。 这就是全部。因为我在我的电脑上测试过,所以请试试。

答案 3 :(得分:0)

我将向您展示我如何建议构建此代码。我的主要观点是你应该使用数组来构造你的代码,但是由于你使用的是面向对象的语言,我会使它更加面向对象。我不太了解Java,所以请考虑以下是伪代码。某些类型可能是错误的,某些方法名称可能会被组成,等等。

class IntList {
  private final int[] numbers;
  private Integer highestNumber;
  private Integer lowestNumber;   

  public IntList(int [] numbers, boolean makeCopy)
    {
    highestNumber = null;
    lowestNumber = null;
    if(makeCopy)
      this.numbers = Arrays.copyOf(numbers, numbers.length);
    else
      this.numbers = numbers;
    }

  public static IntList getInput(BufferedReader reader, BufferedWriter writer)
    {
    writer.print("How many numbers do you want to enter? ");
    String arrayLimit = reader.readLine();
    int limit = Integer.parseInt (arrayLimit);
    int[] numbers = new int[limit];
    //Actually read in the numbers, using the reader and writer that were
    //passed in, rather than reaching out to System.
    return new IntList(numbers, false);
    }

  public int getHighest()
    {
      if(highestNumber!=null)
        return highestNumber;
      else
        ;//Find the highest number; set highestNumber to that value, then return that result.
    }

  public int getLowest()
    {
       //same idea here.
    }
}

public class Main {
  public static void main(String[] argument) throws IOException
  {
      //try-with-resources ensures that the reader gets closed properly. Since nothing
      //else happens in the program, this doesn't matter so much, but it's generally a good
      //habit to get into.
      try(BufferedReader console =
          new BufferedReader(new InputStreamReader(System.in)))
        {
        IntList list = IntList.getInput(console, System.out);
        System.out.println("Highest number: " + list.getHighest());
        System.out.println("Lowest number: " + list.getLowest);
        }
  }
}