方法next()在String类型中未定义。

huangapple go评论81阅读模式
英文:

The method next() is undefined for the type String

问题

我是新手程序员,我正在尝试从一个方法中调用一个扫描器,并在编译之前收到错误,我在这个论坛或其他地方找不到我能理解的答案。

public class BL {
    static double outcome = 0;
    private static String input;

    private static void main() {
        Scanner input = new Scanner(System.in);
    }

    public static void sort() {
        **input = input.next();** // 错误出现在 .next() 下面
    }
}

非常感谢!

英文:

I'm new to programming and I'm trying to call a scanner from a method- and receiving the error before compiling, I could not find an answer to this in this forum or outplace, that I could understand

public class BL {
	static double outcome = 0;
	private static String input;

	private static void main() {
		Scanner input = new Scanner(System.in);
	}
	public static void sort() {
		**input= input.next();**     // the error is under .next()
	}

Thanks upfront!

答案1

得分: 1

我猜你想要

public static void sort() {
    Scanner in = new Scanner(System.in);
    input = in.next(); //或者使用in.nextLine()来获取整行输入
}

Scanner 是用于从键盘读取输入的类。它是拥有 next() 方法的类,该方法获取用户输入(直到找到空格或新行)并将其转换为 String

英文:

I guess you want

public static void sort() {
    Scanner in = new Scanner(System.in);
    input = in.next(); //or in.nextLine() for the whole line
}

The Scanner is the class that reads input from the keyboard. It is the class who has the method next(), that retrieves the input of the user (until it finds a space or a new line) converted in a String.

huangapple
  • 本文由 发表于 2020年7月28日 02:42:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63121582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定