Chapter 1 of java - Basic Grammar

Posted by paulnaj on Tue, 04 Feb 2020 03:20:46 +0100

java learning resources
https://www.bilibili.com/video/av37413483

1. Process control statements

1. Sequential structure

From top to bottom, from left to right, brackets are counted first;

public class Test {
	public static void main(String[] args) {
		//Execute from top to bottom
		int a = 10;
		System.out.println(a);
		
		//Execute from left to right
		System.out.println("5+5="+5+5);//5+5=55
	}
}

2. Select Structure

if statement execution order:

Judge relationship expression 1 first, if true executes body 1, then ends; if false, relationship expression 2; if none of them satisfies the end of an else statement.

switch statement execution order:

Constant values are found based on the key values passed in, and matching executes the related statement body until the end of break; if none of them is satisfied with executing the statement in default.

key type: byte,short,char,int,String

3. Cycle structure

for statement execution order:

Initialization statement-->Decision statement-->Loop body statement-->Control statement-->Decision statement-->Loop body statement...

Initialization statements execute only once

Variables defined in the initialization statement have scopes only within for loop brackets

public class Tesst1 {
	public static void main(String[] args) {
		int c = 1;
		for (int i = 0,c1 =0 ; i < 10;i++,c++,c1++) {//i c1 is defined in for and can only be used for the for scope. c is defined in the main method and can be used for the main method scope
			System.out.println("You are so handsome");
			System.out.println(c1);
		}
		System.out.println(c);
	}
}

the while statement

		int i=1;  //Initialization statement
		while(i<=100){
			//Circulating body;
			//Control condition statement;
		}

do...while syntax format

	Initialization statement;
	do{
		Loop body statement;
		Control condition statement;
	} while;

Dead cycle
​ for(;😉{}
​ while(true){}

Exercises

Which of the following parameters cannot be used for the switch statement?C
A. byte b=1; B. int i= 1; C.boolean b=false; D.char c='a';

  1. What is the output of the following program after execution?C
    for(int I=0;I<2;++I){
    System.out.println("I is "+I);
    }
    A. I is 1
    ​ I is 2
    B. I is 1
    C. I is 0
    I is 1
    D. I is 0

  2. Sum numbers between 1 and 100 that cannot be divided by 3

    public class Test{
    	public static void main(String[] args) {
    		//Sum numbers between 1 and 100 that cannot be divided by 3
    		int sum = 0;
    		for(int i = 1 ;i <100;i++){
    			if( i%3 != 0){
    				sum+=i;
    			}
    		}
    		System.out.println(sum);
    	}
    }
    
  3. In 2006, 80,000 trainees were trained, an annual increase of 25%. What year will the number of trainees reach 200,000?

    public class Test{
    	public static void main(String[] args) {
    		//In 2006, 80,000 trainees were trained, an annual increase of 25%. What year will the number of trainees reach 200,000?
    		int people = 8;
    		int year = 2006;
    		while(people <20){
    			people*=1.25;
    			year+=1;
    		}
    		System.out.println(year);
    		System.out.println(people);
    	}
    }
    
  4. Keyboard input month and output season with switch statement
    // Spring from March to May
    // June-August is summer
    // September-November is autumn
    // December-February is winter

import java.util.Scanner;
// Keyboard input month and output season with switch statement
		// March-May is spring
		// June-August is summer
		// September-November is autumn
		// December-February is winter
public class Test {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Please enter a month number");
		int month = input.nextInt();
		switch (month) {
		case 3:
		case 4:
		case 5:
			System.out.println("Spring");
			break;
		case 6:
		case 7:
		case 8:
			System.out.println("Summer");
			break;
		case 9:
		case 10:
		case 11:
			System.out.println("Autumn");
			break;
		case 12:
		case 1:
		case 2:
			System.out.println("winter");
			break;
		default:
			System.out.println("Wrong month");
			break;
		}
	

	}
}

  1. Print out all the daffodils

    The number of daffodils is a three-digit number whose cube is equal to the number itself, for example, 153 = 13+53+3^3

import java.util.Scanner;
public class Test {
	//Print out all the daffodils   
	//The number of daffodils is a three-digit number whose cube is equal to the number itself, for example, 153 = 1^3+5^3+3^3
	public static void main(String[] args) {
			for(int i = 100;i<1000;i++){
				int baiwei = i/100;
				int shiwei =i%100/10;
				int gewei = i%10;
				if(i == baiwei*baiwei*baiwei + shiwei*shiwei*shiwei + gewei*gewei *gewei){
					System.out.println(i);
				}
			}
	}
}
  1. Find all numbers between 1 and 100 that contain 7 or are divided by 7
public class Test2 {
	//Find all numbers between 1 and 100 that contain 7 or are divided by 7
	public static void main(String[] args) {
		for(int i =1;i<=100;i++){
			if(i/10 ==7){
				System.out.println(i);
			}else if(i%7 ==0){
				System.out.println(i);
			}else if(i%10==7){
				System.out.println(i);
			}
		}
	}
}
  1. A ball falls freely from a height of 100 meters and bounces back half its original height each time it falls.
    When asked how many meters the 10th landing had passed and how high the 10th bounce was
public class Test3 {
	//A ball falls freely from a height of 100 meters and bounces back half its original height each time it falls.
	//When asked how many meters the 10th landing had passed and how high the 10th bounce was
	public static void main(String[] args) {
		int count = 0;
		double sum = 0;
		double huitan = 100;
		while(true){
			count ++;
			if(count ==1){
				sum = 100;
			}else{
				sum+= huitan*2;
			}
			huitan /=2;
			if(count ==10){
				break;
			}
		}
		System.out.println(sum);
		System.out.println(huitan);
	}
}

  1. Find all odd and even sums between 1 and 100

    public class Test4 {
    //Find all odd and even sums between 1 and 100
    	public static void main(String[] args) {
    		int ji = 0;
    		int ou = 0;
    		for(int i =1;i<=100;i++){
    			if(i%2==0){
    				ou+=i;
    			}else{
    				ji+=i;
    			}
    		}
    		System.out.println(ji);
    		System.out.println(ou);
    	}
    }
    
  2. Print lowercase A-Z using a for loop Uppercase A-Z

    import java.util.Scanner;
    
    public class Test {
    	public static void main(String[] args) {
    		//Print lowercase A-Z using a for loop Uppercase A-Z
    		for (int i = 'a'; i < 'z'; i++) {
    			System.out.println((char)i);
    		}
    		System.out.println("---------------------------------------");
    		for (char i = 'A'; i < 'Z'; i++) {
    			System.out.println(i);
    		}
    	}
    }
    
  3. The keyboard enters the value of x, calculates y and outputs it.
    ​ x>=3 y = 2x + 1;
    ​ -1<=x<1 y = 2x;
    ​ x<-1 y = 2x – 1;

    import java.util.Scanner;
    
    public class Test {
    	public static void main(String[] args) {
    		//		The keyboard enters the value of x, calculates y and outputs it.
    		//		x>=3	y = 2x + 1;
    		//		-1<=x<1	y = 2x;
    		//		x<-1	y = 2x – 1;
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Please enter a number");
    		int x = sc.nextInt();
    		int y = 0;
    		if (x >= 3) {
    			y = 2*x + 1;
    		}else if (-1<=x && x<1) {
    			y = 2*x;
    		}else if (x < -1) {
    			y = 2 * x - 1;
    		}else if (x == 1 || x == 2) {
    			System.out.println("No corresponding calculation formula");
    			return;  //End the method directly, and the external System.out.println(y) statement cannot be executed at this time (the reason it cannot be executed is that return is blocked, not y has no value)
    		}
    		System.out.println(y);
    
    	}
    }
    
    
  4. Find out how many numbers between 1 and 1000 can be divided by 7 or 3 or 5, respectively

    public class Test6 {
    //Find out how many numbers between 1 and 1000 can be divided by 7 or 3 or 5, respectively
    	public static void main(String[] args) {
    		int chu7 = 0;
    		int chu3 = 0;
    		int chu5 = 0;
    		for(int i =1;i<=1000;i++){
    			if(i %7 ==0){
    				chu7+=1;
    			}
    			if(i%3==0){
    				chu3+=1;
    			}
    			if(i%5==0){
    				chu5+=1;
    			}
    			
    		}
    		System.out.println("Divided by 7 is"+chu7);
    		System.out.println("Divided by 3 is"+chu3);
    		System.out.println("Divided by 5 is"+chu5);
    	}
    }
    
    
  5. Assume someone has 100,000 cash. A fee is required for every intersection he crosses. The rule is to pay 5% for every intersection when his cash is greater than 50,000. If the cash is less than or equal to 50,000, 5,000 for each intersection. Write a program to calculate how many times this person can cross this intersection.

    public class Test7 {
    //Suppose someone has 100,000 cash. A fee is required for every intersection he passes. The payment rule is when his cash is greater than 50,000
    	//5% each time. If the cash is less than or equal to 50,000, 5,000 each time. Write a program to calculate how many times this person can cross this intersection
    	
    	public static void main(String[] args) {
    		double money = 100000;
    		int count =0;
    		
    		while(money >=5000){
    			if(money >50000){
    				money -=money*0.05;
    			}else{
    				money -=5000;
    			}
    			count++;
    		}
    		System.out.println(count);
    		System.out.println(money);
    	}
    }
    
    
  6. There is a monkey and a pile of peaches in the park. The monkey eats half of the total peaches every day and throws away one bad one of the remaining half.On the seventh day, the monkey opened its eyes and found only one peach left.How many peaches were there in the park at first?

    public class Test8 {
    //There is a monkey and a pile of peaches in the park. The monkey eats half of the total peaches every day and throws away one bad one of the remaining half.
    //On the seventh day, the monkey opened its eyes and found only one peach left.How many peaches were there in the park at first?
    	
    	public static void main(String[] args) {
    		int day = 0;
    		int sum = 1;
    		while(true){
    			day+=1;
    			sum=(sum+1)*2;
    			if(day ==7){
    				System.out.println(sum);
    				break;
    			}
    		}
    	}
    }
    
    
  7. Auto Logon Exercise

    Requirements: 1. Console prompts user for password
    2. User Enter Password
    3. If the password entered by the user does not equal 1234, go back to step 1
    4. If the password entered by the user equals 1234, the login is prompted to succeed

    //Method 1
    import java.util.Scanner;
    public class Test {
    //	Requirements: 1. Console prompts user for password
    //				2. User Enter Password
    //				3. If the password entered by the user is not equal to 1234, go back to step 1
    //				4. If the password entered by the user equals 1234, the login is prompted to succeed
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		while(true){
    			System.out.println("Please input a password");
    			String passwd = sc.next();
    			if( "1234".equals(passwd)){
    				System.out.println("Landing Success");
    				break;
    			}
    		}
    	}
    }
    
    
  8. import java.util.Scanner;
    public class Test {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		String password;//Declare variables first
    		do {
    			System.out.println("Please input a password");
    			password = sc.next();
    		}while(!"1234".equals(password));
    		
    		System.out.println("Login Successful");
    	}
    }
    
    

    Snake Eater: Enter a number on the keyboard. If it is 1, it means the snake eats food. Increase the score by 10 and continue typing.
    If you enter a non-eleven number, stop the program and output a score

    import java.util.Scanner;
    public class Test {
    	//Snake Eater: Enter a number on the keyboard. If it is 1, it means the snake eats food. Increase the score by 10 and continue typing.
    	//If you enter a non-eleven number, stop the program and output a score
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int count = 0;
    		while(true){
    			System.out.println("Enter a number");
    			int go = sc.nextInt();
    			if(go ==1){
    				count +=10;
    			}else{
    				break;
    			}
    		}
    		System.out.println("Your score is"+count);
    	}
    }
    
    
  9. Assume that a simple ATM withdrawal process is like this:
    The user is first prompted for a password.
    You can only enter it three times at most, more than three times prompting the user that the password is incorrect.
    Please pick up the card to close the transaction.If the user password is correct,
    Re-prompt the user to enter the withdrawal amount,
    ATM machine can only output 100 yuan of paper currency, the minimum number of withdrawals at a time is required
    100 yuan, up to 1000 yuan.
    If the amount entered by the user meets the above requirements, the printout user obtains the amount of money.
    Finally, the user is prompted to "Complete the transaction, please pick up the card", otherwise the user is prompted to re-enter the amount.
    Assuming the user password is 11111, implement it programmatically.

import java.util.Scanner;
/**
 * Assume that a simple ATM withdrawal process is like this:
The user is first prompted to enter a password, which can only be entered three times at most. If the password is incorrect more than three times, the user is prompted to "get the card" to end the transaction.
If the user password is correct,
Re-prompt the user to enter the withdrawal amount,
ATM The machine can only output 100 yuan of paper currency. The minimum amount of money withdrawn at one time is 100 yuan and the maximum is 1000 yuan.
If the amount entered by the user meets the above requirements, the printout user obtains the amount of money.
Finally, the user is prompted to "Complete the transaction, please pick up the card", otherwise the user is prompted to re-enter the amount.
Assuming the user password is 11111, implement it programmatically. 
 */
public class Test9 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		for(int i=1 ;i <=3 ;i++){
			System.out.println("Please input a password");
			String input = sc.next();
			if("111111".equals(input)){
				System.out.println("Password is correct");
				break; //Exit Loop
			}else if (i ==3){
				System.out.println("Wrong password, please get the card");
				return;  //End Method
			}else{
              System.out.println("Incorrect password, please re-enter, you still have"+(3-i)+"Second Opportunity");  
			}
		}
		while(true){
			System.out.println("Enter withdrawal amount");
			int amount = sc.nextInt();
			if(amount <100 | amount>1000| amount%100!=0){
				System.out.println("Re-enter amount");
			}else{
				System.out.println("take out"+amount+"Amount of money");
				break;
			}
		}
		System.out.println("Transaction Completed");
	}
}
  1. Grade students'test results 90 - 100 as A 80-90 B 70-80 as C 60-70 as D 60 and below as residue
    Use if else and switch to implement //if else to write your own
    ​ 90 - 99 80-89 70 -79 60-69 60
import java.util.Scanner;

public class Test{
	public static void main(String[] args) {
		// Grade students'test results 90 - 100 as A 80-90 B 70-80 as C 60-70 as D 60 and below as residue
		// Use ifelse and switch to implement
		// 90 - 99   80-89   70 -79   60-69   60
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter your results");
		int score = sc.nextInt();
		if (score > 100 || score < 0) {
			System.out.println("Your results do not meet the requirements");
			return;
		}
		int a = score / 10;
		switch (a) {
		case 10:
		case 9:
			System.out.println("A");
			break;
			
		case 8:
			System.out.println("B");
			break;
			
		case 7:
			System.out.println("C");
			break;
			
		case 6:
			System.out.println("D");
			break;
		
		default:
			System.out.println("E");
			break;
		}
		
	}
}

  1. Keyboard enters two numbers and one of the + - / *% operators
    Let two numbers do the corresponding operation based on the entered operator
    Use switch to implement the following change code
import java.util.Scanner;

public class SwitchDemo3 {
	public static void main(String[] args) {

		// Keyboard enters two numbers and one of the + - / *% operators
		// Let two numbers do the corresponding operation based on the entered operator
		// Use switch to implement the following change code

		Scanner sc = new Scanner(System.in);

		System.out.println("Please enter the first number");
		int a = sc.nextInt();

		System.out.println("Please enter a second number");
		int b = sc.nextInt();

		System.out.println("Please enter the operation you want to select:+ - * / %");
		String s = sc.next();

		switch (s) {
		case "+":
			System.out.println(a + b);
			break;

		case "-":
			System.out.println(a - b);
			break;
		case "*":
			System.out.println(a * b);
			break;
		case "/":
			System.out.println(a / b);
			break;
		case "%":
			System.out.println(a % b);
			break;
		default:
			System.out.println("Unsupported operations");
			break;
		}
	}
}

  1. Exercise:

    Complete procedures as required

    for(int x=1; x<=10; x++) {

    if(x%3==0) {

    //Fill in the code here

    }

    System.out.println("Java Basic Class");

    }

    I want to output two times from the console: "Java Basic Class" should be filled in: break or return

    I want to output 7 times in the console: "Java Basic Class" should be completed:continue

    I want to output 13 "java basic classes" in the console, which should be completed: System.out.println (Java basic classes);

  2. Xiaojuan's mother gives her 3 yuan of money every day, and she will save it. However, whenever the number of days on this day is a multiple of the 6th or 6th day she saves money, she will spend 5 yuan. Please ask how many days it will take for Xiaojuan to save 100 yuan.

public class Test {
	public static void main(String[] args) {
		// Xiaojuan's mother gives her three yuan a day and she will save it.
		// However, whenever the number of days on this day is a multiple of the sixth or sixth day she saves money, she will spend five yuan.
		// Please ask, after how many days, Xiaojuan can save to 100 yuan
		
		int day = 0;
		int money = 0;
		
		while(money < 100){
			money+=3;
			day++;
			if (day % 6 == 0) {
				money -= 5;
			}
		}
		System.out.println(day);
	}
}

  1. Print 99 multiplication table

    public class Test {
    	public static void main(String[] args) {
    		for (int i = 1; i < 10; i++) {
    			for (int j = 1; j < i+1; j++) {
    				System.out.print(j + " * " + i +" = " + (i * j)+"\t");
    			}
    			System.out.println();
    		}
    	}
    }
    
Five original articles published, 0% praised, 28% visited
Private letter follow

Topics: Java Spring less