JAVA基礎篇-----運算符,鍵盤錄入,結構語句,方法及數組

在這裏插入圖片描述概念部分不想贅述,會寫一些比較容易出錯的地方和經典的例子
ex 1:case穿透現象(無所謂好壞,可以根據實際的需求來使用)

import java.util.Scanner;

public class demo  {
  public static void main(String[] args) {
	  Scanner sc=new Scanner(System.in);
	  System.out.println("請輸入一個數字");
	  int s=sc.nextInt();
	  switch(s) {
	    case 3:
	    case 4:
	    case 5:{
		  System.out.println("春天");
		  break;
	  }
	    case 6:
	    case 7:
	    case 8:{
	    	System.out.println("夏天");
	    	break;
	    }
	    case 9:
	    case 10:
	    case 11:{
	    	System.out.println("秋天");
	    	break;
	    }
	    case 12:
	    case 1:
	    case 2:{
	    	System.out.println("冬天");
	    	break;
	    }
	    default:
	    	System.out.println("輸入的數據有誤");
	  }
  }
}
請輸入一個數字
3
春天

public class demo2 {
	public static void main(String[] args) {
        //1*1=1
        //1*2=2 2*2=4
        //1*3=3 2*3=6 3*3=9
        //輸出99乘法表
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j+"*"+i+"="+(j*i)+"\t");
            }
            System.out.println();
        }
	}
}
1*1=1	
1*2=2	2*2=4	
1*3=3	2*3=6	3*3=9	
1*4=4	2*4=8	3*4=12	4*4=16	
1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	

import java.util.Scanner;

public class demo2 {
	public static void main(String[] args) {
	        //根據元素查對應的索引
	        String[] weeks = {"星期一", "星期二", "星期三", "星期四", "星期5", "星期六", "星期天"};
	        //抽取方法
	        Scanner sc = new Scanner(System.in);
	        System.out.println("請輸入一個星期");
	        String str = sc.nextLine();
	        int index = getIndex(weeks, str);
	        System.out.println("你要找的索引是" + index);
	    }
	    private static int getIndex(String[] arr, String str) {
	        //根據元素查找索引
	        for (int i = 0; i < arr.length; i++) {
	            if (str.equals(arr[i])) {
	                return i;
	            }
	        }
	        return -1;//用 -1來代表沒找到
	    }
}
請輸入一個星期
星期三
你要找的索引是2
import java.util.Scanner;

public class demo2 {
	public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6};
        for (int i = 0; i < arr.length/2; i++) {
            int t;
            t=arr[i];
            arr[i]=arr[arr.length-1-i];
            arr[arr.length - 1-i]=t;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+",");
        }
	}
}
6,5,4,3,2,1,
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章