java - “Illegal start of expression”新秀错误?

标签 java compiler-errors

我需要知道为什么在18-21行出现此错误。我觉得它可能是与括号相关的问题,但我无法弄清楚。

import java.util.Scanner;

public class Proj4_1 {

public static void main(String [] args) {
    Scanner reader = new Scanner(System.in);

    int numb1 =;
    int numb2 =; 
    int div =;
    int remainder =;

    System.out.println("Enter a number: ");
    numb1 = reader.nextint();

    System.out.print(numb1);

    System.out.println("Enter another number: ");
    numb2 = reader.nextint();

    System.out.print(numb2);

    if (numb1>numb2){
        div = numb1 / numb2;
        remainder = numb1 % numb2;
    }else{
    if (numb2>numb1) 
        div = numb2 / numb1;
        remainder = numb2 % numb2;  
    }

    System.out.println("The answer quotient is " +div+ "with a remainder of" +remainder);

任何帮助或建议,将不胜感激,在此先感谢

最佳答案

好吧,看看这个:

int numb1 =;

您期望初始值是多少?

您可以声明变量而无需为其分配值,如下所示:
int numb1;

...但是您不能在没有给出值的情况下将=留在那里。

但是,我个人将等待声明变量,直到您准备为其提供值为止:
public static void main(String [] args) {
    Scanner reader = new Scanner(System.in);

    System.out.println("Enter a number: ");
    // Fixed case of nextInt as well...
    int numb1 = reader.nextInt();

    System.out.print(numb1);

    System.out.println("Enter another number: ");
    int numb2 = reader.nextInt();

    System.out.print(numb2);

    // etc
}

您不必在方法顶部声明所有变量,并且,如果仅在最需要它们的地方声明它们,则代码通常会更干净。

编辑:还有这个if块是不正确的:
if (numb2>numb1)
    div = numb2 / numb1;
    remainder = numb2 % numb2;  
}

您错过了左括号,这意味着您已经有效地做到了:
if (numb2>numb1) {
    div = numb2 / numb1;
}
remainder = numb2 % numb2;  
// This closing brace is now dangling!
}

您的意思是这样的:
if (numb2 > numb1) {
    div = numb2 / numb1;
    remainder = numb2 % numb2;  
}

请注意,如果您定期编译代码,那么不会有那么多不同的错误。一旦遇到一个编译时错误,就应该停止并确保理解并修复它。这样一来,您将永远不会遇到试图立即理解的大量问题。此外,您不必担心多个错误会相互影响,这会使生活变得更加艰难。

关于java - “Illegal start of expression”新秀错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15935159/

相关文章:

java - 空指针异常。无法读取流

java - 如何给数组中的对象赋值?

c++ - std::unordered_map - Inserting elements is “attempting to reference a deleted function”

c# - asp.net代码显示编译器错误消息: CS1729: 'EmailReader.Pop3Client' does not contain a constructor that takes 0 arguments

java - 如何从虚拟机调用 native 函数?

java - Dart:如何设置项目

java - SSL 握手失败 - 这里可能出了什么问题?

node.js - Coffeescript resetCards 不是函数

python - 如何告诉 distutils 使用 gcc?

c - 错误 : dereferencing pointer to incomplete type (Codeblocks, 在 C 中编程)