Jave错误:解析时到达文件末尾(不典型)

时间:2017-12-05 19:38:15

标签: java arrays math methods

我试图创建一个方法来查找2个给定数字的公共因子,但我无法得到要编译的文件。我所有的大括号都已关闭,因为我意识到这通常几乎总是导致此错误。希望有人可以帮助我!

import java.util.Scanner;
public class E1{
   public static void main (String [] args){  
      Scanner kb = new Scanner(System.in);
      double n1,n2;

      System.out.println("Enter two numbers");
      n1=kb.nextDouble();
      n2=kb.nextDouble(); 

      printCommonFactors(n1,n2);
   }

 //call a method that prints the positive shared factors of the 2 inputed numbers

    public static void printCommonFactors(int n1,int n2){

    //determining the max/min of the two inputed variables

        int max,min;
        max=Math.max(n1,n2);
        min=Math.min(n1,n2);

    //setting up 2 arrays to store the factors

        int [] maxFactors = new int [max];
        int [] minFactors = new int [min];      
        int counter1;

        for (inti=0;i>max;i++)
            if (i%max=0)
                    counter1++;
                    maxFactors[counter1]=i;


        for (int i=0;i>min;i++)
            if (maxFactors[i]%min=0)
                maxFactors[i]=

    }
}

这是我收到的错误:

enter image description here

1 个答案:

答案 0 :(得分:1)

您看到“解析时到达文件末尾”的原因是解析器希望为equals运算符找到右侧操作数,但未能这样做。您使用maxFactors[i]=结束方法。二元运算符总是需要右侧操作数。在这种情况下,您必须在等号后面放置一个值。

此外,您似乎正在尝试将一些原则应用于您可能从另一种语言中提取的Java。这里最明显的一点是你使用带有空格的替换显式块。这适用于像Python这样的语言,但不适用于Java。缩进在Java中并不重要,只会提高可读性。

这与您的for声明相关。因为您实际上并没有使用块,所以这些语句实际上是等价的:

for (inti=0;i>max;i++)
    if (i%max=0)
        counter1++;
        maxFactors[counter1]=i;

for (inti=0;i>max;i++) {
    if (i%max=0) {
        counter1++;
    }
}
maxFactors[counter1]=i;

这会导致i被引用超出其范围的问题。另一个问题是for初始值设定项(inti=0;)缺少空格,应为int i = 0

其他问题包括尝试分配非整数大小的数组(必须是int类型)并为for使用错误的测试表达式 - 循环(i>min将始终保持不变如果永远为真,则由于您的递增器为真,直到达到整数溢出,则为true。

相关问题