1.創建變數; 2.使用不同類型的變數; 3.在變數中存儲值; 4.在數學表達式中使用變數; 5.把一個變數的值賦給另一個變數; 6.遞增/遞減變數的值。 程式Variable:使用不同類型的變數並賦初值 1 package com.jsample; 2 3 public class Variabl ...
1.創建變數;
2.使用不同類型的變數;
3.在變數中存儲值;
4.在數學表達式中使用變數;
5.把一個變數的值賦給另一個變數;
6.遞增/遞減變數的值。
程式Variable:使用不同類型的變數並賦初值
1 package com.jsample; 2 3 public class Variable { 4 public static void main(String[] args){ 5 int tops;//無初值的整型變數 6 float gpa;//無初值的單精度浮點型變數 7 char key = 'C';//初值為‘C’的字元型變數 8 String productName = "Larvets";//初值為“Larvets”的字元串型變數 9 }//本程式無輸出 10 }View Code
程式PlaneWeight:計算並輸出人在太陽系不同行星上的體重值
1 package com.jsample; 2 3 public class PlaneWeight { 4 public static void main(String[] args){ 5 System.out.print("Your weight on Earth is "); 6 double weight = 178; 7 System.out.println(weight);//print()不會自動換行,println()會 8 9 System.out.print("Your weight on Mercury is "); 10 double mercury = 178 * .378; 11 System.out.println(mercury); 12 13 System.out.print("Your weight on Moon is "); 14 double moon = 178 * .166; 15 System.out.println(moon); 16 17 System.out.print("Your weight on Jupiter is "); 18 double jupiter = 178 * 2.364; 19 System.out.println(jupiter); 20 } 21 }View Code
程式PlaneWeightPlus:計算並輸出人在更多太陽系不同行星上的體重值
1 package com.jsample; 2 3 public class PlaneWeightPlus { 4 public static void main(String[] args){ 5 System.out.print("Your weight on Earth is "); 6 double weight = 178; 7 System.out.println(weight);//print()不會自動換行,println()會 8 9 System.out.print("Your weight on Mercury is "); 10 double mercury = weight * .378; 11 System.out.println(mercury); 12 13 System.out.print("Your weight on Moon is "); 14 double moon = weight * .166; 15 System.out.println(moon); 16 17 System.out.print("Your weight on Jupiter is "); 18 double jupiter = weight * 2.364; 19 System.out.println(jupiter); 20 21 System.out.print("Your weight on Hesperus is "); 22 double hesperus = weight * .907; 23 System.out.println(hesperus); 24 25 System.out.print("Your weight on Uranus is "); 26 double uranus = weight * .889; 27 System.out.println(uranus); 28 } 29 }View Code
程式SquareSum:計算整型數x,y的平方和並輸出
1 package com.jsample; 2 3 public class SquareSum { 4 public static void main(String[] args){ 5 int x = 1; 6 int y = 1; 7 System.out.println(x * x + y * y);//輸出平方和 8 } 9 }View Code