current position:Home>[100 examples of Java introductory exercises] 02, Java interview questions

[100 examples of Java introductory exercises] 02, Java interview questions

2022-01-26 21:35:10 m0_ fifty-four million eight hundred and sixty-one thousand fou

You can get the string entered by the keyboard , If you want to get int,float And other types still need to be converted . See code for details .

  • Scanner

You can get the string entered by the keyboard , There is ready access to int,float And so on , Very convenient , Widely used . See code for details .

2. Mathematical operation function

Java Of Math Class contains the for executing Basic mathematical operations Properties and methods of , Such as the primary index 、 logarithm 、 Square root and trigonometric function .

Math All methods are defined as static form , adopt Math Class can be called directly in the main function .

Here are a few common functions . See code for details .

  • abs() Returns the absolute value of the parameter .

  • min() Returns the minimum of two parameters .

  • max() Returns the maximum of two parameters .

  • pow() Returns the second power of the first argument .

  • sqrt() Find the arithmetic square root of the parameter .

  • random() Returns a random number .

  • Math.PI PI

3. Type conversion

Java Support display and implicit conversion , In practical application, we should be good at using packaging parseXXX() and valueOf() Method .

Special ,char turn int Can pass ascii The way . example :

char ch = ‘9’;

int i=ch-‘0’

System.out.println(i);

Code implementation


《 A big factory Java Analysis of interview questions + Back end development learning notes + The latest architecture explanation video + Practical project source code handout 》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 Full content open source sharing


Three ways to achieve .

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Scanner;

/**

  • Enter a number from the keyboard , Print out the absolute value of the number .

*/

public class question_02 {

public static void main(String[] args) throws IOException {

question_02 question02 = new question_02();

question02.method1();

// question02.method2();

// question02.method3();

}

//System.in

public void method1() throws IOException {

System.out.println(“Please Enter Data:”);

char i = (char)System.in.read();

System.out.println("System.in --> "+Math.abs(i-‘0’));

}

//InputStreamReader

public void method2() throws IOException {

System.out.println(“Please Enter Data:”);

//new One InputStreamReader object

InputStreamReader is = new InputStreamReader(System.in);

//BufferedReader The method of parametric construction of

BufferedReader br = new BufferedReader(is);

// Read a line , Throw an exception

String line = br.readLine();

System.out.println("InputStreamReader --> "+Math.abs(Integer.parseInt(line)));

}

//Scanner

public void method3(){

System.out.println(“Please Enter Data:”);

Scanner sc = new Scanner(System.in);

// Read int

int i = sc.nextInt();

//String s = sc.nextLine(); Read string input

//float f = sc.nextFloat(); Read float Type input

System.out.println("Scanner --> "+Math.abs(i));

}

}

Output results

copyright notice
author[m0_ fifty-four million eight hundred and sixty-one thousand fou],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262135094469.html

Random recommended