JAVA in mathematical functions and string

A trigonometric method .Math class

 

 

import java.util.Scanner;
class test04{
    public static void main(String[]args){
        Scanner scanner=new Scanner(System.in);
            System.out.println(Math.toDegrees(Math.PI/2));
            System.out.println(Math.sin(0));
            System.out.println(Math.toRadians(30));
            System.out.println(Math.cos(0));
            System.out.println(Math.asin(0.5));
            System.out.println(Math.acos(0.5));
            System.out.println(Math.asin(0.5));
    }
}

Output:

 

 

 

Method two exponential class .Math

 

 

import java.util.Scanner;
class test04{
    public static void main(String[]args){
        Scanner scanner=new Scanner(System.in);
            System.out.println(Math.pow(3,2));
            System.out.println(Math.sqrt(4));
            
    }
}

 Output:

 

 

.Math class three rounding methods

 

import java.util.Scanner;
class test04{
    public static void main(String[]args){
        Scanner scanner=new Scanner(System.in);
            System.out.println(Math.ceil(2.1)) ;
            System.out.println(Math.ceil(2.0) );
            System.out.println(Math.ceil(2.0));
            System.out.println(Math.ceil(-2.1)); 
            System.out.println(Math.floor(2.1) );
            System.out.println(Math.floor(2.0) );
            System.out.println(Math.floor(-2.0) );
            System.out.println(Math.floor(-2.1) );
            System.out.println(Math.rint(2.1));
            System.out.println(Math.rint(-2.0)); 
            System.out.println(Math.rint(-2.1) );
            System.out.println(Math.rint(2.5) );
            System.out.println(Math.rint(-2.5));
    }

}

Output:

 

 

Four .min, max abs and methods

例如:
Math.max(2, 3) 返回 3
Math.max(2.5f 3) 返回 3.0 
Math.min(2.5, 4.6) 返回 2.5
Math.abs(-2) 返回 2 
Math.abs(-2.1) 返回 2.1
(int)(Math. random) * 10) -----------返回0~9之间的随机数
5O + (int)(Math.random() * 50)-------------返回50~99之间的一个随机数
  • min and max method returns the number two (int, long, float or double type) of the minimum and maximum values
  • Methods abs returns the absolute value of a number (int, long, float or double type)

 

 

Method class five .Character

 

 E.g:

display:

 

 

Simple Method six .String object

 note:

  • When seeking string length "Welcome to Java". Length () is correct, it is back to 15. Note that "" represents an empty string, and "" .length () 0.
  • Getting characters from a string, the string S in cross-border access character is a common programming errors. To avoid this error, be sure to use the index does not exceed S.length () - 1. For example, s.charAt (s.length ()) will cause a StringlndexOutOfBoundsException exception.

 

Compare Method 7 .String object

 note:

  • If as>,> =, <, or <= comparison operators such as comparing two strings, a syntax error will occur. An alternative approach is to use sl.compareTo (s2) for comparison.
  • s1.compareTo (s2), depending on the distance between the s1 and s2 different from left to right the first character drawn.

 

 

The method comprises obtaining eight .String class substring

 

 

IX. String type programming problem

Question 1: seek times s1, s2 occurs in
        s1 = "abcabcbcacbabbabcba"
        s2 = "ABC"

class Test03{
    public static void main(String[] args){
    question1();
}
    public static void question1(){
        /*
        问题1:求s2在s1中出现的次数
        s1="abcabcbcacbabbabcba"
        s2="abc"
        */
        String s1="abcabcbcacbabbabcba";
        String s2="abc";
        int count=0;
        for(int i=0;i<s1.length()-s2.length()+1;i++){
            String sub=s1.substring(i,i+s2.length());
            if(sub.equals(s2)){
                count++;
            }
        }
        System.out.println("s2在s1中出现了"+count+"次");
    }

result:

 

Question 2: Determine whether the string is a palindrome s
        s = "Shanghai water from the sea."


class Test03{
    public static void main(String[] args){
        question2();
    }



    public static void question2(){
        /*
        问题2:判断字符串s是否是回文
        s="上海自来水来自海上"
        */
        String s="13088888031";
        int left=0;
        int right=s.length()-1;
        boolean flag=true;
        while(true){
            if(s.charAt(left)==s.charAt(right)){
                left++;
                right--;
                if(left>=right){
                    break;
                }
            }else{
                flag=false;
                break;
            }
        }
        System.out.println("s是回文吗:"+flag);

    }
}

 

Output:

发布了11 篇原创文章 · 获赞 1 · 访问量 372

Guess you like

Origin blog.csdn.net/qq_45824565/article/details/104313955