0

当我尝试运行我的程序时,我收到了几条错误消息,困扰我的主要消息是“错误:变量键盘已在方法 main(String []) 中定义”

我应该在我的程序中不止一次地使用 main(String []) 吗,或者只是在我拥有它的开始时?这里还有什么问题?

这是我的程序的开头:

public static void main(String[]args)
{
    String firstName, lastName;
    int moviesDownloaded, stateResidency;
    double movieCost, netPayment, tax, discount, totalCharge, payment, taxRate;

    System.out.println("Enter your first name:");
    Scanner keyboard = new Scanner(System.in);
    firstName = keyboard.nextString();

    System.out.println("Enter your last name:");
    Scanner keyboard = new Scanner(System.in);
    lastName = keyboard.nextString();

    System.out.println("Enter the number of movies downloaded:");
    Scanner keyboard = new Scanner(System.in);
    moviesDownloaded = keyboard.nextInt();

    System.out.println("Enter the cost per movie:");
    Scanner keyboard = new Scanner(System.in);
    movieCost = keyboard.nextDouble();

    System.out.println("Indicate your state of residency. Enter 1 for Mississippi or 2 for any other state.");
    Scanner keyboard = new Scanner(System.in);
    stateResidency = keyboard.nextInt();
4

3 回答 3

4

您应该只声​​明和初始化一次键盘,然后使用它。所以删除所有类型的行:Scanner keyboard = new Scanner(System.in);除了第一行。否则,您会尝试多次声明同一个变量,从而导致 java 抱怨。

于 2013-02-04T09:59:39.480 回答
1

我猜这个问题早就解决了,但我遇到了它,我真的很喜欢关闭,因为没有选择 Ivaylo Strandjev 的答案。也以防其他人偶然发现这一点。

错误是说您已经在此范围内定义了变量键盘,并且您正在尝试再次定义它。就像 Ivaylo Strandjev 说的那样。您可以尝试以下方法:

1 删除声明部分

public static void main(String[]args) {
String firstName, lastName;
int moviesDownloaded, stateResidency;
double movieCost, netPayment, tax, discount, totalCharge, payment, taxRate;

System.out.println("Enter your first name:");
Scanner keyboard = new Scanner(System.in);
firstName = keyboard.nextString();

System.out.println("Enter your last name:");
keyboard = new Scanner(System.in); //-----changed
lastName = keyboard.nextString();

System.out.println("Enter the number of movies downloaded:");
keyboard = new Scanner(System.in); //-----changed
moviesDownloaded = keyboard.nextInt();

System.out.println("Enter the cost per movie:");
keyboard = new Scanner(System.in);//-----changed
movieCost = keyboard.nextDouble();

System.out.println("Indicate your state of residency. Enter 1 for Mississippi or 2 for any other state.");
keyboard = new Scanner(System.in);//-----changed
stateResidency = keyboard.nextInt();`

或者这个,改变新的变量名

public static void main(String[]args){
String firstName, lastName;
int moviesDownloaded, stateResidency;
double movieCost, netPayment, tax, discount, totalCharge, payment, taxRate;

System.out.println("Enter your first name:");
Scanner keyboard1 = new Scanner(System.in);
firstName = keyboard1.nextString();

System.out.println("Enter your last name:");
Scanner keyboard2 = new Scanner(System.in);//-----changed
lastName = keyboard2.nextString();

System.out.println("Enter the number of movies downloaded:");
Scanner keyboard3 = new Scanner(System.in);//-----changed
moviesDownloaded = keyboard3.nextInt();

System.out.println("Enter the cost per movie:");
Scanner keyboard4 = new Scanner(System.in);//-----changed
movieCost = keyboard4.nextDouble();

System.out.println("Indicate your state of residency. Enter 1 for Mississippi or 2 for any other state.");
Scanner keyboard5 = new Scanner(System.in);//-----changed
stateResidency = keyboard5.nextInt();`
于 2015-08-04T20:05:26.213 回答
0

您无需在每次使用键盘时都初始化它。你可以在程序的顶部声明一次,每次你想从中获取一些东西时只需调用keyboard.next()。

于 2013-02-04T10:02:58.990 回答