“错误:方法main(String [])中已经定义了可变键盘". [英] "error: variable keyboard is already defined in method main(String [])"
问题描述
当我尝试运行程序时,我收到一些错误消息,主要的困扰我的是错误:方法main(String [])中已经定义了可变键盘"
I'm getting several error messages when I try to run my program, the main one which bothers me being "error: variable keyboard is already defined in method main(String [])"
我应该在程序中不止一次main(String []),还是在刚开始时就止步?这里还有什么不对吗?
Am I supposed to but main(String []) more than once in my program, or just in the beginning as I have it? What else could be wrong here?
这是我程序的开始:
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();
推荐答案
您只应声明并初始化一次键盘,然后再使用它.因此,请删除以下类型的所有行: Scanner keyboard = new Scanner(System.in);
.否则,您将尝试多次声明同一变量,因此java会抱怨.
You should only declare and initialize keyboard once and then use it. So remove all lines of the type: Scanner keyboard = new Scanner(System.in);
apart from the first one.
Otherwise you try to declare the same variable multiple times and thus java complains.
这篇关于“错误:方法main(String [])中已经定义了可变键盘".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!