System.out.println错误新程序员

问题描述 投票:0回答:4

我正在尝试做一个简单的输入程序,我在System.out.println命令上得到一个错误,我不知道它为什么不接受命令,并且在我修复之前无法继续工作。

错误说:

Multiple markers at this line
    - Syntax error, insert ")" to complete MethodDeclaration
    - Syntax error, insert "Identifier (" to complete 
     MethodHeaderName
    - Syntax error on token ".", @ expected after this token
    - Syntax error, insert "SimpleName" to complete QualifiedName

我的代码如下。

package classPack;

import java.util.Scanner;

public class Main {     
    Scanner s = new Scanner(System.in);
    int numone = s.nextInt();
    System.out.println("please input the number of numbers you want to analyze");
    Scanner r = new Scanner(System.in);
    int numtwo = s.nextInt();
}
java eclipse output java.util.scanner
4个回答
4
投票

试试这个:

public class Main {  
    public static void main(String[] args) {       
        // you only need one instance of scanner
        Scanner s = new Scanner(System.in);
        int numone = s.nextInt();
        System.out.println("please input the number of numbers you want to analyze");
        int numtwo = s.nextInt();
        // don't forget to close the scanner
        s.close();    
    }
}

在Java中,您必须将代码放在方法中,而不是在类级别。特别是,您可以将其放在main()方法中,该方法是执行所有程序的入口点。


0
投票

你错过了什么.....但你可以尝试这个“用户输入”代码

import java.util.Scanner;
public class test {     
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int numone = s.nextInt();
System.out.println("please input the number of numbers you want to analyze");
Scanner r = new Scanner(System.in);
int numtwo = s.nextInt();
}

}

0
投票
import java.util.Scanner;

class Multi {
  public static void doSomething() {
    Scanner s = new Scanner(System.in);
    int numone = s.nextInt();
    System.out.println("please input the number of numbers you want to analyze");
    Scanner r = new Scanner(System.in);
    int numtwo = s.nextInt();
    }
  }
}

public class MultiplicationTable {
  public static void main(String[] args) {
    Multi.doSomething();
  }
}

0
投票
public class Animal {

    public String name;
    public static int nosLegs;



    public Animal(){

    }

    public Animal(String name, int nosLegs) {
        super();
        this.name = name;
        this.nosLegs = nosLegs;
    }


    //getter and setter
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public int getNosLegs() {
        return nosLegs;
    }
    public void setNosLegs(int nosLegs) {
        this.nosLegs = nosLegs;
    }


    public class B{


        public int b;

        System.out.println("hello");

    }



}
change in Main method...
© www.soinside.com 2019 - 2024. All rights reserved.