“错误:变量键盘已在方法main(String [])中定义”

时间:2013-02-04 09:58:11

标签: java string variables keyboard

当我尝试运行程序时,我收到了几条错误消息,主要困扰我的是“错误:变量键盘已在方法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();

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分: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();`

答案 2 :(得分:0)

每次使用时都不需要初始化键盘。您可以在程序顶部声明一次,只需在每次想要从中获取内容时调用keyboard.next()。