Basic Java program practice

Open the way for programmers to learn. In seven days, I feel that the technology industry is finally based on actual combat. Through more practice procedures, I have a deeper understanding of knowledge. In addition, as a lazy person, it's too uncomfortable to memorize by rote. It's simply not too comfortable to improve understanding by hands.

1, Basic output format

The statement is easy to master, mainly the naming format. As a standard programmer, the naming standard is very important. Many companies take the naming standard as one of the recruitment conditions when recruiting.

Naming conventions:

  1. Package name: all lowercase domain name
  2. Project Name: all lowercase     javafirst
  3. Class name: the first letter of each word is uppercase, and the other letters are lowercase     MyWork
  4. Variable: the first word is all lowercase, and the first letter of each subsequent word is uppercase       int myWork=···
//Basic structure of Java program: defining a class
//Access modifier definition class custom class name
public class MyWork {
    //Main method: program entry
    //Access modifier static method has no return value method name (parameter)
	public static void main(String[] args) {
//Direct output does not define what is output

        //Output integer direct write
		//Output decimal write directly
		//Add English double quotation marks to the output statement
		System.out.println("You're not me. Don't understand my choice");
		//If the sum is all numbers, add them
		//Spliced numbers and text are connected by a plus sign
		//Composite + splicing  
		//Splicing + splicing

2, Basic algorithm application

1. Operation

Add+       Minus-       Ride*         Except/        Remainder% (take the remainder of the division of two numbers, also known as modular operation)

The priority of the algorithm is the same as that of primary school mathematics. Multiplication and division are followed by addition and subtraction. Without special parentheses, operations are carried out in sequence according to the priority,% modular operation and multiplication and division are at the same level.

public class MyFirst {
	public static void main(String[] args) {
		//Define variables
		//Syntax: data type variable name
		//The bottom layer allocates space in memory to store data according to the data type
		int a=9;
        int b=5;
        int sum;  //Used to store calculation results
//Assign a value to the variable. The variable corresponds to the memory address. The program uses the variable name = as the assignment operator to assign the right value to the left variable

        //The final program output answer is only the result of modular operation 4
        //If you want each operation to have a result, there must be an output under each operation, otherwise you need to define four more variables,
The results of each operation are stored separately, and then added in the output section at the end


2. i + + and + + i

public static void main(String[] args) {
        int i=1;
        int sum; //Define variables and store data
        sum=i++; //++In the current position, use the original value first, and then + 1
        //Here, i is used to calculate the sum value, and i is added with 1, so the answer is 1

        sum=++i;  //++Before, current position, first + 1, then use
        //If the last two lines above are not deleted, the initial value of i here will be 2, and then i plus 1 will calculate the sum value, and the answer will be 3


3. Practice

Let two variables have initial values, and then the output variable values are exchanged with each other

public class Work {
	public static void main(String[] args) {
		int a=1;
		int b=2;	
//The first method defines the output a as the value of b, and defines the output b as the value of A
		int m=1;
		int n=2;
		int x=m;
//In the second method, add the third variable x, put the m value in first, then put the n value in M, and finally put the x value in n


3, Call class

In the basic mechanism of Java, the default classes do not need to be added, such as addition, subtraction, multiplication and division; However, if you need to call other functions, you need to import the class library to use the corresponding functions.

//Declare package: tell the system which package the current class is in

//Import package: we need to use the classes defined in other packages in our project, and tell the system which package this class is in
import java.util.Scanner;

public class Work {
	public static void main(String[] args) {
	//To create a Scanner class object is to declare a variable of Scanner type
	Scanner scanner=new Scanner(;
	//Use the program to promote what people should do. First output the prompt statement
	System.out.println("Please enter Java achievement");
    //Enter an integer; Use the scanner object to call the nextInt() method
	//This method can accept the integer entered by the user from the keyboard
	int java=scanner.nextInt();
	System.out.println("Please enter html achievement");
	int html=scanner.nextInt();
	System.out.println("Please enter sql achievement");
	int sql=scanner.nextInt();
//Output partition makes the interface beautiful
	System.out.println("- -— - - -— -— -—");
	int sum=java+html+sql;
	System.out.println("- -— - - -— -— -—");
	System.out.println("Total score="+sum);


  1. \t jumps to the next tab output bit   This command means to empty one byte and eight bits.
  2. \n line feed
  3. \\ \
  4. \"

4, Practice

1. Enter any quantity to indicate how many days, how many weeks, and how many days are left?

import java.util.Scanner;

public class Work3 {
	public static void main(String[] args) {
	Scanner scanner=new Scanner(;

	System.out.println("Please enter the number of days");
	int days=scanner.nextInt();
	System.out.println("Remaining days:"+(days%7));

2. Enter the radius to calculate the area of the circle

import java.util.Scanner;

public class Work4 {
	public static void main(String[] args) {
	Scanner scanner=new Scanner(;

	System.out.println("Please enter a radius");
	int r=scanner.nextInt();
	System.out.println("the measure of area:"+(r*r*3.14));

 ———— Hard work to solve food and clothing, knowledge changes fate, continue to adhere to!!!

Tags: Java

Posted on Sat, 23 Oct 2021 22:09:36 -0400 by raymedia