导入扫描仪类的问题

时间:2012-09-17 00:56:54

标签: java

我正在尝试执行以下代码,但不断收到此错误:

  

错误:在ScannerDemo类中找不到主要方法,请将main方法定义为:      public static void main(String[] args)

import java.util.Scanner;
class ScannerDemo
public class Main {

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

        System.out.println("Enter a number");
        username = sc.nextLine();
        System.out.println("your number is" + username + "enter your next number");
        username2 = sc.nextline();
        System.out.println("your total is" + username2 );     
    }
}

我认为我必须将Scanner类导入错误,我尝试过不同的方法,但对我来说没有任何效果。

5 个答案:

答案 0 :(得分:4)

您在顶部连续有两个类声明!这是错的。你的进口很好。尝试:

import java.util.Scanner;
public class ScannerDemo {
  public static void main(String[] args) {
    ...
  }
}

答案 1 :(得分:3)

你的班级定义错了。您正尝试定义两个类ScannerDemoMain。替换:

class ScannerDemo
 public class Main

只有:

public class ScannerDemo

此外,在您的main方法中,您应该引用userName变量,而不是username,并且您没有定义username2。请注意,Java标识符 区分大小写:

public static void main (String [] args)
{
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter a number");
  String userName = sc.nextLine();
  System.out.println("your number is" + username + "enter your next number");
  String username2 = sc.nextline();
  System.out.println("your total is" + username2 );
}

答案 2 :(得分:-1)

import java.util.Scanner;

public class ScannerDemo {

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

        System.out.println("Enter a number");
        username = sc.nextLine();
        System.out.println("your number is" + username + "enter your next number");
        username2 = sc.nextline();
        System.out.println("your total is" + username2 );     
    }
}

答案 3 :(得分:-1)

if I build the Dockerfile standalone and run the container, it will execute the sql files inside the folder

希望这有帮助

答案 4 :(得分:-3)

import java.util.Scanner;


public class ScannerDemo {

public static void main (String [] args) {

        Scanner sc = new Scanner(System.in);
        String userName;

        System.out.println("Enter a number");
        int username = sc.nextInt();
        System.out.println("your number is " + username);
        System.out.println("enter your next number");
        int username2 = sc.nextInt();
        System.out.println("your total is " + (username2 + username));     
    }
}