JAVA中如何实现从键盘中输入一个整数?
2个回答
展开全部
首先需要用到Scanner 类,所以要在最开始import java.util.Scanner;
然后在需要用到的方法里面创建个Scanner 物体,Scanner sc = new Scanner(System.in);
然后就可以用Scanner的方法nextInt()了: sc.nextInt();
举例如下:
import java.util.Scanner; //import the Scanner class
public class sampleInput{
public static void main(String[] args){
Scanner sc = new Scanner(System.in); //create the new Scanner object sc
System.out.println("please input an integer");
int a = sc.nextInt(); //use the nextInt() method to read in
System.out.println("Thanks for your help!" + a); //print the input integer
}
}
然后在需要用到的方法里面创建个Scanner 物体,Scanner sc = new Scanner(System.in);
然后就可以用Scanner的方法nextInt()了: sc.nextInt();
举例如下:
import java.util.Scanner; //import the Scanner class
public class sampleInput{
public static void main(String[] args){
Scanner sc = new Scanner(System.in); //create the new Scanner object sc
System.out.println("please input an integer");
int a = sc.nextInt(); //use the nextInt() method to read in
System.out.println("Thanks for your help!" + a); //print the input integer
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.*
public class test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("请输入一个整数: ");
int num = input.nextInt();
System.out.println("你输入的整数是:"+num);
}
}
public class test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("请输入一个整数: ");
int num = input.nextInt();
System.out.println("你输入的整数是:"+num);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询