3.1給出ax^2+bx+c的a.b.c系數(shù),求根;
- import java.util.Scanner;
- public class C03t1 {
- public static void main(String[] args){
- Scanner input=new Scanner(System.in);
- System.out.println("請(qǐng)輸入一元二次方程a,b,c系數(shù)的值\n請(qǐng)輸入a:");
-
- int a=input.nextInt();
- System.out.println("請(qǐng)輸入b:");
- int b=input.nextInt();
- System.out.println("請(qǐng)輸入c:");
- int c=input.nextInt();
- int x=b*b-4*a*c;
- double y1,y2;
- if(x<0)
- System.out.println("方程無解");
- else if(x==0){
- y1=(double)(-b)/(2*a);
- System.out.println("有一個(gè)根為:"+y1);
- }
- else{
- y1=((double)(-b)+Math.pow(x,0.5))/2*a;
- y2=((double)(-b)-Math.pow(x,0.5))/2*a;
- System.out.println("有兩個(gè)根為:"+y1+"和"+y2);
- }
- }
-
- }
3.8輸入三個(gè)數(shù),按大小顯示
- import javax.swing.JOptionPane;
- public class C03t8 {
- public static void main(String[] args){
- int num1,num2,num3,temp;
- String s1=JOptionPane.showInputDialog("請(qǐng)輸入第1個(gè)整數(shù)");
- num1=Integer.parseInt(s1);
- s1=JOptionPane.showInputDialog("請(qǐng)輸入第2個(gè)整數(shù)");
- num2=Integer.parseInt(s1);
- s1=JOptionPane.showInputDialog("請(qǐng)輸入第3個(gè)整數(shù)");
- num3=Integer.parseInt(s1);
- if(num1<num2){
- temp=num2;
- num2=num1;
- num1=temp;
- }
- if(num1<num3){
- temp=num3;
- num3=num1;
- num1=temp;
- }
- if(num3>num2){
- temp=num3;
- num3=num2;
- num2=temp;
- }
- JOptionPane.showMessageDialog(null,"MAX Number is "+num1+
- "; the second number is "+num2+";least number is "+num3);
- }
-
- }
3.14猜硬幣正反面擴(kuò)展,1表示正面,0表示反面;可多次猜測(cè),最終顯示猜測(cè)的準(zhǔn)確率。
- import javax.swing.JOptionPane;
- public class C03t14 {
- public static void main(String[] args){
- int count=0; //統(tǒng)計(jì)次數(shù)
- int correctCount=0; //統(tǒng)計(jì)正確次數(shù)
- String s1="正面請(qǐng)選擇是,反面請(qǐng)選擇否";
- String sz="本次投擲為正面, ";
- String sf="本次投擲為反面, ";
- String g1="恭喜你,猜對(duì)了";
- String g2="很遺憾,猜錯(cuò)了";
- String sx,gx;
- while(JOptionPane.showConfirmDialog(null,"接著猜么?")==0){
- int coin=(int)(Math.random()*10)%2;
- int answer=JOptionPane.showConfirmDialog(null,s1);
- if(coin==1)
- sx=sz;
- else
- sx=sf;
- if((coin==1&&answer==0)||(coin==0&&answer==1)){
- gx=g1;
- correctCount++;
- }
- else
- gx=g2;
- JOptionPane.showMessageDialog(null,sx+gx);
- count++;
- }
- double x=(double)correctCount/count*100;
- JOptionPane.showMessageDialog(null,"你猜測(cè)準(zhǔn)確率為"+x+"%.");
-
- }
-
- }
|