close

1.林偉川版
class EX10_12 {
 public static void main(String [] args) {
  String[] black = {"將", "士", "士", "象", "象", "車", "車", "馬", "馬", "包", "包", "卒", "卒", "卒", "卒", "卒"};
  String[] red = {"帥", "仕", "仕", "相", "相", "人車", "人車", "傌", "傌", "炮", "炮", "兵", "兵", "兵", "兵", "兵"};
  String[] cards = new String[32];

  for(int i=0; i   for(int i=0; i     for(int i=0; i    int newLoc = (int) (Math.random()*cards.length);

   String tmp = cards[i];
   cards[i] = cards[newLoc];
   cards[newLoc] = tmp;
  }

  for(int i=0; i    System.out.print(cards[i]+" ");
   if((i+1) % 4 == 0) System.out.println();
  }
 }
}

2.阿輝版
class works_01 {
 public static void main(String [] args) {
  String[] black = {"黑將", "黑士1", "黑士2","黑象1" ,
  "黑象2","黑車1","黑車2","黑馬1","黑馬2","黑包1","黑包2",
  "黑卒1","黑卒2","黑卒3","黑卒4","黑卒5"};
  String[] red = {"紅帥", "紅仕1", "紅仕2","紅相1" ,
  "紅相2","紅(人車)1","紅(人車)2","紅傌1","紅傌2","紅炮1","紅炮2",
  "紅兵1","黑卒2","黑卒3","黑卒4","黑卒5"};  
   
  //定義阿里不達的牌
  
  String[] cards = new String[32];

  for(int i=0; i   for(int i=0; i     
   
  
  for(int i=0; i    int newLoc = (int) (Math.random()*cards.length);

   String tmp = cards[i];
   cards[i] = cards[newLoc];
   cards[newLoc] = tmp;
  }

  for(int i=0; i    System.out.print(cards[i]+" ");
   if((i+1) % 4 == 0) System.out.println();
  }
  
  
  
  //for(int i=0; i   
  
 /* String[] red = {"桃", "心", "磚", "梅"};
  String[] size = {"A", "2", "3", "4", "5", "6", "7",
   "8", "9", "O", "J", "Q", "K"};
  String[] cards = new String[52];
    */
 /*
  for(int i=0; i    for(int j=0; j     cards[i*size.length+j] = pattern[i]+size[j];
   
  
  for(int i=0; i    int newLoc = (int) (Math.random()*cards.length);

   String tmp = cards[i];
   cards[i] = cards[newLoc];
   cards[newLoc] = tmp;
  }

  for(int i=0; i    System.out.print(cards[i]+" ");
   if((i+1) % 13 == 0) System.out.println();
  }
 }
 
 */
}
}

3.
class EX10_9{
 public static void main(String [] args) {
  /*if(args.length    System.out.println("請傳入一整數參數!");
   return;
  }

  long n = Long.parseLong(args[0]); //轉換成long型別
  
  */
  long n=1000000000000L;
  int g = 1;  //指示用的變數

  if(Byte.MIN_VALUE = n) g=1;
  else if(Short.MIN_VALUE = n) g=2;
  else if(Integer.MIN_VALUE = n) g=3;
  else if(Long.MIN_VALUE = n) g=4;
  
  switch(g) {
   case 1:
    System.out.println("可以為 byte 型別");
   case 2:
    System.out.println("可以為 short 型別");
   case 3:
    System.out.println("可以為 int 型別");
   case 4:
    System.out.println("可以為 long 型別");
  }
 }
}

4.
class EX10_1 {
 public static void main(String [] args) {
  /*if(args.length    System.out.println("請傳入一個程式參數!");
   return;
  }
*/
  //int len = args[0].length(); //取得第一個字串參數的長度
        int len =10;
        String aa="this is a test";
        System.out.println(aa+"string長度:"+aa.length());
     
       
        for (int i=aa.length()-1; i >=0; i--) System.out.print( aa.charAt(i) );
        System.out.println("");
        aa="test"; System.out.println("aa:"+aa.length());       
       
        byte a[]={65,66,67,68};
        aa=new String(a);
        System.out.println("aa:"+"byte長度:"+aa.length());
        
        byte a2[]={65,66,67,68};
        aa=new String(a2);
        System.out.println("aa:"+"char長度:"+aa.length());
        
        aa=new String(a2,2,2);
        System.out.println("aa:"+"char長度:"+aa.length());
       
        int cc[]=new int[8];
        System.out.println("cc長度:"+cc.length);
       
        String a1=new String("this is a test");
        System.out.println("a1:"+a1+" "+a1.length());
        a1=new String("test");
        System.out.println("a1:"+a1+" "+a1.length());
        for (int i=a1.length()-1; i >=0; i--) System.out.print( a1.charAt(i) );
        System.out.println("");
       
       
  //for(int i=len-1; i>=0; i--) System.out.print( args[0].charAt(i) );
 }
}

5.
class EX10_2 {
 public static void main(String [] args) {
  String str1 = "東西";
  String str2 = str1 + "當舖當";//東西 + 當舖當

  System.out.println( str2.concat(str1) );;//東西當舖當 +東西
  System.out.println(str1);//東西
  System.out.println(str2);//東西當舖當
  str1=str2.concat(str1);//東西當舖當東西
  System.out.println(str1);//東西當舖當東西
 }
}

6.
class EX10_3 {
 public static void main(String [] args) {
  String str = "東當舖 西當舖 東西當舖當東西";
  String sub = "當舖";
  char ch1 = '東';
  char ch2 = '北';

  System.out.println(str);

  int loc = str.indexOf(ch1); //0

  while(loc > -1) {
   showArrow(loc, ch1+""); //0, "東"  // 8, "東"// 13, "東"
   int i= loc+1;
   loc = str.indexOf(ch1, i); //找下一個 '東' 的位置
  }

  showArrow(str.indexOf(sub), sub);//1, "當舖"
  showArrow(str.lastIndexOf(sub), "最後一個"+sub);//當舖
  showArrow(str.indexOf(ch2), ch2+"");//找不到北
 }

 static void showArrow(int loc, String s) {
  if(loc == -1) {
   System.out.println("找不到:" + s);
   return;
  }

  for(int i=0; i

  System.out.println("↑"+s+"的位置為:"+loc);
 }
}
7.
class EX10_4{
 public static void main(String [] args) {
  String str = "感覺Happy就是幸福!";

  System.out.println("str = " + str);
  System.out.println("str.substring(9) = " + str.substring(9)); // 幸福!
  System.out.println("str.substring(2, 7) = " + str.substring(2, 7)); // happy

   int i;
  for ( i=str.length()-1;i>=0;i--) System.out.print(str.substring(i,i+1));
  System.out.println(); 
 }
}

8.
  //QA2
  String s ="ABCDEF";
  //System.out.println(s.substring(cd));//1.error
  System.out.println(s.substring(3));//2
  System.out.println(s.substring(2,4));//3
  System.out.println(s.substring(3,4));//4
9.

class EX10_6 {
 public static void main(String [] args) {
  String s1 = "AbCd多雷米";
  String s2 = "小溪啊小溪~田裡的灌溉都靠妳!";
  
  //無個性
  String n =s1.valueOf(32768);//n=32768
   System.out.println(n);
   System.out.println( "n="+ n.substring(2,5));


//
  System.out.println( s1.toLowerCase() );
  System.out.println( s1.toUpperCase() );
  System.out.println( s2.replace('溪', '河') );
  System.out.println( s2 );
  
  
 }
}

10.
class EX10_5 {
 public static void main(String [] args) {
  String[] s = new String[4];
  s[0] = "java";
  s[1] = new String("java");
  s[2] = "java";
  s[3] = "JaVa";
   
        char [] f={'j','a','v','a'};
       
  for(int i=1; i    System.out.print("s[0]==s["+i+"]→"+(s[0] ==s[i]));
   System.out.println("\ts[0].equals(s["+i+"])→"+ s[0].equals(s[i]) );
  }
  System.out.println("s[1].equalsIgnoreCase(s[3])→" +s[1].equalsIgnoreCase(s[3]) );
  
  StringBuffer c=new StringBuffer(s[1]);
  StringBuffer d=new StringBuffer(s[0]);
  System.out.println("\ts[0].equals(s[1])"+c.equals(d) );
  String g=new String(f);
  System.out.println("\ts[0].equals(s[0])"+s[0].equals(g) );
 }
}

11.
class EX10_5 {
 public static void main(String [] args) {
  String[] s = new String[4];
  s[0] = "java";
  s[1] = new String("java");
  s[2] = "java";
  s[3] = "JaVa";
   
        char [] f={'j','a','v','a'};
       
  for(int i=1; i    System.out.print("s[0]==s["+i+"]→"+(s[0] ==s[i]));
   System.out.println("\ts[0].equals(s["+i+"])→"+ s[0].equals(s[i]) );
  }
  System.out.println("s[1].equalsIgnoreCase(s[3])→" +s[1].equalsIgnoreCase(s[3]) );
  
  StringBuffer c=new StringBuffer(s[0]);
  StringBuffer d=new StringBuffer(s[1]);
  StringBuffer e=new StringBuffer(s[2]);
  StringBuffer g=new StringBuffer(s[3]);  
  System.out.println("c長度:"+c.length()+" "+c.capacity());
  System.out.println("d長度:"+d.length()+" "+d.capacity()); 
  System.out.println("e長度:"+e.length()+" "+e.capacity());   
  System.out.println("g長度:"+g.length()+" "+g.capacity());
  System.out.println("\tc.equals(d)"+c.equals(d) );
  System.out.println("\tc.equals(e)"+c.equals(e) );
  System.out.println("\tc.equals(g)"+c.equals(g) );
  
  System.out.println("\tc.equals(d)"+c.equals(d)+"   "+(c==d) );
  System.out.println("\tc.equals(e)"+c.equals(e)+"   "+(c==e) );
  System.out.println("\tc.equals(g)"+c.equals(g)+"   "+(c==g) );
  
  
   String h=new String(f);
  System.out.println("\tc.equals(h)"+s[0].equals(h) ); 
  /*System.out.println("\ts[0].equals(s[1])"+c.equals(d) );
  */
 }
}


12.

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 凝輝 的頭像
    凝輝

    凝輝部落格

    凝輝 發表在 痞客邦 留言(0) 人氣()