从零自学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
原文地址:https://www.cnblogs.com/redlogic/p/8570476.html