项目赢得';t输出最小的数字[Java]

项目赢得';t输出最小的数字[Java],java,Java,我正在创建一个程序,用户在其中输入随机数,然后返回最高和最低的数字。我将highestNumber和LowerstNumber初始化为数组中的第一个元素。然后我检查这个数字是否大于或小于为各个变量分配的值。除了将索引值与最低数字进行比较的if语句外,其他所有语句都可以工作。我不知道为什么它不起作用。对于最大的数字,它与if语句正好相反(顺便说一句,它是有效的)。对于最小的数字,程序始终返回0.0。以下是我的主要方法: private static String arrayLimit; priva

我正在创建一个程序,用户在其中输入随机数,然后返回最高和最低的数字。我将highestNumber和LowerstNumber初始化为数组中的第一个元素。然后我检查这个数字是否大于或小于为各个变量分配的值。除了将索引值与最低数字进行比较的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);
}
私有静态字符串arrayLimit;
私有静态双高数;
私有静态双下限数;
公共静态void main(字符串[]参数)引发IOException
{
缓冲读取器控制台=
新的BufferedReader(新的InputStreamReader(System.in));
System.out.print(“您想输入多少数字?”);
arrayLimit=console.readLine();
int limit=Integer.parseInt(arrayLimit);
int[]编号=新的int[限制];
最高编号=编号[0];
最低编号=编号[0];
对于(int index=0;index最高编号)
{
最高编号=编号[索引];
}
if(编号[索引]<最低编号)
{
最低编号=编号[索引];
}
} 
System.out.println(“最高编号:“+最高编号”);
System.out.println(“最低编号:+最低编号”);
}

注意如何将最高和最低值初始化为编号[0]。在这一点上,它们都是0。当您浏览数字列表时,没有任何东西会小于0(假设您只输入正数),因此0将是您的输出。我建议将其初始化为
Integer.MAX\u VALUE
——根据定义,每个int都比这个低。类似地,您可能希望将
highestNumber=number[0]
更改为
highestNumber=Integer.MIN\u VALUE
——不低于的值,这将允许您的程序处理负数。

注意如何将最高和最低值初始化为
number[0]
。在这一点上,它们都是0。当您浏览数字列表时,没有任何东西会小于0(假设您只输入正数),因此0将是您的输出。我建议将其初始化为
Integer.MAX\u VALUE
——根据定义,每个int都比这个低。类似地,您可能希望将
highestNumber=number[0]
更改为
highestNumber=Integer.MIN\u VALUE
——不小于的值,这将允许您的程序处理负数

我对数组中的第一个元素初始化了
highestNumber
lowerstnumber

不,您没有将这些变量初始化为数组的第一个元素:如果您这样做了,您的程序就会运行良好。问题是,您在
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]);
highestIndex=0;
Lowestinex=0;
对于(int index=0;index编号[高索引])
{
highestIndex=指数;
}
if(编号[索引]<编号[下限索引])
{
Lowestinex=指数;
}
} 
System.out.println(“最高编号:“+number[highestIndex]);
System.out.println(“最低编号:“+编号[lowestIndex]);
我对数组中的第一个元素初始化了
highestNumber
lowerstnumber

不,您没有将这些变量初始化为数组的第一个元素:如果您这样做了,您的程序就会运行良好。问题是,您在
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]);
highestIndex=0;
Lowestinex=0;
对于(int index=0;index编号[高索引])
{
highestIndex=指数;
}
if(编号[索引]<编号[下限索引])
{
Lowestinex=指数;
}
} 
System.out.println(“最高编号:“+number[highestIndex]);
System.out.println(“最低编号:“+编号[lowestIndex]);
for(int index=0;index最高编号)
{
最高编号=编号[索引];
}
if(编号[索引]<最低编号)
{
最低编号=编号[索引];
}
} 
**请注意,除了在用户实际输入数字[0]的值之前通过数字[0]初始化了highestNumber和LowerstNumber之外,代码的所有内容都很好,所以请使用这些 用户输入值fo的循环中的初始化语句
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);
        }
  }
}