Scanner sc=new Scanner(System.in)
int j=sc.nextInt()
如果通过使用 nextInt() 方法,此扫描器输入信息中的下一个标记可以解释为默认基数中的一个 int 值。
关键字:一般是用scanner用法。
扩展资料
利用 Scanner 实现从键盘读入integer或float 型数据:
import java.util.*
public class test {
public static void main(String args[])
{
Scanner in=new Scanner(System.in)//使用Scanner类定义对象
System.out.println("please input a float number")
float a=in.nextFloat()//接收float型数据
System.out.println(a)
System.out.println("please input a integer number")
int b=in.nextInt()//接收整形数据
System.out.println(b)
}
}
1.Scanners
=
new
Scanner(System.in)
int
i
=
s.nextInt()
2.
BufferedRader
b
=
new
BufferedReader(new
InputStreamReader(System.in))
int
i
=
Integer.parseInt(b.readLine())
一般我用这两个。
--------------------
s.nextInt()表示只接受int的数据。
还有nextDouble()接收double的数据
因为你输入的内容可能有很多种数据类型,s.nextInt()就自动把你输入的数据转化为int型