简体   繁体   English

为什么需要int? Java 错误:发现双精度,需要整数

[英]Why is an int required? Java Error: Found double, required int

I'm working on a code to read from a text file and display the maximum of the numbers.我正在编写一个从文本文件中读取并显示最大数字的代码。 Using MaxArray[] the for statement is throwing an error because the input numbers are doubles and it is expecting an int.使用 MaxArray[] for 语句会引发错误,因为输入数字是双精度数,并且它需要一个 int。 Any tips on what can be used to find the max of an array containing doubles?关于什么可用于查找包含双精度数的数组的最大值的任何提示?

  import java.io.*;

public class Read {
    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Rayman7337\\IdeaProjects\\Exam2\\src\\Data.txt"));

            String Num_1 = br.readLine();
            String Num_2 = br.readLine();
            String Num_3 = br.readLine();
            String Num_4 = br.readLine();
            String Num_5 = br.readLine();

            double Convert_1 = Double.parseDouble(Num_1);
            double Convert_2 = Double.parseDouble(Num_2);
            double Convert_3 = Double.parseDouble(Num_3);
            double Convert_4 = Double.parseDouble(Num_4);
            double Convert_5 = Double.parseDouble(Num_5);

            double MaxArray[] = {Convert_1, Convert_2, Convert_3, Convert_4, Convert_5};

            double Max = MaxArray[0];

            for(double i = 0; i<=4; i++){
                if (MaxArray[i] > Max) Max = MaxArray[i];
            }
            System.out.println("The maximum number is: " + Max);

        }catch (Exception ex){
            return;
        }

    }
}

All you need to do is replace您需要做的就是更换

for(double i = 0; i<=4; i++){ for(双 i = 0; i<=4; i++){

with

for(int i = 0; i<=4; i++){ for(int i = 0; i<=4; i++){

The position in the array can only be an integer value, the value at this array position is still double as you defined.数组中的position只能是 integer 值,这个数组中的position 仍然是您定义的两倍。

double MaxArray[]双最大数组[]

Further, I would replace <=4此外,我会替换 <=4

for(int i = 0; i < MaxArray.length ; i++){ for(int i = 0; i < MaxArray.length ; i++){

So you don't have to update the limit if you add/remove a value to the array.因此,如果向数组添加/删除值,则不必更新限制。

Array indexes should not be double.数组索引不应该是双倍的。 Kindly check the difference with below code.请使用以下代码检查差异。

  import java.io.*;

public class Read {
    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Rayman7337\\IdeaProjects\\Exam2\\src\\Data.txt"));

            String Num_1 = br.readLine();
            String Num_2 = br.readLine();
            String Num_3 = br.readLine();
            String Num_4 = br.readLine();
            String Num_5 = br.readLine();

            double Convert_1 = Double.parseDouble(Num_1);
            double Convert_2 = Double.parseDouble(Num_2);
            double Convert_3 = Double.parseDouble(Num_3);
            double Convert_4 = Double.parseDouble(Num_4);
            double Convert_5 = Double.parseDouble(Num_5);

            double MaxArray[] = {Convert_1, Convert_2, Convert_3, Convert_4, Convert_5};

            double Max = MaxArray[0];

            for(int i = 0; i<=MaxArray.length; i++){
                if (MaxArray[i] > Max) Max = MaxArray[i];
            }
            System.out.println("The maximum number is: " + Max);

        }catch (Exception ex){
            return;
        }

    }
}

Array indices are integers so any type used as an array index must be safely cast to an int.数组索引是整数,因此任何用作数组索引的类型都必须安全地转换为 int。 To solve your immediate problem, change the type of your for-loop index accordingly.要解决您当前的问题,请相应地更改 for 循环索引的类型。

In addition, you can begin iteration on the second element of the array because you initialize max to the value of maxArray[0] .此外,您可以在数组的第二个元素上开始迭代,因为您将max初始化为maxArray[0]的值。 The traditional form of the for-loop condition in this pattern is to use less-than-array-length.此模式中 for 循环条件的传统形式是使用小于数组长度。 Java naming convention is for variables to generally begin with lowercase letters. Java 命名约定是变量通常以小写字母开头。 With all that in mind...考虑到这一切...

double max = maxArray[0];
for (int i = 1 ; i < maxArray.length ; ++i) {
  if (maxArray[i] > max) {
    max = maxArray[i];
  }
}

Once you understand how this works, you could use a Stream to do it more succinctly.一旦你了解了它是如何工作的,你就可以使用 Stream 来更简洁地完成它。

double max = Arrays.stream(maxArray).max().getAsDouble();

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

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