必需:无参数,找到:字符串

Required: no arguments, found: String

只是想知道你们是否可以帮助我解决我一直在努力解决的这个错误 修复过去一个小时左右的时间。

我在这段代码中调用错误的函数:

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

如果有人能指出我做错了什么,那就太好了!谢谢!

你应该这样写,因为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。所以这里的参数表示方法参数并且 required 被提到为 no arguments,而不是 String 即“你好,你叫什么名字?”。

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();

我认为您混淆了 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.

另一个也一样。