错误:预期的类,接口或枚举以及其他问题

时间:2018-09-01 23:58:26

标签: java debugging

我试图对输入和输出程序进行编码,以了解它如何使“ int name = in.nextInt();”错误。和括号。我一直在研究问题并解决问题,但似乎无济于事。我希望用户回答一系列问题,例如您的名字,输入您的年龄,输入您喜欢的食物。

public class Main {
   public static void main( String args[] );
   {
   }
   Scanner Int = new Scanner( System.in );
}
   int n;
   {

   }
   System.out.print (  
   "What is your name", args) ; 
        int name = in.nextInt();
}
}

以下是错误:

C:\Users\Brenden\Google Drive\proj\tempj8\src\quicktest\ScratchPad.java:22: error: class, interface, or enum expected
   int n;
C:\Users\Brenden\Google Drive\proj\tempj8\src\quicktest\ScratchPad.java:23: error: class, interface, or enum expected
   {
C:\Users\Brenden\Google Drive\proj\tempj8\src\quicktest\ScratchPad.java:28: error: class, interface, or enum expected
        int name = in.nextInt();
C:\Users\Brenden\Google Drive\proj\tempj8\src\quicktest\ScratchPad.java:29: error: class, interface, or enum expected
}
4 errors
C:\Users\Brenden\Google Drive\proj\tempj8\nbproject\build-impl.xml:938: The following error occurred while executing this line:
C:\Users\Brenden\Google Drive\proj\tempj8\nbproject\build-impl.xml:270: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 1 second)

2 个答案:

答案 0 :(得分:1)

您的代码中有很多错误:

1。有一个';'在主要方法之后

2。您的代码应位于main方法中。您已经以方括号'}'

结尾

3.class在输入之前已经结束

4。您必须导入实用程序包才能使用扫描仪

5。您正在输入整数作为名称

您尝试这样的事情:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter your name : ");
        String name = in.nextLine();
    }
}

答案 1 :(得分:0)

您的代码中有很多错误..对其进行重构...

 import java.util.*; 
 public class Main { 
public static void  main(String args[]) { 
 Scanner in = new Scanner(System.in);     
 System.out.print("Enter your name : "); 
 String name = in.nextLine(); 
 System.out.println(name);
in.close(); // you should always close  your scanner

}

 }