04-數(shù)學函數(shù)深碱、字符和字符串

04-數(shù)學函數(shù)、字符和字符串

  • 4.1 引言

    • 介紹數(shù)學函數(shù)棺蛛、字符和字符串對象怔蚌,并使用他們來開發(fā)程序。
  • 4.2 常用數(shù)學函數(shù)

    • 4.2.1 三角函數(shù)方法
      <center>Math類中的三角函數(shù)方法</center>
    方法 描述
    sin(radians) 返回以弧度為單位的角度的三角正弦函數(shù)值
    cos(radians) 返回以弧度為單位的角度的三角余弦函數(shù)值
    tan(radians) 返回以弧度為單位的角度的三角正切函數(shù)值
    toRadians(degrees) 將以度為單位的角度值轉(zhuǎn)換為以弧度表示
    toDegrees(radians) 將以弧度為單位的角度值轉(zhuǎn)換為以度表示
    asin(a) 返回以弧度為單位的角度的反三角正弦函數(shù)值
    acos(a) 返回以弧度為單位的角度的反三角余弦函數(shù)值
    atan(a) 返回以弧度為單位的角度的反三角正切函數(shù)值
    package chapter04;
    
    public class MathMethod {
        public static void main(String[] args){
            System.out.println(Math.toDegrees(Math.PI));
            System.out.println(Math.toRadians(30));
            System.out.println(Math.sin(0));
            System.out.println(Math.sin(Math.toRadians(270)));
            System.out.println(Math.sin(Math.PI / 6));
            System.out.println(Math.sin(Math.PI / 2));
            System.out.println(Math.cos(0));
            System.out.println(Math.cos(Math.PI / 6));
            System.out.println(Math.cos(Math.PI / 2));
            System.out.println(Math.asin(0.5));
            System.out.println(Math.acos(0.5));
            System.out.println(Math.atan(1.0));
        }
    }
    
    

    結(jié)果顯示:

    180.0
    0.5235987755982988
    0.0
    -1.0
    0.49999999999999994
    1.0
    1.0
    0.8660254037844387
    6.123233995736766E-17
    0.5235987755982989
    1.0471975511965979
    0.7853981633974483
    
    Process finished with exit code 0
    
    
    • 4.2.2 指數(shù)函數(shù)方法
      <center>Math類中的指數(shù)函數(shù)方法</center>
      | 方法 |描述 |
      |--|--|
      |exp(x) |返回e的x次方(e^x) |
      | log(x) | 返回x的自然對數(shù)(Inx=loge(x) |
      | log10(x)| 返回x的以10為底的對數(shù)(log10(x))|
      | pow(a,b) |返回a的b次方(a^b) |
      | sqrt(x) | 對于x≥0的數(shù)字鞠值,返回x的平方根|
    package chapter04;
    
    public class MathMethod {
        public static void main(String[] args){
            System.out.println(Math.exp(3.5));
            System.out.println(Math.log(3.5));
            System.out.println(Math.log10(3.5));
            System.out.println(Math.pow(2,3));
            System.out.println(Math.pow(3,2));
            System.out.println(Math.pow(4.5,2.5));
            System.out.println(Math.sqrt(4));
            System.out.println(Math.sqrt(10.5));
        }
    }
    
    

    結(jié)果顯示:

    33.11545195869231
    1.252762968495368
    0.5440680443502757
    8.0
    9.0
    42.95673695708276
    2.0
    3.24037034920393
    
    Process finished with exit code 0
    
    
    • 4.2.3 取整方法
      <center>Math類中的取整方法</center>
      | 方法 |描述 |
      |--|--|
      | ceil(x) | x向上取整為它最近的整數(shù)媚创。該整數(shù)作為一個雙精度值返回 |
      | floor(x)| x向下取整為它最近的整數(shù)。該整數(shù)作為一個雙精度值返回|
      | rint(x)| x取整為它最接近的整數(shù)彤恶。如果x與兩個整數(shù)的距離相等钞钙,偶數(shù)的整數(shù)作為一個雙精度值返回 |
      | round(x)| 如果x是單精度數(shù),返回(int)Math.floor(x+0.5)声离;如果x是雙精度數(shù)芒炼,返回(long)Math.floor(x+0.5)|
    package chapter04;
    
    public class MathMethod {
        public static void main(String[] args){
            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(4.5));
            System.out.println(Math.rint(-2.5));
            System.out.println(Math.round(2.6f));//return int
            System.out.println(Math.round(2.0));//return long 
            System.out.println(Math.round(-2.0f));//returns int 
            System.out.println(Math.round(-2.6));//return long 
            System.out.println(Math.round(-2.4));//return long 
        }
    }
    
    

    結(jié)果顯示:

    3.0
    2.0
    -2.0
    -2.0
    2.0
    2.0
    -2.0
    -3.0
    2.0
    -2.0
    -2.0
    2.0
    4.0
    -2.0
    3
    2
    -2
    -3
    -2
    
    Process finished with exit code 0
    
    
    • 4.2.4 min、max和abs方法
      • min和max方法用于返回兩個數(shù)(int术徊、long本刽、float和double型)的最小值和最大值。
      • abs函數(shù)以返回一個數(shù)(int、long子寓、float和double)的絕對值暗挑。
    ```java
    package chapter04;
    
    public class MathMethod {
        public static void main(String[] args){
            System.out.println(Math.max(2,3));
            System.out.println(Math.min(2.5,4.6));
            System.out.println(Math.max(Math.max(2.5,4.6),Math.min(3,5.6)));
            System.out.println(Math.abs(-2));
            System.out.println(Math.abs(-2.1));
        }
    }
    
    ```
    結(jié)果顯示:
    
    ```java
    3
    2.5
    4.6
    2
    2.1
    
    Process finished with exit code 0
    
    ```
- 4.2.5 random方法
    - random()方法生成大于等于0.0且小于1.0的double型隨機數(shù)。
    

        ```java
        (int)(Math.random() * 10);//返回0-9之前的一個隨機整數(shù)
                50 + (int)(Math.random() * 50);//返回50-99之間的一個隨機數(shù)
                a + Math.random() * b;//返回a——a+b之間的一個隨機數(shù)斜友,不包括a+b
        ```
- 4.2.6 示例學習:計算三角形的角度
    - 提示用戶輸入三角形三個頂點的x和y坐標值炸裆,然后顯示三個角。
    

        ```java
        package chapter04;
        
        import java.util.Scanner;
        
        public class ComputeAngles {
            public static void main(String[] args){
                Scanner input = new Scanner(System.in);
        
                System.out.print("Enter three points: ");
                double x1 = input.nextDouble();
                double y1 = input.nextDouble();
                double x2 = input.nextDouble();
                double y2 = input.nextDouble();
                double x3 = input.nextDouble();
                double y3 = input.nextDouble();
        
                double a = Math.sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
                double b = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
                double c = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
        
                double A = Math.toDegrees(Math.acos((a * a - b * b - c * c) / (-2 * b * c)));
                double B = Math.toDegrees(Math.acos((b * b - a * a - c * c) / (-2 * a * c)));
                double C = Math.toDegrees(Math.acos((c * c - b * b - a * a) / (-2 * b * a)));
        
                System.out.println("The three angles are " + Math.round(A * 100) / 100.0 + " " + Math.round(B * 100) / 100.0 + " " + Math.round(C *100) / 100.0);
            }
        }
        
        ```
  • 4.3 字符數(shù)據(jù)類型和操作
    • 4.3.1 Unicode和ASCII碼
      • 字符數(shù)據(jù)類型用于表示單個字符鲜屏。
      • 字符串字面值必須括在雙引號中烹看。而字符字面值是括在單引號中的單個字符。因為“A”是一個字符串洛史,而‘A’是一個字符惯殊。
      • 自增和自減操作符也可用在char型變量上,這會得到該字符之前或之后的Unicode字符也殖。例如土思,下面的語句顯示字符b:
        ```java
        char ch = 'a';
        System.out.println(++ch);
        ```
- 4.3.2 特殊字符的轉(zhuǎn)義序列
![在這里插入圖片描述](https://upload-images.jianshu.io/upload_images/24494503-a2d6a09f1a437de2?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    - 反斜杠\被稱為轉(zhuǎn)義字符。它是一個特殊字符毕源。
- 4.3.3 字符型數(shù)據(jù)與數(shù)字型數(shù)據(jù)之間的轉(zhuǎn)換
    - char型數(shù)據(jù)可以轉(zhuǎn)換成任意一種數(shù)值類型浪漠,反之亦然。將整數(shù)轉(zhuǎn)換成char型數(shù)據(jù)時霎褐,只用到該數(shù)據(jù)的低十六位址愿,其余部分都被忽略。
    

        ```java
        package chapter04;
        
        public class MathMethod {
            public static void main(String[] args){
                char ch1 = (char)0XAB0041;
                System.out.println(ch1);
        
                char ch2 = (char)65.25;
                System.out.println(ch2);
        
                int i = (int)'A';
                System.out.println(i);
        
                byte b = 'a';
                int j = 'a';
                byte bb = (byte)'\uFFF4';
        
                int z = '2' + '3';
                System.out.println("z is " + z);
                int x = 2 + 'a';
                System.out.println("x is " + x);
                System.out.println(x + " is the Unicode for character " + (char)x);
                System.out.println("Chapter " + '2');
            }
        }
        
        ```
        結(jié)果顯示:
        
        ```java
        A
        A
        65
        z is 101
        x is 99
        99 is the Unicode for characterc
        Chapter 2
        
        Process finished with exit code 0
        
        ```
- 4.3.4 字符比較和測試
    - 兩個字符可以使用關(guān)系操作符進行比較冻璃,如同比較兩個數(shù)字一樣响谓。
    ![在這里插入圖片描述](https://upload-images.jianshu.io/upload_images/24494503-085fd5a3690b8e56?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
        ```java
        package chapter04;
        
        public class CharacterMethod {
            public static void main(String[] args){
                System.out.println("isDidit('a') is " + Character.isDigit('a'));
                System.out.println("isLetter('a') is " + Character.isLetter('a'));
                System.out.println("isLowerCase('a') is " + Character.isLowerCase('a'));
                System.out.println("isUpperCase('a') is " + Character.isUpperCase('a'));
                System.out.println("toLowerCase('T') is " + Character.toLowerCase('T'));
                System.out.println("toUpperCase('q') is " + Character.toUpperCase('q'));
            }
        }
        
        ```
  • 4.4 String類型
    字符串是一個字符序列。String類型不是基本類型省艳,而是引用類型娘纷。


    在這里插入圖片描述
    • 4.4.1 獲取字符串長度
      • 可以調(diào)用字符串的length()方法獲取他的長度。
      • 為方便起見跋炕,Java允許在不創(chuàng)建新變量的情況下赖晶,使用字符串字面值直接飲用字符串。這樣“Welcome to Java”.length()是正確的辐烂,它返回15.注意遏插,“”表示空字符串,并且.length()為0纠修。
    • 4.4.2 從字符串中獲取字符
      • 方法s.charAt(index)可用于提取字符串s中的某個特定字符胳嘲。
      • 在字符串s中越界訪問字符是一個常見的程序設(shè)計錯誤矫渔。為了避免此類錯誤颊亮,要確保使用的下標不會超過s.length()-1犀勒。
    • 4.4.3 連接字符串
      • 加號(+)也可以用于連接字符串和數(shù)組统舀。在這種情況下揩悄,先將數(shù)字轉(zhuǎn)換成字符串蝗岖,然后再進行連接碉输。i跟继,若要用加號實現(xiàn)連接的功能,至少要有一個操作數(shù)必須為字符串福荸。
    • 4.4.4 字符串的轉(zhuǎn)換
      • 方法trim()通過刪除字符串兩端的空白字符返回一個新字符串蕴坪。字符‘ ’、\t敬锐、\f、\r或者\n被稱為空白字符呆瞻。
    • 4.4.5 從控制臺讀取字符串
      • 將使用方法next()台夺、nextByte()、nextShort()痴脾、nextInt()颤介、nextLong()、nextFloat()和nextDouble()的輸入稱為基于標記的輸入赞赖,因為他們讀取采用空白字符分隔的單個元素滚朵,而不是讀取整行。nextLine()方法稱為基于行的輸入前域。
    • 4.4.6 從控制臺讀取字符
      • 調(diào)用nextLine()方法讀取一個字符串辕近,然后在字符串上調(diào)用charAt(0)來返回一個字符。
    • 4.4.7 字符串比較


      在這里插入圖片描述
      package chapter04;
      
      import java.util.Scanner;
      
      public class OrderTwoCities {
          public static void main(String[] args){
              Scanner input = new Scanner(System.in);
              System.out.print("Enter the first city: ");
              String city1 = input.nextLine();
              System.out.print("Enter the second city: ");
              String city2 = input.nextLine();
              if (city1.compareTo(city2) < 0)
                  System.out.println("The cities in alphabetical order are " + city1 + " " + city2);
              else
                  System.out.println("The cities in alphabetical order are " + city2 + " " + city1);
          }
      }
      
      
    • 4.4.8 獲得子字符串


      在這里插入圖片描述
      • 如果beginIndex為endIndex匿垄,substring(beginIndex,endIndex)返回一個長度為0的空字符串移宅。如果beginIndex>endIndex,將發(fā)生運行錯誤椿疗。
    • 4.4.9 獲取字符串中的字符或者子串


      在這里插入圖片描述
    • 4.4.10 字符串和數(shù)字間的轉(zhuǎn)換
    ```java
    package chapter04;
    
    public class StringToInteger {
        public static void main(String[] args){
            String intString = "123";
            int intValue = Integer.parseInt(intString);
            System.out.println(intValue);
            String doubleString = "3.1415926";
            double doubleValue = Double.parseDouble(doubleString);
            System.out.println(doubleValue);
            int number = 222;
            String s = number + "";
            System.out.println(s);
        }
    }
    
    ```
  • 4.5 示例學習
    • 4.5.1 猜測生日


      在這里插入圖片描述
      package chapter04;
      
      import java.util.Scanner;
      
      public class GuessBirthday {
          public static void main(String[] args){
              String set1 = "1 3 5 7\n" + "9 11 13 15\n" + "17 19 21 23\n" + "25 27 29 31";
              String set2 = "2 3 6 7\n" + "10 11 14 15\n" + "18 19 22 23\n" + "26 27 30 31";
              String set3 = "4 5 6 7\n" + "12 13 14 15\n" + "20 21 22 23\n" + "28 29 30 31";
              String set4 = "8 9 10 11\n" + "12 13 14 15\n" + "24 25 26 27\n" + "28 29 30 31";
              String set5 = "16 17 18 19\n" + "20 21 22 23\n" + "24 25 26 27\n" + "28 29 30 31";
      
              int day = 0;
      
              Scanner input = new Scanner(System.in);
      
              System.out.print("Is you birthday in Set1?\n");
              System.out.print(set1);
              System.out.print("\nEnter 0 for No and 1 for Yes: ");
              int answer = input.nextInt();
      
              if (answer == 1)
                  day += 1;
      
              System.out.print("\nIs you birthday in Set2?\n");
              System.out.print(set2);
              System.out.print("\nEnter 0 for No and 1 for Yes: ");
              answer = input.nextInt();
      
              if (answer == 1)
                  day += 2;
      
              System.out.print("\nIs you birthday in Set3?\n");
              System.out.print(set3);
              System.out.print("\nEnter 0 for No and 1 for Yes: ");
              answer = input.nextInt();
      
              if (answer == 1)
                  day += 4;
      
              System.out.print("\nIs you birthday in Set4?\n");
              System.out.print(set4);
              System.out.print("\nEnter 0 for No and 1 for Yes: ");
              answer = input.nextInt();
      
              if (answer == 1)
                  day += 8;
      
              System.out.print("\nIs you birthday in Set5?\n");
              System.out.print(set5);
              System.out.print("\nEnter 0 for No and 1 for Yes: ");
              answer = input.nextInt();
      
              if (answer == 1)
                  day += 16;
              System.out.println("\nYour birthday is " + day + "!");
          }
      }
      
      
      在這里插入圖片描述
    • 4.5.2 將十六進制數(shù)轉(zhuǎn)換為十進制

    ```java
    package chapter04;
    
    import java.util.Scanner;
    
    public class HexDigit2Dec {
        public static void main(String[] args){
            Scanner input = new Scanner(System.in);
            System.out.print("Enter a hex digit: ");
            String hexString = input.nextLine();
    
            if (hexString.length() != 1){
                System.out.println("You must enter exactly one character");
                System.exit(1);
            }
    
            char ch = Character.toUpperCase(hexString.charAt(0));
            if ('A' <= ch && ch <= 'F'){
                int value = ch - 'A' + 10;
                System.out.println("The decimal value for hex digit " + ch + " is " + value);
            }
            else if (Character.isDigit(ch)){
                System.out.println("The decimal value for hex digit " + ch + " is " + ch);
            }
            else{
                System.out.println(ch + " is an invalid input");
            }
        }
    }
    
    ```
- 4.5.3 使用字符串修改彩票程序
![在這里插入圖片描述](https://upload-images.jianshu.io/upload_images/24494503-fb6bdd02ae00432e?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    ```java
    package chapter04;
    
    import java.util.Scanner;
    
    public class LotteryUsingStrings {
        public static void main(String[] args){
            String lottery = "" + (int)(Math.random() * 10) + (int)(Math.random() * 10);
    
            Scanner input = new Scanner(System.in);
            System.out.print("Enter your lottery pick (two digits): ");
            String guess = input.nextLine();
    
            char lotteryDigit1 = lottery.charAt(0);
            char lotteryDigit2 = lottery.charAt(1);
    
            char guessDigit1 = guess.charAt(0);
            char guessDigit2 = guess.charAt(1);
    
            System.out.println("The lottery number is " + lottery);
    
            if (guess.equals(lottery))
                System.out.println("Exact match:you win $10,000");
            else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2)
                System.out.println("Match all digits:you win $3,000");
            else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit2 == lotteryDigit2 || guessDigit2 == lotteryDigit1)
                System.out.println("Match one digit:yuo win $1,000");
            else
                System.out.println("Sorry,no match");
        }
    }
    
    ```
  • 4.6 格式化控制臺輸出
    • 可以使用System.out.printf方法在控制臺上顯示格式化輸出漏峰。
    • printf中的f代表格式(format),暗示著方法將以某種格式打印届榄。調(diào)用這個方法的語法是:
    ```java
    System.out.printf(format,item1,item2,...,itemk);
    ```
[圖片上傳失敗...(image-edf74e-1601915130907)]
在這里插入圖片描述

- 如果項需要比指定寬度更多的空間浅乔,寬度自動增加。例如铝条,下面的代碼:

    ```java
    System.out.printf("%3d#%2s#%4.2f\n",1234,"Java",51.6653);
    ```
    結(jié)果顯示:
    
    ```java
    1234#Java#51.67
    ```
- 如果要顯示一個帶有逗號的數(shù)字靖苇,可以在數(shù)字限定符前面添加一個逗號。

    
    ```java
    System.out.printf("%,8d %,10.1f\n",12345678,12345678.263);
    ```
    結(jié)果顯示:
    
    ```java
    12,345,678 12,345,678.3
    ```
- 如果要在數(shù)字前面添加0而不是空格來湊齊位數(shù)攻晒,可以在一個數(shù)字限定符前面添加0顾复。

    
    ```java
    System.out.printf("%08d %08.1f\n",1234,5.63);
    ```
    結(jié)果顯示:
    
    ```java
    00001234 000005.6
    ```
- 默認情況下,輸出是右對齊的鲁捏⌒驹遥可以在格式限定符中放一個減號(-)萧芙,指定該項在指定域中的出書是左對齊的。


    ```java
    System.out.printf("%-8d%-8s%-8.1f\n",1234,"Java",5.63);
    ```
    結(jié)果顯示:
    
    ```java
    1234    Java    5.6     
    ```
- 條目與格式標識符必須在類型上嚴格匹配假丧。對應于格式標識符%f或%e的條目必須是浮點型值双揪,可以使用%.2f來指定一個小數(shù)點后兩位的浮點數(shù)值,而是用%0.2f是不正確 包帚。
package chapter04;

public class FormatDemo {
    public static void main(String[] args){
        System.out.printf("%-10s%-10s%-10s%-10s%-10s\n","Degrees","Radians","Sine","Cosine","Tangent");

        int degrees = 30;
        double radians = Math.toRadians(degrees);
        System.out.printf("%-10d%-10.4f%-10.4f%-10.4f%-10.4f\n",degrees,radians,Math.sin(radians),Math.cos(radians),Math.tan(radians));

        degrees = 60;
        radians = Math.toRadians(degrees);
        System.out.printf("%-10d%-10.4f%-10.4f%-10.4f%-10.4f\n",degrees,radians,Math.sin(radians),Math.cos(radians),Math.tan(radians));
    }
}

結(jié)果顯示:

Degrees   Radians   Sine      Cosine    Tangent   
30        0.5236    0.5000    0.8660    0.5774    
60        1.0472    0.8660    0.5000    1.7321    

Process finished with exit code 0

  • 編程小習題
    • 1渔期、
      在這里插入圖片描述
      package chapter04;
      
      import java.util.Scanner;
      
      public class Code_01 {
          public static void main(String[] args){
              Scanner input = new Scanner(System.in);
              System.out.print("Enter the length from the center to a vertex: ");
              double r = input.nextDouble();
              double s = 2 * r * Math.sin(Math.PI / 5);
              double area = 5 * s * s / (4 * Math.tan(Math.PI / 5));
              System.out.printf("The area of the pentagon is %.2f",area);
          }
      }
      
      
    • 2、
      在這里插入圖片描述
      package chapter04;
      
      public class Code_06 {
          public static void main(String[] args){
              double x1, y1, x2, y2, x3, y3;
              double angleA,angleB,angleC,alpha;
              double a,b,c;
      
              alpha = Math.random() * (2 * Math.PI);
              x1 = 40 * Math.cos(alpha);
              y1 = 40 * Math.sin(alpha);
      
              alpha = Math.random() * (2 * Math.PI);
              x2 = 40 * Math.cos(alpha);
              y2 = 40 * Math.sin(alpha);
      
              alpha = Math.random() * (2 * Math.PI);
              x3 = 40 * Math.cos(alpha);
              y3 = 40 * Math.sin(alpha);
      
              a = Math.sqrt(Math.pow(x1 - x2, 2)+Math.pow(y1 - y2, 2));
              b = Math.sqrt(Math.pow(x1 - x3, 2)+Math.pow(y1 - y3, 2));
              c = Math.sqrt(Math.pow(x3 - x2, 2)+Math.pow(y3 - y2, 2));
      
              angleA = Math.toDegrees(Math.acos((a * a - b * b - c * c) / (-2 * b * c)));
              angleB = Math.toDegrees(Math.acos((b * b - a * a - c * c) / (-2 * a * c)));
              angleC = Math.toDegrees(Math.acos((c * c - b * b - a * a) / (-2 * b * a)));
      
              System.out.printf("The first angle in degree is %.2f\n", angleA);
              System.out.printf("The second angle in degree is %.2f\n", angleB);
              System.out.printf("The third angle in degree is %.2f", angleC);
          }
      }
      
      
    • 3渴邦、[圖片上傳失敗...(image-75be4e-1601915130907)]
      package chapter04;
      
      import java.util.Scanner;
      
      public class Code_08 {
          public static void main(String[] args){
              Scanner input = new Scanner(System.in);
              System.out.print("Enter an ASCII code : ");
              int number = input.nextInt();
              System.out.println("The character for ASCII code " + number + " is " + (char)number);
          }
      }
      
      
    • 4疯趟、[圖片上傳失敗...(image-30ecef-1601915130907)]
      package chapter04;
      
      import java.util.Scanner;
      
      public class Code_09 {
          public static void main(String[] args){
              Scanner input = new Scanner(System.in);
              System.out.print("Enter a character: ");
              String str = input.nextLine();
              char character = str.charAt(0);
              System.out.println("The Unicode for the character " + character + " is " + (int)character);
          }
      }
      
      
    • 5、
      在這里插入圖片描述
      package chapter04;
      
      import java.util.Scanner;
      
      public class Code_15 {
          public static void main(String[] args){
              Scanner input = new Scanner(System.in);
              System.out.print("Enter a letter: ");
              String str = input.nextLine();
              char letter = str.toUpperCase().charAt(0);
              if (letter == 'A' || letter == 'B' || letter == 'C')
                  System.out.println("The corresponding number is 2");
              else if (letter == 'D' || letter == 'E' || letter == 'F')
                  System.out.println("The corresponding number is 3");
              else if (letter == 'G' || letter == 'H' || letter == 'I')
                  System.out.println("The corresponding number is 4");
              else if (letter == 'J' || letter == 'K' || letter == 'L')
                  System.out.println("The corresponding number is 5");
              else if (letter == 'M' || letter == 'N' || letter == 'O')
                  System.out.println("The corresponding number is 6");
              else if (letter == 'P' || letter == 'Q' || letter == 'R' || letter == 'S')
                  System.out.println("The corresponding number is 7");
              else if (letter == 'T' || letter == 'U' || letter == 'V')
                  System.out.println("The corresponding number is 8");
              else
                  System.out.println("The corresponding number is 9");
          }
      }
      
      
    • 6谋梭、[圖片上傳失敗...(image-f286eb-1601915130907)]
      package chapter04;
      
      public class Code_16 {
          public static void main(String [] args){
              System.out.println((char) ((int)(Math.random() * 26) + 65));
          }
      }
      
      
    • 7信峻、
      在這里插入圖片描述
      package chapter04;
      
      import java.util.*;
      
      public class Code_21 {
          public static void main(String[] args){
              String ssnString;
      
              System.out.print("Enter a SSN: ");
              Scanner input = new Scanner(System.in);
              ssnString = input.nextLine();
      
              if(ssnString.length() == 11)
              {
                  if(Character.isDigit(ssnString.charAt(0))
                          && Character.isDigit(ssnString.charAt(1))
                          && Character.isDigit(ssnString.charAt(2))
                          && ssnString.charAt(3) == '-'
                          && Character.isDigit(ssnString.charAt(4))
                          && Character.isDigit(ssnString.charAt(5))
                          && ssnString.charAt(6) == '-'
                          && Character.isDigit(ssnString.charAt(7))
                          && Character.isDigit(ssnString.charAt(8))
                          && Character.isDigit(ssnString.charAt(9))
                          && Character.isDigit(ssnString.charAt(10)))
      
                      System.out.println(ssnString + " is a valid social security number");
                  else
                      System.out.println(ssnString + " is an invalid social security number");
              }
              else
                  System.out.println(ssnString + " is an invalid social security number");
      
              input.close();
          }
      }
      
      
    • 8、
      在這里插入圖片描述
      package chapter04;
      
      import java.util.*;
      
      public class Code_22 {
          public static void main(String[] args) {
              String string1,string2;
      
              System.out.print("Enter string s1: ");
              Scanner input = new Scanner(System.in);
              string1 = input.nextLine();
      
              System.out.print("Enter string s2: ");
              string2 = input.nextLine();
      
              System.out.println(string1.contains(string2)
                      ?string2 + " is a substring of " + string1
                      :string2 + " is not a substring of " + string1);
              input.close();
          }
      }
      
      
    • 9瓮床、
      在這里插入圖片描述
      package chapter04;
      
      import java.util.*;
      
      public class Code_24 {
          public static void main(String[] args) {
              String city1,city2,city3;
      
              Scanner inputScanner = new Scanner(System.in);
      
              System.out.print("Enter the first city: ");
              city1 = inputScanner.nextLine();
              System.out.print("Enter the second city: ");
              city2 = inputScanner.nextLine();
              System.out.print("Enter the third city: ");
              city3 = inputScanner.nextLine();
      
              if(city1.compareTo(city2) > 0) // city1 > city2
              {
                  if(city2.compareTo(city3) > 0) // city2 > city3
                      System.out.println("The three cities in alphabetical order are " + city1 + " " + city2 + " " + city3);
                  else //city2 < city3
                  {
                      if(city1.compareTo(city3) > 0) // city1 > city3
                          System.out.println("The three cities in alphabetical order are " + city2 + " " + city3 + " " + city1);
                      else // city1 < city3
                          System.out.println("The three cities in alphabetical order are " + city2 + " " + city1 +  " " + city3);
                  }
              }
              else // city1 < city2
              {
                  if(city1.compareTo(city3) > 0) // city1 > city3
                      System.out.println("The three cities in alphabetical order are " + city3 + " " + city1 + " " + city2);
                  else //city1 < city3
                  {
                      if(city2.compareTo(city3) > 0) // city2 > city3
                          System.out.println("The three cities in alphabetical order are " + city1 + " " + city3 + " " + city2);
                      else // city2 < city3
                          System.out.println("The three cities in alphabetical order are " + city1 + " " + city2 + " " + city3);
                  }
              }
      
              inputScanner.close();
          }
      }
      
      
    • 10盹舞、[圖片上傳失敗...(image-a71146-1601915130907)]
      package chapter04;
      
      public class Code_25 {
          public static void main(String[] args) {
      
              char char1,char2,char3,char4,char5,char6,char7;
      
              char1 = (char)(65 + (int)(Math.random() * 26));
              char2 = (char)(65 + (int)(Math.random() * 26));
              char3 = (char)(65 + (int)(Math.random() * 26));
      
              char4 = (char)(48 + (int)(Math.random() * 10));
              char5 = (char)(48 + (int)(Math.random() * 10));
              char6 = (char)(48 + (int)(Math.random() * 10));
              char7 = (char)(48 + (int)(Math.random() * 10));
      
              System.out.println("The vehicle plate numbers is " + char1+char2+char3+char4+char5+char6+char7);
          }
      }
      
      
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市隘庄,隨后出現(xiàn)的幾起案子踢步,更是在濱河造成了極大的恐慌,老刑警劉巖丑掺,帶你破解...
    沈念sama閱讀 222,627評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件获印,死亡現(xiàn)場離奇詭異,居然都是意外死亡吼鱼,警方通過查閱死者的電腦和手機蓬豁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來菇肃,“玉大人地粪,你說我怎么就攤上這事∷霭” “怎么了蟆技?”我有些...
    開封第一講書人閱讀 169,346評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長斗忌。 經(jīng)常有香客問我质礼,道長,這世上最難降的妖魔是什么织阳? 我笑而不...
    開封第一講書人閱讀 60,097評論 1 300
  • 正文 為了忘掉前任眶蕉,我火速辦了婚禮,結(jié)果婚禮上唧躲,老公的妹妹穿的比我還像新娘造挽。我一直安慰自己碱璃,他們只是感情好,可當我...
    茶點故事閱讀 69,100評論 6 398
  • 文/花漫 我一把揭開白布饭入。 她就那樣靜靜地躺著嵌器,像睡著了一般。 火紅的嫁衣襯著肌膚如雪谐丢。 梳的紋絲不亂的頭發(fā)上爽航,一...
    開封第一講書人閱讀 52,696評論 1 312
  • 那天,我揣著相機與錄音乾忱,去河邊找鬼讥珍。 笑死,一個胖子當著我的面吹牛饭耳,可吹牛的內(nèi)容都是我干的串述。 我是一名探鬼主播,決...
    沈念sama閱讀 41,165評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼寞肖,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了衰腌?” 一聲冷哼從身側(cè)響起新蟆,我...
    開封第一講書人閱讀 40,108評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎右蕊,沒想到半個月后琼稻,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,646評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡饶囚,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,709評論 3 342
  • 正文 我和宋清朗相戀三年帕翻,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片萝风。...
    茶點故事閱讀 40,861評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡嘀掸,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出规惰,到底是詐尸還是另有隱情睬塌,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布歇万,位于F島的核電站揩晴,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏贪磺。R本人自食惡果不足惜硫兰,卻給世界環(huán)境...
    茶點故事閱讀 42,196評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望寒锚。 院中可真熱鬧劫映,春花似錦违孝、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至摹蘑,卻和暖如春筹燕,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背衅鹿。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評論 1 274
  • 我被黑心中介騙來泰國打工撒踪, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人大渤。 一個月前我還...
    沈念sama閱讀 49,287評論 3 379
  • 正文 我出身青樓制妄,卻偏偏與公主長得像,于是被迫代替她去往敵國和親泵三。 傳聞我的和親對象是個殘疾皇子耕捞,可洞房花燭夜當晚...
    茶點故事閱讀 45,860評論 2 361