日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Java語言程序設(shè)計(jì)基礎(chǔ)篇第三章編程練習(xí)題

 Blanche1111 2013-12-15

3.1給出ax^2+bx+c的a.b.c系數(shù),求根;

  1. import java.util.Scanner;  
  2. public class C03t1 {  
  3.     public static void main(String[] args){  
  4.         Scanner input=new Scanner(System.in);  
  5.         System.out.println("請(qǐng)輸入一元二次方程a,b,c系數(shù)的值\n請(qǐng)輸入a:");  
  6.           
  7.         int a=input.nextInt();  
  8.         System.out.println("請(qǐng)輸入b:");  
  9.         int b=input.nextInt();  
  10.         System.out.println("請(qǐng)輸入c:");  
  11.         int c=input.nextInt();  
  12.         int x=b*b-4*a*c;  
  13.         double y1,y2;  
  14.         if(x<0)  
  15.             System.out.println("方程無解");  
  16.         else if(x==0){  
  17.             y1=(double)(-b)/(2*a);  
  18.             System.out.println("有一個(gè)根為:"+y1);  
  19.         }  
  20.         else{  
  21.             y1=((double)(-b)+Math.pow(x,0.5))/2*a;  
  22.             y2=((double)(-b)-Math.pow(x,0.5))/2*a;  
  23.             System.out.println("有兩個(gè)根為:"+y1+"和"+y2);  
  24.         }  
  25.     }  
  26.   
  27. }  


3.8輸入三個(gè)數(shù),按大小顯示

  1. import javax.swing.JOptionPane;  
  2. public class C03t8 {  
  3.     public static void main(String[] args){  
  4.         int num1,num2,num3,temp;  
  5.         String s1=JOptionPane.showInputDialog("請(qǐng)輸入第1個(gè)整數(shù)");  
  6.         num1=Integer.parseInt(s1);  
  7.         s1=JOptionPane.showInputDialog("請(qǐng)輸入第2個(gè)整數(shù)");  
  8.         num2=Integer.parseInt(s1);  
  9.         s1=JOptionPane.showInputDialog("請(qǐng)輸入第3個(gè)整數(shù)");  
  10.         num3=Integer.parseInt(s1);  
  11.         if(num1<num2){  
  12.             temp=num2;  
  13.             num2=num1;  
  14.             num1=temp;  
  15.         }  
  16.         if(num1<num3){  
  17.             temp=num3;  
  18.             num3=num1;  
  19.             num1=temp;  
  20.         }  
  21.         if(num3>num2){  
  22.             temp=num3;  
  23.             num3=num2;  
  24.             num2=temp;  
  25.         }  
  26.         JOptionPane.showMessageDialog(null,"MAX Number is "+num1+  
  27.                 "; the second number is "+num2+";least number is "+num3);         
  28.     }  
  29.   
  30. }  


3.14猜硬幣正反面擴(kuò)展,1表示正面,0表示反面;可多次猜測(cè),最終顯示猜測(cè)的準(zhǔn)確率。

  1. import javax.swing.JOptionPane;  
  2. public class C03t14 {  
  3.     public static void main(String[] args){  
  4.         int count=0;                //統(tǒng)計(jì)次數(shù)  
  5.         int correctCount=0;       //統(tǒng)計(jì)正確次數(shù)  
  6.         String s1="正面請(qǐng)選擇是,反面請(qǐng)選擇否";  
  7.         String sz="本次投擲為正面,   ";  
  8.         String sf="本次投擲為反面,   ";  
  9.         String g1="恭喜你,猜對(duì)了";  
  10.         String g2="很遺憾,猜錯(cuò)了";  
  11.         String sx,gx;  
  12.         while(JOptionPane.showConfirmDialog(null,"接著猜么?")==0){  
  13.         int coin=(int)(Math.random()*10)%2;  
  14.         int answer=JOptionPane.showConfirmDialog(null,s1);  
  15.         if(coin==1)  
  16.             sx=sz;  
  17.         else  
  18.             sx=sf;  
  19.         if((coin==1&&answer==0)||(coin==0&&answer==1)){  
  20.             gx=g1;  
  21.             correctCount++;  
  22.         }  
  23.         else   
  24.             gx=g2;  
  25.         JOptionPane.showMessageDialog(null,sx+gx);  
  26.         count++;  
  27.         }  
  28.         double x=(double)correctCount/count*100;  
  29.         JOptionPane.showMessageDialog(null,"你猜測(cè)準(zhǔn)確率為"+x+"%.");  
  30.           
  31.     }  
  32.   
  33. }  


 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多