只是想知道你们是否可以帮我解决这个错误,我一直在努力 修复过去一小时左右。
我在这段代码中调用带有错误的函数:
static Scanner scan = new Scanner(System.in);
static int xIn, yIn;
static String name = "";
static String input = "";
public static void main(String[] args){
if(args.length < 2){
System.out.println("Not enough arguments. Temination.");
System.exit(0);
}
else if(args.length > 2){
System.out.println("Too many arguments. Termination.");
System.exit(0);
}
BottyBot.Test(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
BottyBot.Greet();
}
BottyBot.Greet()函数是:
public static void Greet(){
name = scan.nextLine("Hello, what is your name? ");
do{
input = scan.nextLine("Would you like to order some boots, " + name + "? (y/n) ");
if(input == "y"){
System.out.println("Great! Let's get started.");
break;
}
else if(input == "n"){
System.out.println("Come back next time, " + name + ".");
System.exit(0);
}
else{
System.out.println("Invalid response. Try again.");
}
}
while(true);
}
我在两个扫描仪行都遇到两个错误。具体来说,就是说
error: method nextLine in class Scanner cannot be applied to given types;
next = scan.nextLine("Hello, what is your name? ");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
如果有人能指出我做错了什么,那就太棒了!谢谢!
答案 0 :(得分:4)
您应该这样写,因为scanner.nextLine()
方法不接受任何参数
System.out.println("Hello, what is your name? ");
name = scan.nextLine();
打印部件由System.out.println()
完成,扫描部件由scanner.nextLine()
完成。
error: method nextLine in class Scanner cannot be applied to given types;
next = scan.nextLine("Hello, what is your name? ");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
此消息为您提供了明确的提示。
它声明actual and formal argument lists differ in length
。所以这里的参数表示方法参数,必需提到no arguments
,而是找到String
,即&#34;你好,你叫什么名字? &#34;
答案 1 :(得分:1)
我认为你会混淆Scanner
所做的事情。这让我想起了QBasic中的input
函数,尽管我可能不正确。
如错误所述,nextLine()
方法不接受任何参数,但是您将字符串传递给它。根据我的理解,您的目的是打印到控制台并从中读取。为此,您需要将扫描仪语句拆分为两个。鉴于此:
name = scan.nextLine("Hello, what is your name? ");
把它变成:
System.out.println("Hello, what is your name?"); //Print to console.
name = scan.nextLine(); //Read from it.
另一方也是如此。
答案 2 :(得分:0)
Scanner.nextLine()
不接受任何参数,它只是读一行。
你可以用这个:
System.out.println("Hello, what is your name?");
name = scan.nextLine();
和
System.out.println("Would you like to order some boots, " + name + "? (y/n) ");
input = scan.nextLine();