无法让扫描仪类在Java中工作

我试图让一个程序一起读取用户输入的整数。我一直在阅读有关扫描仪类,这似乎是在java中这样做的最常见的方式。但是,当我复制+粘贴the examples给出的网站like this one我得到某种错误,我不知道如何解决。这是令人沮丧的,因为所有发布的东西都应该是完整的代码,应该没有问题!无法让扫描仪类在Java中工作

的一些代码,应该工作的一个例子:

import java.util.Scanner; 

public class ScannerDemo {

public static void main(String[] arguments){

Scanner input = new Scanner(System.in);

String username;

double age;

String gender;

String marital_status;

int telephone_number;

// Allows a person to enter his/her name

Scanner one = new Scanner(System.in);

System.out.println("Enter Name:");

username = one.next();

System.out.println("Name accepted " + username);

// Allows a person to enter his/her age

Scanner two = new Scanner(System.in);

System.out.println("Enter Age:");

age = two.nextDouble();

System.out.println("Age accepted " + age);

// Allows a person to enter his/her gender

Scanner three = new Scanner(System.in);

System.out.println("Enter Gender:");

gender = three.next();

System.out.println("Gender accepted " + gender);

// Allows a person to enter his/her marital status

Scanner four = new Scanner(System.in);

System.out.println("Enter Marital status:");

marital_status = four.next();

System.out.println("Marital status accepted " + marital_status);

// Allows a person to enter his/her telephone number

Scanner five = new Scanner(System.in);

System.out.println("Enter Telephone number:");

telephone_number = five.nextInt();

System.out.println("Telephone number accepted " + telephone_number);

}

}

相反的程序运行,它给了我两个错误。

在行public class ScannerDemo {它给了我这个错误:

Illegal modifier for the local class ScannerDemo; only abstract or final is permitted

在下一行public static void main(String[] arguments){我得到这个错误:

The method main can not be declared static; static methods can only be declared in a static or top level type.

我曾与许多不同形式的扫描仪的尝试这一点,应该都准备好了,每次都会出错。我究竟做错了什么?我该如何解决它?

我使用的加工3.

回答:

我相信作为@Hovercraft提到你只需要在一个叫做ScannerDemo.java文件的代码,被猜测你拥有了它在不同的文件名。

回答:

你的类名和文件名必须是相同的。这是第一个错误的决定。

public static void main(String[] arguments){ 

由于第一个错误而没有工作。

回答:

请理解Java和加工之间的区别。 Processing是它自己的语言和编辑器,它有自己的语法规则,你不能只复制粘贴随机Java代码并期望它能够工作。您必须了解处理如何工作,然后以处理中的方式添加代码。

假设你正在使用的加工编辑器,那么你的主素描文件不应该只包含一个类,它绝对不应该包含main()方法。它应该包含setup()draw()函数。

如果您确实想使用某个类,那么请删除您的main()方法,将您的逻辑封装在类中的某个函数中,然后添加使用该类的setup()或函数。

或者更好的是,停止使用类,只需使用Scanner在你的处理代码。

如果您仍然无法得到它的工作,请在一个新的问题,后一个MCVE,我们会从那里。祝你好运。

以上是 无法让扫描仪类在Java中工作 的全部内容, 来源链接: utcz.com/qa/258265.html

回到顶部