我使用Eclipse编程,它告诉我,如果要输出的“输入字符串”
不能让一个静态引用非静态字段输入
为什么变量是静态的最后块?
import java.util.Scanner;
public class NameSort {
String Input;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
System.out.println("Inupt some Text");
while (sc.hasNextLine()){
String Input = sc.nextLine();
System.out.println(Input);
if (Input.toLowerCase().equals("ende")) {
System.exit(0);
sc.close();
}
}
} finally {
if (sc != null)
sc.close();
System.out.print(Input);
}
}
}
在Java中,你不能使用/调用从非静态变量/方法的static
方法。此外,比下面的代码等,您的代码的其余部分是无用的:
import java.util.Scanner;
public class NameSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input;
System.out.println("Inupt some Text");
while (!(input = sc.nextLine()).equals("ende")) {
System.out.println(input);
}
}
}
一个运行示例:
Inupt some Text
hello
hello
hi
hi
ende
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句