java複習第1天---1.1---常用類案例

java複習第1天---1.1---常用類案例


目錄




內容

1、scanner

  1. 把用戶輸入2個整數相加,輸出結果。比如,用戶輸入23,65 ,輸出:23+65=88
  • 代碼1-1:

      import java.util.Scanner;
    
      public class ScannerTest1{
      	public static void main(String[] args){
      		Scanner sc = new Scanner(System.in);
      		System.out.println("請輸入2個整數");
      		int a = sc.nextInt();
      		int b = sc.nextInt();
      		System.out.println("a + b = " + (a + b));
      	}
      }
    

2、Random

  1. 猜數字小遊戲
    • 規則:系統隨機產生1-100之間的隨機數,用戶輸入輸入數字與隨機數做比較。如果比隨機大,提升大了;比隨機數小,提升小了;等於隨機數,提示“恭喜,猜對了",結束遊戲;但是用戶只有3次機會,如果3次之後都沒有猜對,則提升”大笨蛋,GAME OVER!",遊戲結束。

    • 代碼2-1:

        import java.util.Random;
        
        import java.util.Scanner;
      
      
        /**
         * 猜數字小遊戲
        */
        public class GuessNumber{
        	public static void main(String[] args){
        		// 0、歡迎界面
        		System.out.println("========猜數字小遊戲[1,100]之間隨機1個整數======");
      
        		// 1、生成[1,100]的一個隨機整數
        		Random r = new Random();
        		int guess = r.nextInt(100) + 1;
        		// 2、用戶有3次機會
        		int count = 3;
        		Scanner sr = new Scanner(System.in);
      
        		while(count >0){
        			// 3、用戶輸入數字
        			System.out.println("用戶第" + (4 - count) + "次輸入");
        			int innum = sr.nextInt();
        			// 4、根據用戶輸入做提示
        			if(innum > guess){
        				// 4.1、大於生成的隨機數,提升大了
        				System.out.println("輸入數字大了");
        			}else if(innum < guess){
        				// 4.2、小於生成的隨機數,提升小了
        				System.out.println("輸入數字小了");
        			}else{
        				// 4.3、等於生成的隨機數,恭喜
        				System.out.println("恭喜,猜對了");
        				break;
        			}
        			count--;
        		}
      
      
      
      
      
        		// 5、3次都猜錯,結束小遊戲
      
        		if(count == 0){
        			System.out.println("======大笨蛋,GAME OVER!====");
        			System.out.println("正確數字:" + guess);
        		}
        	}
        }
      

3、String

  1. 統計輸入字符串中數字、大寫字母,小寫字碼 和其他字符的個數
    • 代碼3-1:

        public class StringTest1{
        public static void main(String[] args){
        	String str = "dfaf3lfl2fDF3F22ff!?df|";
        	char[] letters = str.toCharArray();
      
        	int num = 0, lower = 0, upper = 0, others = 0;
        	for(int i = 0; i < letters.length; i++){
        		if(Character.isDigit(letters[i])){
        			num++;
        		}else if(Character.isLowerCase(letters[i])){
        			lower++;
        		}else if(Character.isUpperCase(letters[i])){
        			upper++;
        		}else {
        			others++;
        		}
        	}
      
        	System.out.println("str總長度:" + letters.length);
        	System.out.println("str中數字個數:" + num);
        	System.out.println("str中小寫字母個數:" + lower);
        	System.out.println("str中大寫字母個數:" + upper);
        	System.out.println("str中剩餘字符個數:" + others);
      
        }
      

      }

4、static

  • 修飾屬性:爲類屬性,爲類的公共屬性

  • 修飾方法:爲類方法

  • 修飾代碼塊:爲靜態代碼塊

  • 注意:

    1. 對象與類都可以調用靜態成員,建議用類調用
    2. 靜態方法只能使用靜態方法或者屬性,不能調用非靜態成員
    3. 靜態代碼塊優先於構造方法執行
  • 示例代碼4-1:

      public class Test{
      	int a;
      	static int b;
      	
      	static {
      		System.out.println("靜態代碼塊執行");
      	}
      	public Test(){
      		System.out.println("構造方法執行");
      	}
      	
      	public void show() {
      		System.out.println(a + " " + b);
      	}
      	public static void display() {
      		// System.out.println(a);//錯誤,不能調用非靜態成員
      		System.out.println(b);
      	}
      	public static void main(String[] args) {
      		Test t1 = new Test();
      		t1.a = 100;
      		Test.b = 200;
      		t1.show();
      		t1.display();
      	}
      }
      
      結果:
      靜態代碼塊執行
      構造方法執行
      100 200
      100
    

5、Arrays

  1. 字符串倒敘輸出
    • 示例:Hello World ----> dlroW olleH
    • 代碼:

6、Math

  1. 計算在-10.8到5.9之間,絕對值大於6或者小於2.1的整數有多少個?
    • 簡單分析:關鍵點 整數個數

      • 閉區間:小的向上取整,大的向下取整
      • 開區間:小於向上取整,大於向下取整
    • 代碼6-1:

        /*
        1. 計算在-10.8到5.9之間,絕對值大於6或者小於2.1的整數有多少個?
        */
      
      
        public class MathTest1{
        	public static void main(String[] args){
        		int begin =(int)Math.ceil(-10.8);
        		int end =(int)Math.floor(5.9);
        		int x = 6;
        		int y = (int)Math.ceil(2.1);
      
        		int count = 0;
        		for(int i = begin; i <= end; i++){
        			if(i > 6 || i < y){
        				count++;
        			}
        		}
      
        		System.out.println(count);
        	}
        }
      

後記

本項目爲參考某馬視頻開發,相關視頻及配套資料可自行度娘或者聯繫本人。上面爲自己編寫的開發文檔,持續更新。歡迎交流,本人QQ:806797785

前端項目源代碼地址:https://gitee.com/gaogzhen/vue-leyou
    後端JAVA源代碼地址:https://gitee.com/gaogzhen/JAVA

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章