java学习日记(一)------输入Scanner类

java学习日记(一)------输入Scanner类

Java使用System.out作为标准输出设备,使用System.in作为标准输入设备。当键盘作为控制台输入时,可以使用Scanner创建一个对象,读取System.in的输入,再通过input.nextdatatype()方法读取键盘输入的值。
具体的代码演示为:

import java.util.*
//Scanner类在java的util包中,使用前需导入
Scanner input = new Scanner (System.in);
//创建一个Scanner对象,并将其引用赋值给变量input
int a = input.nextInt();
//读取整型值int
double b = input.nextDouble();
//读取双精度浮点数double

练习:计算圆的面积

package computearea;
import java.util.Scanner;
	//扩展程序,使用Scanner类从键盘接受数据	
public class ComputeArea {
	public static void main(String arg[]) {
		Scanner input = new Scanner(System.in);
		//system.out---标准输出程序;system.in---标准输入程序	
		System.out.print("enter the number for redius");
		double radius = input.nextDouble();
		double area;
		//定义radius(半径),area(面积)
		area =  radius *  radius * 3.14159;
		//圆的面积计算公式 radius * radius * 3.14159
		System.out.println("The area for the circle of radius " + radius + " is " + area);		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值