简体   繁体   English

Java - 在数组中查找高低数

[英]Java - Find High and Low Number in an Array

I'm trying to find the high and low number in an array, but I'm not sure why my codes not working correctly.我试图在数组中找到高低数,但我不确定为什么我的代码不能正常工作。 It's giving me the output of 0 and 56. I understand why it's giving the 0, but where did the 56 come from?它给了我 0 和 56 的输出。我明白为什么它给了 0,但是 56 是从哪里来的?

package test;

public class Test {

    public static void main(String[] args) {
        int[] numbs = { '2', '4', '2', '8', '4', '2', '5'};

        int count = 0;
        int low = 0;
        int high = 0;

        while(count < numbs.length)
        {
             if(numbs[count]< low) {
                low = numbs[count];
            }

            if(numbs[count]> high) {
                high = numbs[count];
            }

            count++;   
        }

        System.out.println(low); 
        System.out.println(high);           

    }
}

You need to start the low low enough;你需要从足够low开始; currently, you start it at zero - too low to "catch" the lowest element of the array.目前,您从零开始 - 太低而无法“捕获”数组的最低元素。

There are two ways to deal with this:有两种方法可以解决这个问题:

  • Use Integer.MAX_VALUE and Integer.MIN_VALUE to start the high and low , or使用Integer.MAX_VALUEInteger.MIN_VALUE启动highlow ,或
  • Use the initial element of the array to start both high and low , then process the array starting with the second element.使用数组的初始元素开始highlow ,然后从第二个元素开始处理数组。

int low = Integer.MAX_VALUE;
int high = Integer.MIN_VALUE;

PS You would find strange numbers printed, because you used characters instead of integers: PS 你会发现打印出奇怪的数字,因为你使用了字符而不是整数:

int[] numbs = { 2, 4, 2, 8, 4, 2, 5};

另外..它可能正在打印ASCII表示...使用实际整数(我不确定您是否有意使用字符)。

You should set both the low and high to the value in the first position of the array.您应该将lowhigh都设置为数组第一个位置的值。

int low = numbs[0];
int high = numbs[0];

If the first position is indeed the lowest value it will never be swapped, otherwise it will be swapped with a lower number, the same happen to the highest number.如果第一个位置确实是最低值,它将永远不会被交换,否则它将与较低的数字交换,最高数字也会发生同样的情况。

You want an array of ints that you should declared like such:你需要一个整数数组,你应该像这样声明:

int[] numbs = {2, 4, 2, 8, 4, 2, 5};

You are getting 56 as the highest value because the decimal representation of the char '8' according to the ASCII table is 56 .您得到56作为最高值,因为根据 ASCII 表,字符 '8' 的十进制表示是56

您在数组 init 块中使用char s 而不是int s,这会立即转换为int s 以适应int[]数组,但不要使用其实际值,而是使用unicode值,其中: 2 = 52 和8 = 56。

'8' is char literal, not an int . '8'char文字,而不是int The Java Language Specification defines char as follows: Java 语言规范定义char如下:

The integral types are byte, short, int, and long [...] and char, whose values are 16-bit unsigned integers representing UTF-16 code units.整数类型是 byte、short、int 和 long [...] 和 char,它们的值是表示 UTF-16 代码单元的 16 位无符号整数。

If you use a char where an int is expected, the char is implicitly converted to int .如果在需要int地方使用char ,则该char将隐式转换为int Since char is a numeric type, this will preserve its numeric value, ie its UTF-16 code point .由于char是数字类型,这将保留其数值,即其 UTF-16代码点 The utf-16 code point of 8 is 56.的UTF-16代码点8是56。

Treating char as a numeric type can be useful.将 char 视为数字类型可能很有用。 For instance, we can compute the n-th letter of the alphabet by例如,我们可以通过以下方式计算字母表的第 n 个字母

char nthLetter = (char) `A` + n;

or find the numeric value of a digit by或通过查找数字的数值

if ('0' <= digit && digit <= '9')
    return value = digit - '0';
else
    throw new IllegalArgumentException(digit);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM