本文實例講述了Java基于Scanner對象的簡單輸入計算功能。分享給大家供大家參考,具體如下:
問題及代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/* *Copyright (c)2015,西南大學計信院 *All rights reserved. *文件名稱:Computearea.java *作 者:高碩 *完成日期:2015年10月14日 *版 本 號:v1.0 *問題描述:輸入半徑求面積。 *程序輸入:半徑。 *程序輸出:面積。 */ package practice_01; import java.util.Scanner; //Scanner is the java.util package public class Computearea { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println( "服務器之家測試結果:" ); //create a new Scanner object Scanner input= new Scanner(System.in); //enter the radius System.out.println( "please input the radius" ); double radius=input.nextDouble(); double area=radius*radius* 3.14 ; System.out.println( "the raisus is" + radius + "the area is " +area); } } |
運行結果:
知識點總結:
建立Scanner對象進行輸入。以及nextInt()
nextDouble()
讀入不同類型。
心得體會:
java輸入好麻煩,先讀Scanner包,再建立對象。還有要注意println輸出時的空格。希望本文所述對大家java程序設計有所幫助。
原文鏈接:http://blog.csdn.net/acingdreamer/article/details/49130285