顯示具有 java 習題 標籤的文章。 顯示所有文章
顯示具有 java 習題 標籤的文章。 顯示所有文章

2019年1月11日 星期五

java 習題

1.下列選向哪些可以編譯成功?(選擇2個) B D
   A.char xy='yz';  //不能放兩個字
   B.String s2="s2";
   C.char a='\';     // 斜線不行, 除非變成 "//" 或是 /"
   D.String s="\"";
   E.int i=1+0.1;     //int 不能小數點

2.請選擇合法的敘述句。(選擇2個) B C //只有 "$" 跟"_" 底線可以使用
  A. String #s1="Hi!";
  B. int $money=1000;
  C. double _tax=0.06;
  D. double ~pi=3.14;

3.程式碼如下:

01 int i=1;
02 long I=1;
03 float f=1.0f;
04 double d=1.0;
05 sum=i+I+f+d;

請問sum應該定義成什麼資料類型? F //double 最大
A. byte
B. short
C. int
D. long
E. float
F. double

4.下列選向哪些可以編譯成功?(選擇3個)   A,B,D
  A. int i=(int)(1+1.1f+1.1);          //掛號起來轉int 會過
  B. double d=(float)(1+1.1f+1.1);   
  C. long I=1+1.1f+1.1;
  D. float f=(long)(1+1.1f+1.1);   
  E. int i=(int)1.1f+1.1;                //沒有全部跨號轉換

5.選出合法的識別字(選擇4個 )   A B C E
  A. _$i
  B. $_i
  C. javac
  D. 2i        //數字不能放前面
  E. i2



1. 7+6-5*4/3%(2+1)的結果為何? 答案是 A   

    A. 13
    B. 14
    C. 15
    D. 16
    E. 17


2.程式碼如下,請問最後結果為何? 答案: C // "+" 可以連結兩個字

    01. int i=100;
    02. String s="10";
    03. s+=i;
    04. System.out.println(s);

  A. 110
  B. 10010
  C. 10100
  D. 11000
  E. 編譯失敗

3.程式碼如下,請問最後結果為何? 答案 C //result=1 ==2 && ++i>=2; 已是錯誤沒有執行 
                                   // or一個 true 出現就執行  AND  出現 FALSE 就不做了


     01. boolean result;
     02. int i=1;
     03. result=1 = =2 && ++i>=2;
     04. System.out.println("result="+result+",i="+i);

   A. result=true , i=1
   B. result=true , i=2
   C. result=false , i=1
   D. result=false , i=2
   E. 編譯失敗


4. 程式碼如下,請問最後結果為何? 答案 :B 
    
      01. int x=1, y=1;
      02. boolean b=++x>++y;  //兩個都先加過 再比對  
      03. System.out.println(b);


   A. true
   B. false
   C. 編譯失敗
※如果為  boolean b=++x>y++; 答案會變成 Ture 因為會先 ++X  然後  比較  在 y 執行++


5.程式碼如下,請問最後結果如何? 答案:B   \\! 會將結果相反
     
      01. int x=1 , y=1;
      02. boolean b=!(x>y)^!(x<y);
      03. System.out.println(b);

    A. true
    B. false
    C. 編譯失敗


1.程式碼如下,請問最後結果如何?  答案: B  //第一個沒有誇號 所以 y= 2 會打印 所以等於B
 01. class Test{
 02.     public static void main(String args[]){
 03.        int x=2;
 04.     int y=1;
 05.        if((x==1)&&(y = = 2))
 06.            System.out.println("x=1,");
 07.            System.out.println("y=2, ");
 08.        if((x+y)==3)
 09.            System.out.println("x+y=3");
 10.        }
 11.  }
  
  A. x=1 , y=2 , x+y=3
  B. y=2 , x+y=3
  C. x=1 , y=2
  D. x+y=3
  E. 編譯失敗

2.程式碼如下,請問最後結果為何?  答案 等於 B
 01. int x=0;
 02. int y=0;
 03. do{
 04. y++;
 05. ++x;
 06. }while(x<5);
 07. System.out.println(x+","+y);

 A. 5,6
 B. 5,5
 C. 6,5
 D. 6,6
 E. 編譯失敗


3.程式碼如下,請問最後結果為何? 答案: F //Z:要在for之上
 01. class Test{
 02. public static void main(String args[]){
 03. String str;
 04. z:
 05. str="";
 06. for(int x=3;x<8;x++){
 07.    if(x==4) break;
 08.    if(x==6) break z;
 09.    str+=x;
 10.    }
 11.    System.out.println(str);
 12.   }
 13. }

 A. 3
 B. 34
 C. 345
 D. 3456
 E. 34567
 F. 編譯失敗


4.程式碼如下,請問最後果為何? 答案: D
 01. class Test{
 02. public static void main(String args[]){
 03.     int i=2000;
        04.     int j=1999;
 05.     int k=1000;
        06.     if((i>j)^((k*2)==i))
 07.        System.out.print(1);
 08.     if((j+1)!=i^((k*2)==j))
 09.        System.out.print(2);
 10.    }
 11. }

 A. 1
 B. 2
 C. 12
 D. 沒有任何輸出
 E. 執行失敗
 F. 編譯失敗


5.程式碼如下,請問最後結果為何? 答案 D
 01. class Test{
 02. public static void main(String args[]){
 03.     String str="";
 04.     z:
 05.     for(int x=0;x<3;x++){
 06.         for(int y=0;y<2;y++)
 07.             if(x==1) break;
 08.             if(x==2) break z;
 09.             str=str+x+y;
 10.          }
 11.      }
 12.      System.out.println(str);
 13.     }
 14. }

 A. 00
 B. 0001
 C. 000110
 D. 00011011
 E. 編譯失敗



 

java - 習題 計算業務獎金

需求算出 獎經 並且減去相關的費用算出剩餘金額

class ex2
{
public static void main(String args[])
{
java.util.Scanner sc=new java.util.Scanner(System.in);
int x1=22000;//底薪
int x2=2000;//車馬費
int x3;//業績
int x4;//獎金
int x5;//勞保
int x6;//健保
int x7;//薪資

System.out.println("請輸入業績");
x3=sc.nextInt();

if(x3>=1500000)
{
x4=(int)(x3*0.08);
x5=2500;
x6=2500;

}
else if(x3>=800000 && x3<1500000)
{
x4=(int)(x3*0.05);
x5=2000;
x6=2000;

}
else if(x3>=300000 && x3<800000)
{
x4=(int)(x3*0.03);
x5=1500;
x6=1500;

}
else if(x3>=0 && x3<300000)
{
x4=x3*0;
x5=1000;
x6=1000;

}
else
{
System.out.println("業績需大於0");
x4=0;
x5=0;
x6=0;

}

x7=x1+x2+x4-x5-x6;


System.out.println("\n底薪:"+x1+"元"+
"\n獎金:"+x4+"元"+
"\n勞保:"+x5+"元"+
"\n健保:"+x6+"元"+
"\n薪資:"+x7+"元");



}



}
執行結果:



java - 習題 購物車 跟 找零 系統

需求:   購買物品  並且顯示  可以找零多少

物品價格 :  1.led  單價 4999元
                    2.RAM  單價 1280元
                    3.Mouse  單價 699元
特價:         超過五萬  或是  是會員  都打 95折


題目內容:

import java.util.Scanner;
class ex2
{
public static  void main(String args[])
{
java.util.Scanner sc = new  java.util.Scanner(System.in);

System.out.println("你好請先選購商品.超過5萬打95折,是會員在打95折");
System.out.println("1.led  單價 4999元\n2.RAM  單價 1280元\n3.Mouse  單價 699元\n請選購:\n");
int a,b,c,dd,ww;
String   gg;
double hj,cf,ok;

a = 4999;
b = 1280;
c = 699;

Scanner in = new Scanner(System.in);
String k;
char j;



System.out.println("要購買幾個 led  單價 4999元:");
a= sc.nextInt();
System.out.println("要購買幾個 RAM  單價 1280元:");
b= sc.nextInt();
System.out.println("要購買幾個 Mouse  單價 699元:");
c= sc.nextInt();

dd = (a*4999) + (b*1280) + (c*699);
System.out.println("消費金額為"+dd);

if(dd >50000)
{
System.out.println("您花費超過5萬,享有打95折優惠");
hj = dd * 0.95;  //超過五萬打折
System.out.println("價格調整為:"+hj);
}
System.out.println("請問是否為會員,y/n");
//k = in.next();   
//j = k.charAt(0);

while(true)
{
k = in.next();   
j = k.charAt(0);
if ( j ==121)
{
System.out.println("您是會員價,將享有價格優惠");
cf= dd * 0.95; //會員價格
System.out.println("價格調整為:"+cf);
ok = cf;
break;
}
else if ( j ==110)
{
System.out.println("很抱歉不是會員,加入會員享有95折");
ok = dd;
break;
}
else
{
System.out.println("輸入無效,請輸入y/n");
}
}
System.out.println("結帳,您的消費金額為"+ok+"元");
System.out.println("請輸入您的付款金額");
int rr;
double uy;


while(true)
{
rr= sc.nextInt();
uy = rr -ok;
if(uy > 0)
{
System.out.println("您的付款金額為"+rr+"扣除消費總額為"+(int)ok+"\n剩餘"+(int)uy);
break;
}
else
{
System.out.println("輸入金額不足,請再次輸入");
}
}


double kcc,wcc,bcc,scc,gcc;

kcc = uy / 1000;
wcc = (uy % 1000) /500 ;
bcc = ((uy % 1000) %500) /100 ;
scc =(((uy % 1000) %500)%100) / 10 ;
gcc = ((((uy % 1000) %500)%100)%10) / 1 ;;

System.out.println("為您轉換零錢"+(int)kcc+"張千元,"+(int)wcc+"張五百元,"+(int)bcc+"張百元,"+(int)scc+"個十元硬幣跟"+(int)gcc+"個一元硬幣");



/*
System.out.println("========================找零========================");
double kcc,wcc,bcc,scc,gcc;
double ck, cw, cb, cs, cg;


if (uy  >=  1000 )
{
kcc = (int)uy  / 1000 ;
ck  = uy-(kcc*1000) ;

wcc = (int)ck  / 500 ;
cw  = ck-(wcc*500) ;

bcc = (int)cw  / 100 ;
cb  = cw-(bcc*100) ;

scc = (int)cb  / 10 ;
cs  = cb-(scc*10) ;

gcc = cs / 1;
System.out.println("為您轉換零錢"+(int)kcc+"張千元,"+(int)wcc+"張五百元,"+(int)bcc+"張百元,"+(int)scc+"個十元硬幣跟"+(int)gcc+"個一元硬幣");
}

else if (uy  >=  500 )
{
wcc = (int)uy  / 500 ;
cw  = uy-(wcc*500) ;

bcc = (int)cw  / 100 ;
cb  = cw-(bcc*100) ;

scc = (int)cb  / 10 ;
cs  = cb-(scc*10) ;

gcc = cs / 1;
System.out.println("為您轉換零錢"+(int)wcc+"張五百元,"+(int)bcc+"張百元,"+(int)scc+"個十元硬幣跟"+(int)gcc+"個一元硬幣");

}
else if (uy  >=  100 )
{
bcc = (int)uy  / 100 ;
cb  = uy-(bcc*100) ;

scc = (int)cb  / 10 ;
cs  = cb-(scc*10) ;

gcc = cs / 1;
System.out.println("為您轉換零錢"+(int)bcc+"張百元,"+(int)scc+"個十元硬幣跟"+(int)gcc+"個一元硬幣");

}

else if (uy  >=  10 )
{
scc = (int)uy  / 10 ;
cs  = uy-(scc*10) ;

gcc = cs / 1;
System.out.println((int)scc+"個十元硬幣跟"+(int)gcc+"個一元硬幣");

}

else
{
gcc = uy / 1;
System.out.println("為您轉換零錢"+(int)gcc+"個一元硬幣");
}

*/




/*
else
{
System.out.println("99999888885665454654");
}
*/




/*
System.out.println("===="+uy);
kcc = uy  % 1000;
System.out.println("===="+kcc);
wcc = kcc % 500;
bcc = wcc % 100;
scc = bcc % 10;
gcc = scc % 1;
*/
//System.out.println("為您轉換零錢"+kcc+"張千元,"+wcc+"張五百元,"+bcc+"張百元,"+scc+"個十元硬幣跟"+gcc+"個一元硬幣");

}
}


執行結果: