java learning notes

Data type conversion:
    
Small Data Types - ----> Large Data Types - Automatic Type Conversion

    
Large Data Types - -- - > Small Data Types -- Forced Type Conversion

Mandatory type conversion format:
            
Small Data Type Variable Name= (Small Data Type) Large Data Type
    
Details for data type conversion:
1. All byte, short, char data types are automatically converted into int data type and re-calculated when they are operated.
2. When two different data types are run, the result depends on the large data type.

Connector

 

Connector's function: Let any data can be spliced with strings.
If the + sign is used for strings, then the + sign is a connector, not an additive function.

Attention to connectors: Any type of data is connected to a string using a connector, and the result is string-type data.

Comparison operator

COMPARISON OPERATOR: The result of the COMPARISON OPERATOR is that it returns a Boolean value.

== (Judging whether it is equal to)
== When comparing data of two basic data types, the comparison is whether the values stored by the two variables are identical.
== When comparing the data of two reference type variables, the comparison is whether the memory addresses recorded by the two reference type variables are identical.

/*
Use XOR to encrypt image data.

*/
import java.io.*;
class ImageTest 
{
	public static void main(String[] args)  throws Exception
	{
		//Find the picture file
		File inFile = new File("e:\\Encrypted pictures.jpg");
		File outFile = new File("e:\\Decrypted pictures.jpg");

		//Establish a data channel to allow the binary data of the picture to flow in
		FileInputStream input = new FileInputStream(inFile);
		FileOutputStream output = new FileOutputStream(outFile);
		//As you read, you can write out the data you read, different from or different from the data you read.

		int content = 0; //This variable is used to store read data.
		while((content = input.read())!=-1){  // If it is not at the end of the file, read on and the read data is stored in the content variable.
			output.write(content^12);
		}
	
			
		//close resource
		output.close();
		input.close();

	}
}

 

/*
Requirements: Keyboard input a score, according to the score output corresponding grade.

For example: 100-90 A grade 89-80 B grade.... E grade


Procedures for accepting keyboard input data:
	1. Create a scanner object.
	2. Call the nextInt method of the scanner object to scan the data.
	3. Import packages.
*/
import java.util.*;
class Demo3 
{
	public static void main(String[] args) 
	{
	
		//Create a scanner
		Scanner scanner = new Scanner(System.in);
		//Call the scanner to scan the data entered by the keyboard
		
		System.out.println("Please enter a score:");
		int score = scanner.nextInt(); //A num variable is defined to receive scanned content.
		
		if(score>=90&&score<=100){
			System.out.println("A Grade");
		}else if(score>=80&&score<=89){

			System.out.println("B Grade");
		}else if(score>=70&&score<=79){
			
			System.out.println("C Grade");
		}else if(score>=60&&score<=69){
			
			System.out.println("D Grade");
		}else if(score>=0&&score<=59){
			
			System.out.println("E Grade");
		}else{
			System.out.println("A make-up exam..");
		}
		
	
	}
}

 

/*
Requirement 2: Realize the guess number game. If you don't guess right, you can continue to enter the guess number. If you guess right, stop the program.

You can only guess three times at most. If you have one last chance left, remind the user.

*/
import java.util.*;
class Demo8 {

	public static void main(String[] args) 
	{
		//Create a random number object
		Random random = new Random();
		//Call the nextInt method of the random number object to generate a random number
		int randomNum = random.nextInt(10)+1; //Random numbers are required to be 1-10
		//Create a scanner object
		Scanner scanner = new Scanner(System.in);
		
		while(true){
			System.out.println("Please enter the number you want to guess.:");
			//Call the nextInt method of the scanner to scan a number
			int guessNum = scanner.nextInt();
			if (guessNum>randomNum){
				System.out.println("Guess big...");
			}else if(guessNum<randomNum){
				System.out.println("Guess it's small..");	
			}else{
				System.out.println("Congratulations. You guessed right.`..");	
				break;
			}
		}
		
	}
}
/*
Escaped Characters: Special Characters use "" to convert it into the output of the character itself, then the character using "" is called a transfer character.

Requirement: Print a hello world on the console 

Common escape characters are:
	\b	Backspace (Backspace key)
	\t	Tab    Tables (the purpose of tabs is to align a column) A tab is generally equal to four spaces.
	
	\n	Line feed
	\r	Enter and move the cursor to the top of the line.
	
Note: If you need to change lines when operating files on windows system, you need to usern together.
If you need to change lines on other operating systems, you only need \ n.

*/
import java.io.*;
class Demo16 {

	public static void main(String[] args) throws Exception
	{
		//System.out.println("Hello Haha rworld!");

		File file = new File("F:\\a.txt");
		FileWriter  out = new FileWriter(file);
		out.write("Hello everyone\r\n");
		out.write("How do you do");
		out.close();


	}
}
/*
break, 

break Scope of application: Can only be used in switch or loop statements.

break Effect:
	1. break The purpose of a switch statement is to end a switch statement.
	2. break The function used in a loop statement is to end the loop statement in which it is currently located.

Written Test Title: Break is currently located in the inner for cycle, how can we let break act on the outer for cycle.
	Markable Solution


The naming of tags should only conform to the naming rules of identifiers.




*/
class Demo17 
{
	public static void main(String[] args) 
	{
		aaa:for(int j = 0 ; j<3 ; j++){ // j=0 outer for loop
			bbb:for(int i = 0 ; i< 2 ; i++){  // i=0 inner for loop
				System.out.println("hello world"); // 1	
				break aaa;
			}
			
		}


	}
}

 

 

 

Tags: Java Windows

Posted on Tue, 13 Aug 2019 22:35:57 -0700 by mzanimephp