iT邦幫忙

2021 iThome 鐵人賽

DAY 5
0
自我挑戰組

從零開始學Java系列 第 5

Day5 Data types, Variables, and Operators (Ⅰ)

Question:如果給定一個圓的半徑是10,那麼圓面積是多少?
Sol:首先第一個想法是,將10輸入圓面積公式,半徑x半徑x3.14,然後再輸出答案。有了想法之後,再來就是將想法轉換成程式碼了!
Step1:建立一個算面積的class- ComputeAreaDemo
https://ithelp.ithome.com.tw/upload/images/20210917/20140457Vlkx6T8giu.jpg
Step2:Input 半徑(r)為10
int r = 10; //int表示宣告,r=10表示初始化,=表示指定
Step3:寫公式A=rr3.14將10輸入
double A=rr3.14;
Step4:印出結果
System.out.println(A);
https://ithelp.ithome.com.tw/upload/images/20210917/20140457Mcpr6NyPX2.jpg

在這支程式我們使用兩種primitive data types : int,double.
接下來將開始詳細介紹!

Variable Declaration
First, we name the variable, say x
We then need to determine a proper type for x..
EX: int x; // x is a variable declared an integer type.

Naming Rules
●開頭不可以是數字
●不可以有reserved word EX:class,double,int,public,float…
●字和字中間不可以有空格
●不可以包含運算符號 EX:+,-,*,/

Data Types
●每一個變數都需要一個Type EX:int x,double y
●每一個語句都要一個Type
●Java是靜態型別(static-typed);Python是動態型別
●還有primitive types(原生型別), and reference types(參考型別)


上一篇
Day4 Hello World! &基本介紹
下一篇
Day6 Data types, Variables, and Operators (Ⅱ)
系列文
從零開始學Java30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

0
WinnieHsu
iT邦新手 5 級 ‧ 2021-09-17 17:48:09

怎麼這麼厲害?

我要留言

立即登入留言