简体   繁体   English

“扫描仪无法解析为一种类型”我该如何解决?

[英]“Scanner cannot be resolved to a type” how can i fix it?

I am taking an online course to learn java and currently we are learning "Getting User Input".我正在参加在线课程来学习 java,目前我们正在学习“获取用户输入”。 this is what ive started with so far...到目前为止,这就是我开始的......

public class mama {
    public static void main(String[] args) {        
        Scanner input;
    }
}

however it keeps saying "Scanner input;"但是它一直说“扫描仪输入;”

what do i do?我该怎么办?

 import java.util.Scanner;
 public class mama{
 public static void main(String[] args) {

 Scanner sc= new Scanner(System.in); 
 System.out.print("Enter input value ");  
 int a = sc.nextInt();  
   }
}

you can get input from the user just like this in your main function.您可以像这样在主 function 中从用户那里获得输入。

You have not assigned any value to the scanner variable, the first step to using a scanner is importing the library, then assigning the scanner a variable like "sc" or "keyboard" (which I use), then assign something the scanner variable.您没有为扫描仪变量分配任何值,使用扫描仪的第一步是导入库,然后为扫描仪分配一个变量,如“sc”或“键盘”(我使用),然后分配一些扫描仪变量。

Step by step breakdown:逐步分解:

You are missing:你错过了:

import java.util.Scanner; 

This is the first step, always remember这是第一步,永远记住

Then do this:然后这样做:

Scanner sc = new Scanner(System.in);

Lastly, assign the "sc" to something, either a string or Int variables.最后,将“sc”分配给字符串或 Int 变量。

For example:例如:

int number = sc.nextInt();

or:或者:

String x = sc.next();

Note: You need to understand what the scanner variables are for every type of variable you assign for example string or Int.注意:您需要了解您分配的每种变量类型的扫描仪变量是什么,例如字符串或整数。

For String it is:对于字符串,它是:

sc.next();

For Int:对于国际:

sc.nextInt();

Same goes for double, float...etc, just change the Int to what you want double,float ......等也是如此,只需将Int更改为您想要的

In the end, your code should look something like this:最后,您的代码应如下所示:

import java.util.Scanner;
public class mama {
public static void main(String[] args) {        
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter desired input");
    int input = sc.nextInt();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM