import java.util.Scanner;
public class CircleArea {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int str1 = sc.nextInt();
System.out.println("输入的数据为:" + str1);
}
}
nextFloat():
import java.util.Scanner;
public class CircleArea {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
float str1 = sc.nextFloat();
System.out.println("输入的数据为:" + str1);
}
}
nextDouble():
import java.util.Scanner;
public class CircleArea {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
Double str1 = sc.nextDouble();
System.out.println("输入的数据为:" + str1);
}
}
三、Scanner 的具体使用例子(计算圆面积)
代码示例如下:
import java.util.Scanner;
public class CircleArea {
public static void main(String[] args) {
// TODO Auto-generated method stub
double pi = 3.14, Area = 0.0;
Scanner sc = new Scanner(System.in);
System.out.print("请输入圆的半径:");
double R = sc.nextDouble();
Area = R * R * pi;
System.out.println("圆面积:" + Area);
sc.close(); // 关闭Scanner对象
}
}