從零自學JAVA-3.在程式中儲存和修改變數資訊,java-3變數

來源:互聯網
上載者:User

從零自學JAVA-3.在程式中儲存和修改變數資訊,java-3變數

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.