3.1给出ax^2+bx+c的a.b.c系数,求根;
- import java.util.Scanner;
- public class C03t1 {
- public static void main(String[] args){
- Scanner input=new Scanner(System.in);
- System.out.println("请输入一元二次方程a,b,c系数的值\n请输入a:");
-
- int a=input.nextInt();
- System.out.println("请输入b:");
- int b=input.nextInt();
- System.out.println("请输入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("有一个根为:"+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("有两个根为:"+y1+"和"+y2);
- }
- }
-
- }
3.8输入三个数,按大小显示
- import javax.swing.JOptionPane;
- public class C03t8 {
- public static void main(String[] args){
- int num1,num2,num3,temp;
- String s1=JOptionPane.showInputDialog("请输入第1个整数");
- num1=Integer.parseInt(s1);
- s1=JOptionPane.showInputDialog("请输入第2个整数");
- num2=Integer.parseInt(s1);
- s1=JOptionPane.showInputDialog("请输入第3个整数");
- 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猜硬币正反面扩展,1表示正面,0表示反面;可多次猜测,最终显示猜测的准确率。
- import javax.swing.JOptionPane;
- public class C03t14 {
- public static void main(String[] args){
- int count=0; //统计次数
- int correctCount=0; //统计正确次数
- String s1="正面请选择是,反面请选择否";
- String sz="本次投掷为正面, ";
- String sf="本次投掷为反面, ";
- String g1="恭喜你,猜对了";
- String g2="很遗憾,猜错了";
- 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,"你猜测准确率为"+x+"%.");
-
- }
-
- }
|