(3.2节+(3.8-3.16小节)+综合题)
3.2节
*3.1(代数:解一元二次方程)
import java.util.Scanner;
public class T1 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter a, b, c: ");
double a=input.nextDouble();
double b=input.nextDouble();
double c=input.nextDouble();
input.close();
double derta= Math.pow(b,2)-4*a*c ;//根的判别式
double r1= ( -b + Math.pow( Math.pow(b,2) - 4*a*c , 0.5 ) ) / 2*a ;
double r2=( -b - Math.pow( Math.pow(b,2) - 4*a*c , 0.5 ) ) / 2*a ;
if( derta>0 )
System.out.print("The equation has two roots:" + (float)r1 + " and " + (float)r2 );
else if ( derta==0 )
System.out.print("The equation has one root " + (float)r1);
else
System.out.print("The equation has no real roots");
}
}
3.2(游戏:三个数的加法)
import java.util.Scanner;
public class T2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int random1=(int) (Math.random()*10);
int random2=(int)(System.currentTimeMillis()%10);
int random3=(int)(System.currentTimeMillis()/7%10);
System.out.print(" 请输入这三个整数的和: ");
int number=input.nextInt();
input.close();
int sum=random1 + random2 + random3;
System.out.println(" 这三个随机数是 :"+ random1 + ", " + random2 + ", " + random3);
if(sum==number)
System.out.println("you are right!");
else
System.out.println("you are wrong!");
System.out.println("答案是:"+ random1 + "+" + random2 + "+" + random3 + "=" + sum);
}
}
*3.3(代数:求解2x2线性方程)
import java.util.Scanner;
public class T3 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter a, b, c, d, e, f: ");
double a=input.nextDouble();
double b=input.nextDouble();
double c=input.nextDouble();
double d=input.nextDouble();
double e=input.nextDouble();
double f=input.nextDouble();
input.close();
double x=(e*d-b*f)/(a*d-b*c);
double y=(a*f-e*c)/(a*d-b*c);
if( (a*d-b*c)==0 )
System.out.println(" The equation has no solution ");
else
System.out.println(" x is " + x + " and y is " + y );
}
}
**3.4(随机月份)
public class T4 {
public static void main(String[] args) {
int random=(int) (Math.random()*12+1);
if (random==1)
System.out.println("random is " + random + " : January ");
else if (random==2)
System.out.println("random is " + random + " : February ");
else if (random==3)
System.out.println("random is " + random + " : March ");
else if (random==4)
System.out.println("random is " + random + " : April ");
else if (random==5)
System.out.println("random is " + random + " : May ");
else if (random==6)
System.out.println("random is " + random + " : June ");
else if (random==7)
System.out.println("random is " + random + " : July ");
else if (random==8)
System.out.println("random is " + random + " : August ");
else if (random==9)
System.out.println("random is " + random + " : September ");
else if (random==10)
System.out.println("random is " + random + " : October ");
else if (random==11)
System.out.println("random is " + random + " : November ");
else if (random==12)
System.out.println("random is " + random + " : December ");
}
}
*3.5(找到将来的日期)
import java.util.Scanner;
public class T5 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter today's day (0-6):");
int number=input.nextInt();
System.out.print("Enter the number of days elapsed since today:");
int days=input.nextInt();
input.close();
String day = null;
switch(number) {
case 0: day="Sunday";
break;
case 1: day="Monday";
break;
case 2: day="Tuesday";
break;
case 3: day="Wednesday";
break;
case 4: day="Thursday";
break;
case 5: day="Friday";
break;
case 6: day="Saturday";
break;
}
int afterDay=( number + (days%7) ) % 7 ;
String futureDay = null;
switch(afterDay) {
case 0: futureDay="Sunday";
break;
case 1: futureDay="Monday";
break;
case 2: futureDay="Tuesday";
break;
case 3: futureDay="Wednesday";
break;
case 4: futureDay="Thursday";
break;
case 5: futureDay="Friday";
break;
case 6: futureDay="Saturday";
break;
}
System.out.print("Today is " + day + " and the future day is " + futureDay );
}
}
*3.6(医疗应用程序:BMI)
import java.util.Scanner;
public class T6 {
public static void main(String[] args) {
//1英尺=0.3047999995367米
//1英寸=0.0254米
Scanner input=new Scanner(System.in);
System.out.print("Enter weight in pounds: ");
double weight=input.nextDouble();
System.out.print("Enter feet: ");
double feet=input.nextDouble();
System.out.print("Enter inches: ");
double inches=input.nextDouble();
input.close();
double weightInKilograms= weight*0.45359237 ;
double heightInMeters= inches*0.0254 + feet*0.3047999995367 ;
double bmi=weightInKilograms/(heightInMeters*heightInMeters);
System.out.println("BMI is " + bmi);
if(bmi<18.5)
System.out.println("Underweight");
else if(bmi<25)
System.out.println("Normal");
else if(bmi<30)
System.out.println("Overweight");
else
System.out.println("Obese");
}
}
3.7(财务应用程序:整钱兑零)
略,暂时不会,看见钱头就大o(╥﹏╥)o,不想去想,。。。。。。(用IF选择语句去做)
*3.8(对三个整数排序)