gpt4 book ai didi

java - "error: variable keyboard is already defined in method main(String [])"

转载 作者:行者123 更新时间:2023-12-01 17:26:25 36 4
gpt4 key购买 nike

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

最佳答案

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

关于java - "error: variable keyboard is already defined in method main(String [])",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14684267/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com