所以我不知道如何使用扫描仪作为类的属性,然后使用该属性读取内容。
private Scanner scannerName;
public theClassImUsing(){
in = new Scanner(System.in);
}
System.out.println("Command > ");
String comando = in.nextLine();
System.out.println("You just wrote: " + comando);
答案 0 :(得分:1)
似乎您是Java语言的新手,请稍候片刻。
我认为以下代码可以达到并证明一些观点。
public class ScannerImplementation {
private static Scanner scannerName; /*creating the object. It's static so it can be referenced in method below */
public static void main(String[] args) { //think of this as the method that starts the program
System.out.println("Command > "); //printing commando
String comando = scannerName.nextLine(); //assning what you enter to the String commando
System.out.println("You just wrote: " + comando); //printing the result
}
public void assignScanner() { //method that creates the Scanner
scannerName = new Scanner(System.in);
}
}
您的问题可能不仅仅是您所描述的事情。但我认为在这种情况下,范围是您的问题。在互联网上有很多可以找到的知识,并且是学习Java的绝对核心。
答案 1 :(得分:0)
就像GBlodgett所说的那样,您需要将其放入类中的方法中。
另外,还要补充一点,您创建了Scanner scannerName
但从未使用过。