From scratch learn JAVA-3. Store and modify variable information in the program, java-3 variable

Source: Internet
Author: User

From scratch learn JAVA-3. Store and modify variable information in the program, java-3 variable

1. Create a variable;

2. Use different types of variables;

3. Store values in variables;

4. Use variables in mathematical expressions;

5. Assign the value of a variable to another variable;

6. Increment/decrease the value of the variable.

Program Variable: use different types of variables and assign initial values

1 package com. jsample; 2 3 public class Variable {4 public static void main (String [] args) {5 int tops; // integer Variable 6 float gpa without initial values; // Single-precision floating point variable 7 char key = 'C' without initial values; // String productName = "Larvets "; // string variable 9 whose initial value is "Larvets"} // This program has no output 10}
View Code

Program PlaneWeight: calculates and outputs The Weight Values of humans on different planets in the solar system

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 () will not automatically wrap, println () Will 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

Program PlaneWeightPlus: calculates and outputs the weights of people on different planets in more solar systems

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 () will not automatically wrap, println () Will 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

Program SquareSum: calculates the sum of squares of integer x and y and outputs

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); // sum of squares of output 8} 9}
View Code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.