JAVA语言程序设计(基础篇)第十版——第三章 选择 编程练习题(参考答案)

(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(对三个整数排序)

import java.util.Scanner;

public class T8 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("请输入三个整数:");
		int n1=input.nextInt();
		int n2=input.nextInt();
		int n3=input.nextInt();
		input.close();
		
		int temp;
		if(n1>n2) {
			temp=n1;
			n1=n2;
			n2=temp;
		}//先交换1和2
		
		if(n2>n3) {
			temp=n2;
			n2=n3;
			n3=temp;
		}//接着交换2和3
		
		if(n1>n2) {
			temp=n1;
			n1=n2;
			n2=temp;
		}//接着交换1和3
		
		
		
		System.out.print("从小到大排序为:" + n1 + " " + n2 + " "+ n3);
		
	}

}

**3.9(商业:检查ISBN-10)

import java.util.Scanner;

public class T9 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter the first 9 digits of an ISBN as integer: ");   
		int d=input.nextInt();//输入9个数字
		
		int d1=d/100000000%10;
		int d2=d/10000000%10;
		int d3=d/1000000%10;
		int d4=d/100000%10;
		int d5=d/10000%10;
		int d6=d/1000%10;
		int d7=d/100%10;
		int d8=d/10%10;
		int d9=d%10;
		input.close();
		
		int d10= (d1*1+d2*2+d3*3+d4*4+d5*5+d6*6+d7*7+d8*8+d9*9) % 11 ;
		
		String dd="X";
		
		if(d10==10)
			System.out.print("The ISBN-10 number is " + d1+d2+d3+d4+d5+d6+d7+d8+d9+ dd );
		else 
			System.out.print("The ISBN-10 number is " + d1+d2+d3+d4+d5+d6+d7+d8+d9+ d10 );
		
	}

}

3.10(游戏:加法测验)

import java.util.Scanner;

public class T10 {

	public static void main(String[] args) {
		int number1=(int)(Math.random()*100);
		int number2=(int)(Math.random()*100);
		
	/*	if(number1<number2) {
			int temp=number1;
			number1=number2;
			number2=temp;
		}  */
		
		System.out.print("What is " + number1 + " + " + number2 + " ? ");  
		Scanner input=new Scanner(System.in);
		int answer=input.nextInt();
		input.close();
		
		if(number1 + number2 == answer)
			System.out.println("You are right! ");
		else {
			System.out.println("Your answer is wrong.");
			System.out.println( number1 + " + " + number2 
					+" should be " + (number1+number2) );
			
		}
		
	}

}

(3.8-3.16小节)

*3.11(给出一个月的总天数)

import java.util.Scanner;

public class T11 {

	public static void main(String[] args) {
	  /*  一月大        二月平
		    三月大        四月小
		    五月大        六月小
		   七月大        八月大
		   九月小        十月大
		  十一月小     十二月大  */
		
		System.out.print( " 请输入月份和年份: " );
		Scanner input=new Scanner(System.in);
		
		int month=input.nextInt();
		int year=input.nextInt();
		input.close();
		
		String monthName=null;
		String day=null;
		
		//判定闰年,对2月份的影响
		boolean isLeapYear=
			( year%4==0 && year%10!=0 ) || ( year%400==0 );//判断是否是闰年  
		
		switch(month) {
		case 1: monthName="January";
				day="31";
				break;
		case 2: monthName="February";//特殊
		
				if(isLeapYear) 
						day="29";
				else
						day="28";
				
				break;
		case 3: monthName="March";
				day="31";
				break;
		case 4: monthName="April";
				day="30";
				break;
		case 5: monthName="May";
				day="31";
				break;
		case 6: monthName="June";
				day="30";
				break;
		case 7: monthName="July";
				day="31";
				break;
		case 8: monthName="August";
				day="31";
				break;
		case 9: monthName="September";
				day="30";
				break;
		case 10: monthName="October";
				day="31";
				break;
		case 11: monthName="November";
				day="30";
				break;
		case 12: monthName="December";
				day="31";
				break;
		}
		
		
		System.out.println( monthName +" "+ year + " has " + day + " days " );
		
		

	}

}

3.12(回文数字)

import java.util.Scanner;

public class T12 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a three-digit integer: ");
		int d=input.nextInt();
		input.close();
		
		int d1=d%10;
		int d2=d/10%10;
		int d3=d/100%10;
		
		if (d1==d3)
			System.out.print( d + " is a palindrome");
		else
			System.out.print( d + " is not a palindrome");
		
	}

}

*3.13(财务应用程序:计算税款)

3.14(游戏:猜硬币的正反面)

import java.util.Scanner;

public class T14 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.print(" 请输入一个猜测值:");
		Scanner input=new Scanner(System.in);
		int answer=input.nextInt();
		input.close();
		
		int s=(int)(Math.random()*2);
		
		String what=null;
		
		switch(s) {
		
		case 0: what=" 0 代表正面,1 代表反面. 随机数产生 0.";
				break;
		case 1: what=" 0 代表正面,1 代表反面. 随机数产生 1.";
				break;
				
		}
		
		if(s==answer)
			System.out.println( what + " You are right !");
		else
			System.out.println( what + " You are wrong.");
		

	}

}

**3.15(游戏:彩票)

import java.util.Scanner;

public class T15 {

	public static void main(String[] args) {
		int lottery =(int)(Math.random()*1000);
		
		Scanner input=new Scanner(System.in);
		System.out.print("Enter your lottery pick (three digit): ");
		int guess=input.nextInt();
		input.close();
		
		int l1=lottery%10;
		int l2=lottery/10%10;
		int l3=lottery/100%10;
		
		int g1=guess%10;
		int g2=guess/10%10;
		int g3=guess/100%10;
		
		System.out.println("The lottery number is: " + lottery );
		
		if(guess==lottery)
			System.out.println("Exact match: you win $10,000 ");
		
		else if(   (g1==l1 && g2==l3 && g3==l2 ) 
				|| (g1==l2 && g2==l1 && g3==l3 ) 
				|| (g1==l2 && g2==l3 && g3==l1 )
				|| (g1==l3 && g2==l1 && g3==l2 )
				|| (g1==l3 && g2==l2 && g3==l1 ) )
			
			System.out.println("Match all digits: you win $3,000");
		else if(l1==g1 || l1==g2 ||l1==g3
			|| l2==g1 || l2==g2 || l2==g3 
			|| l3==g1 || l3==g2 || l3==g3 )	
			
			System.out.println("Match noe digit: you win $1,000");
		else
			System.out.println("Sorry, no match ");
		
	}

}

3.16(随机点)

/* 
 * 我解释一下这一部分:(Math.random()*101 -  Math.random()*1)
 * 
 * 我是这样想的:
 * 用double型的 Math.random()*101,来产生 [0,101) 区间里的整数和小数,
 * 然后用 double型的 Math.random()*1,来产生 [0,1) 区间里的整数和小数,
 * 用 [0,101) - [0,1) =[0,100]; //解释一下:[0,101)=[0,100]∪[100,101),
 * 而[100,101)这一部分相当于[0,1)这一部分 ,他们的区间长度是一样的
 * 
 * */        //当然用(int)类型产生随机数也可以,我这里想用double型的试试

public class T16 {

	public static void main(String[] args) {
		//X的上限是50,下限是-50;
		//  Y的上限是100,下限是-100.



		double r1=((Math.random()*101 -  Math.random()*1)  - 50 );

		double r2=((Math.random()*201 -  Math.random()*1) - 100 );
		

		
		System.out.println("产生的随机点是: (  " + r1 + " , " + r2 +" ) ");
	}

}

*3.17(游戏:剪刀、石头、布)

import java.util.Scanner;

public class T17 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		int random=(int) Math.random()*3;
		
		System.out.print(" Scissor(0), Rock(1), Paper(2): ");		
		int number=input.nextInt();
		input.close();
		
		String word=null;
		switch(random) {
		
		case 0:word=" scissor. ";
			break;
		case 1:word=" rock. ";
			break;
		case 2:word=" paper. ";
			break;
		}
		
		
		String word2=null;
		if(random==0 && number==0)
			word2=" You are scissor too. It is a draw ";
		else if(random==0 && number==1)
			word2=" You are rock. You won ";
		else if(random==0 && number==2)
			word2=" You are paper. You lose ";
		else if(random==1 && number==0)
			word2=" You are scissor. You lose ";
		else if(random==1 && number==1)
			word2=" You are rock too. It is a draw ";
		else if(random==1 && number==2)
			word2=" You are paper. You won ";
		else if(random==2 && number==0)
			word2=" You are scissor. You win ";
		else if(random==2 && number==1)
			word2=" You are rock. You lose ";
		if(random==2 && number==2)
			word2=" You are paper too. It is a draw ";
		
		
		System.out.print("The computer is " + word + word2 );
		
	}

}

*3.18(运输成本)

import java.util.Scanner;

public class T18 {

	public static void main(String[] args) {
		
		Scanner input=new Scanner(System.in);
		
		System.out.print( " 请输入您的包裹重量: " );
		double weight=input.nextDouble();
        input.close();
		
		if(weight>0 && weight<=1)
			System.out.print( " 运输成本为 : " + 3.5*weight + " 美元 " );
		if(weight>1 && weight<=3)
			System.out.print( " 运输成本为 : " + 5.5*weight + " 美元 " );
		if(weight>3 && weight<=10)
			System.out.print( " 运输成本为 : " + 8.5*weight + " 美元 " );
		if(weight>10 && weight<=20)
			System.out.print( " 运输成本为 : " + 10.5*weight + " 美元 " );
		else if(weight>20)
			System.out.print( " the package cannot be shipped ");
		
	}

}

**3.19(计算三角形的周长)

import java.util.Scanner;

public class T19 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print( " 请输入三角形的三条边 : " );
		int a=input.nextInt();
		int b=input.nextInt();
		int c=input.nextInt();
		input.close();
		
		if(a+b>c && a+c>b && b+c>a)
			System.out.println("这个三角形的周长为: " + (a+b+c));
		else 
			System.out.println(" 这些输入值不合法.");
		
	}

}

*20(科学:风寒温度)

import java.util.Scanner;

public class T20 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.println("Enter the temperature in Fahrenheit between -58℃F and 41℃F:");
		double temperature=input.nextDouble();
		
		System.out.print("Enter the wind speed (>=2) in miles per hour:");
		double windSpeed=input.nextDouble();
		input.close();
		
		
		
		double windChill= 35.74 + 0.6215*temperature - 35.75*Math.pow(windSpeed, 0.16) 
		                  + 0.4275*temperature*Math.pow(windSpeed, 0.16);  
		
		if(temperature>=-58 && temperature<=41 && windSpeed>=2)
			System.out.print("The wind chill index is " + windChill);
		else if(temperature<-58 || temperature>41)
			System.out.print(" 输入的温度不合法 ");
		else if(windSpeed<2)
			System.out.print(" 输入的风速不合法 ");
		
		
		
	}

}

综合题

**3.21(科学:某天是星期几)

import java.util.Scanner;

public class T21 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter year: (e.g.,2012) : ");
		int year=input.nextInt();
		
		System.out.print("Enter month: 1-12: ");
		int month=input.nextInt();
		
		System.out.print("Enter the day of the month: 1-31:  ");
		int day=input.nextInt();
		input.close();
		
		int q ;   //某月的第几天
		q=day;
		
		
		if(month==1) {
			month=13;
			year=year-1;
		}
		else if(month==2) {
			month=14;
			year=year-1;
		}
		
		
		int m ;  /*是月份(注意:一月和二月分别记为上一年的13和14月,
				其他月份照常,3为三月,4为四月,...,12为十二月 */
		m=month;
		
		
		
		 int j = Math.abs( year/100 ); // 世纪数 |year/100|
		
		int k=year%100;//该世纪的第几年
		
		int h= ( q + (26*(m+1)/10) + k + (k/4) + (j/4) + (5*j) ) % 7 ;
		
		String days = null;
		switch(h) {
		
		
		case 2:	days="Monday";
				break;
		case 3:	days="Tuesday";
				break;
		case 4:	days="Wednesday";
				break;	
		case 5:	days="Thursday";
				break;	
		case 6:	days="Friday";
				break;	
		case 0:	days="Saturday";
				break;	
		case 1:	days="Sunday";
				break;		
		}
		
		
		System.out.print("Day of the week is " + days);
		
		
	}

}

**3.22(几何:点是否在圆内?)

import java.util.Scanner;

public class T22 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a point with two coordinates :");
		float x=input.nextFloat();
		float y=input.nextFloat();
		input.close();
		
		float d= (float) Math.pow( (Math.pow(x, 2) + Math.pow(y, 2)) , 0.5 );
		
		String word=null;
		if(d<=10)
			word=" is";
		else
			word=" is not";
		
		
		
		System.out.print("Point ("+ x +", "+ y +") " + word  + " in the circle "  );
	}

}

**3.23(几何:点是否在矩形内?)

import java.util.Scanner;

public class T23 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a point with two coordinates :");
		float x=input.nextFloat();
		float y=input.nextFloat();
		input.close();
		
		String word=null;
		if( (x-0)<=10/2 && (y-0)<=5/2 )
			word=" is";
		else
			word=" is not";
		
		
		System.out.print("Point ("+ x +", "+ y +") " + word  + " in the rectangle "  );
		
	}

}

**3.24(游戏:挑一张牌)

public class T24 {

	public static void main(String[] args) {
		
		int random=(int)(Math.random()*13);
		
		int random1=(int)(Math.random()*4) ;
		
		String word=null;
		
		switch(random) {
		case 0: word=" Ace ";
				break;
		case 1: word=" 2 ";
				break;
		case 2: word=" 3 ";
				break;
		case 3: word=" 4 ";
				break;
		case 4: word=" 5 ";
				break;
		case 5: word=" 6 ";
				break;
		case 6: word=" 7 ";
				break;
		case 7: word=" 8 ";
				break;
		case 8: word=" 9 ";
				break;
		case 9: word=" 10 ";
				break;
		case 10: word=" Jack ";
				break;
		case 11: word=" Queen ";
				break;
		case 12: word=" King ";
				break;
		}
			
		
		String word1=null;
		
		switch(random1) {
		case 0: word1=" Clubs ";
				break;
		case 1: word1=" Diamonds ";
				break;
		case 2: word1=" Hearts ";
				break;
		case 3: word1=" Spades ";
				break;
		}
		
		System.out.print("The card you picked is " + word + " of " + word1);
		
	}

}

*3.25(几何:交点)

import java.util.Scanner;

public class T25 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter x1, y1, x2, y2, x3, y3, x4, y4: ");
		float x1=input.nextFloat();
		float y1=input.nextFloat();
		float x2=input.nextFloat();
		float y2=input.nextFloat();
		float x3=input.nextFloat();
		float y3=input.nextFloat();
		float x4=input.nextFloat();
		float y4=input.nextFloat();
		input.close();
		
		float x=(  ((y1-y2)*x1-(x1-x2)*y1)*(-(x3-x4)) - (-(x1-x2))*((y3-y4)*x3-(x3-x4)*y3)  ) 
				/ ( (y1-y2)*(-(x3-x4)) - (-(x1-x2))*(y3-y4) );   
		
		float y=( (y1-y2)*((y3-y4)*x3-(x3-x4)*y3) - ((y1-y2)*x1-(x1-x2)*y1)*(y3-y4)   )
				/ ( (y1-y2)*(-(x3-x4)) - (-(x1-x2))*(y3-y4) ); 
				
		float t=( (y1-y2)*(-(x3-x4)) - (-(x1-x2))*(y3-y4) ) ;
		
		if(t==0)
			System.out.print(" The two lines are parallel ");
		else
			System.out.print("The intersecting point is at (" + x + ", " + y + ")");
	}

}

3.26(使用操作符&&、||  和 ^)

import java.util.Scanner;

public class T26 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter an integer: ");
		int integer=input.nextInt();
		input.close();
		
		boolean answer=(integer%5==0 && integer%6==0);
		
		boolean answer1=(integer%5==0 || integer%6==0);
		
		boolean answer2=(integer%5==0 ^ integer%6==0);
		
		System.out.println(" Is " + integer + " divisible by 5 and 6 ?  " + answer );  
		System.out.println(" Is " + integer + " divisible by 5 or 6 ?  " + answer1 );
		System.out.println(" Is " + integer + " divisible by 5 or 6, but not both ?  " + answer2 );
		
		

	}

}

**3.27(几何:点是否在三角形内?)

我是看某大神的博客得到的启发:(里面还有很多不同的证明方法)

https://blog.csdn.net/dageda1991/article/details/77875637

在本书 第四章 的第103页,P103 有求角度的详细说明。

 

提示:内角和等于360°

1.这种方法最好理解,即对于△ABC内的某一点P,连接PA、PB、PC得到三条线段,当且仅当三条线段组成的三个夹角和为360°的时候,该点才位于三角形内部。

2.类似的还有,P与ABC三个顶点组成的三个三角形,面积之和等于△ABC的面积,同样可以证明点P位于三角形内部。

 

import java.util.Scanner;

public class T27 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter a point's x- and y-coordinates:  ");
		float x=input.nextFloat();
		float y=input.nextFloat();
		input.close();
		
		int x1=0, y1=0;    //点 O(x1,y1)
		int x2=200, y2=0;  //点 B(x2,y2)
		int x3=0, y3=100;  //点 A(x3,y3)
						  // 设点 P 为(x,y).
		//先计算第一个 角∠APO=one ,必须先知道边AO,OP,AP
		double LAO=100;
		double LOP=Math.sqrt((x-x1)*(x-x1) + (y-y1)*(y-y1));
		double LAP=Math.sqrt((x3-x)*(x3-x) + (y3-y)*(y3-y));
		
		double one=Math.toDegrees(  Math.acos((LAO*LAO - LOP*LOP - LAP*LAP) / (-2*LOP*LAP) )  ) ;  
		
		
		//计算第二个 角∠APB=two  ,必须先知道它的三条边AB,PB,AP
		double LAB=Math.sqrt( (x2-x3)*(x2-x3) + (y2-y3)*(y2-y3) );
		//double LAP=Math.sqrt((x3-x)*(x3-x) + (y3-y)*(y3-y));
		double LPB=Math.sqrt((x2-x)*(x2-x) + (y2-y)*(y2-y));
		
		double two=Math.toDegrees(  Math.acos((LAB*LAB - LAP*LAP - LPB*LPB) / (-2*LAP*LPB) )  ) ;  
		
		
		//计算第三个  角∠OPB=three  ,必须知道它的三条边OB,PB,OP
		double LOB=200;
		//double LPB=Math.sqrt((x2-x)*(x2-x) + (y2-y)*(y2-y));
		//double LOP=Math.sqrt((x-x1)*(x-x1) + (y-y1)*(y-y1));
		
		double three=Math.toDegrees(  Math.acos((LOB*LOB - LOP*LOP - LPB*LPB) / (-2*LOP*LPB) )  ) ;  
		
		
	//如果三个角相加为 360° ,则这个点在三角形内部
		String word=null;
		if(one+two+three==360)
			word=" is ";
		else
			word=" is not ";
	
		
		System.out.print("The point " + word + " in the triangle ");
		
		
	}

}

**3.28(几何:两个矩形)

import java.util.Scanner;

public class T28 {

	public static void main(String[] args) {
		Scanner input =new Scanner(System.in);
		System.out.print("Enter r1's center x-, y-coordinates, width, and height: "); 
		float x1=input.nextFloat() ;
		float y1=input.nextFloat() ;
		float w1=input.nextFloat() ;
		float h1=input.nextFloat() ;
		
		System.out.print("Enter r2's center x-, y-coordinates, width, and height: ");   
		float x2=input.nextFloat() ;
		float y2=input.nextFloat() ;
		float w2=input.nextFloat() ;
		float h2=input.nextFloat() ;
		input.close();
		
		if( (x1-w1/2<=x2-w2/2) && (x2+w2/2<=x1+w1/2)
				&&(y2+h2/2<=y1+h1/2) && (y2-h2/2>=y1-h1/2))
			
			System.out.print(" r2 is inside r1 ");
		
		else if (  ((y2+h2/2>y1-h1/2) && (x2-w2/2<x1+w1/2))
				|| ((y2-h2/2<y1+h1/2) && (x2+w2/2>x1-w1/2) )  )
			
			System.out.print(" r2 overlaps r1 ");
		
		else 
			System.out.print(" r2 dose not overlap r1 ");
			
				

	}

}

**3.29(几何:两个圆)

import java.util.Scanner;

public class T29 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print( "Enter circle1's center x-, y-coordinates, and radius: ");
		float x1=input.nextFloat();
		float y1=input.nextFloat();
		float r1=input.nextFloat();
		
		System.out.print( "Enter circle2's center x-, y-coordinates, and radius: ");
		float x2=input.nextFloat();
		float y2=input.nextFloat();
		float r2=input.nextFloat();
		input.close();
		
		float d=(float) Math.sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
		
		if( d <= Math.abs(r1-r2) )
			System.out.print(" cricle2 is inside circle1 ");
		else if( d <= (r1+r2) )
			System.out.print(" cricle2 overlaps circle1 ");
		else
			System.out.print(" cricle2 does not overlap circle1 ");
		
		
	}

}

*3.30(当前时间)

略(不会)

一看见数时间数钱的我就烦┭┮﹏┭┮

*3.31(金融:货币兑换)

import java.util.Scanner;

public class T31 {

	public static void main(String[] args) {
		
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the exchange rate from dollars to RMB: ");  
		float exchangeRate=input.nextFloat();
		
		System.out.print("Enter 0 to convert dollars to RMB and 1 vice versa: ");
		int number=input.nextInt();
		
		if (number==0) {
			System.out.print("Enter the dollar amount:");
			float dollars=input.nextFloat();
			System.out.print(" $"+dollars+" is " +(exchangeRate*dollars)+" yuan");
		}
		else if(number==1) {
			System.out.print("Enter the RMB amount:");
			float emb=input.nextFloat();
			System.out.print( emb + " yuan is $" + (emb/exchangeRate));
		}
			
		else
			System.out.print(" Incorrect input ");
		
		input.close();
	
		
	}

}

*3.32(几何:点的位置)

import java.util.Scanner;

public class T32 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter three points for p0, p1, p2:");
		float x0=input.nextFloat();
		float y0=input.nextFloat();
		float x1=input.nextFloat();
		float y1=input.nextFloat();
		float x2=input.nextFloat();
		float y2=input.nextFloat();
		input.close();
		
		float number= (x1-x0)*(y2-y0) - (x2-x0)*(y1-y0) ;
		
		if(number>0)
			System.out.print(" ("+x2+", "+y2+") is on the left side of "
					+ "the line from ("+x0+", "+y0+") to ("+x1+", "+y1+") ");
		else if(number==0)
			System.out.print(" ("+x2+", "+y2+") is on the  the line "
					+ "from ("+x0+", "+y0+") to ("+x1+", "+y1+") ");
		else if(number<0)
			System.out.print(" ("+x2+", "+y2+") is on the right side of "
					+ "the line from ("+x0+", "+y0+") to ("+x1+", "+y1+") ");
			
	}

}

*3.33(金融:比较成本)

import java.util.Scanner;

public class T33 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter weight and price for package 1:");
		float weight1=input.nextFloat();
		float price1=input.nextFloat();
		
		System.out.print("Enter weight and price for package 2:");
		float weight2=input.nextFloat();
		float price2=input.nextFloat();
		input.close();
		
		if((weight1/price1) > (weight2/price2))
			System.out.print("Package 1 has a better price");
		else if((weight1/price1) < (weight2/price2))
			System.out.print("Package 2 has a better price");
		else if((weight1/price1) == (weight2/price2))
			System.out.print("Two packages have the same price");
		
		
	}
}

*3.34(几何:线段上的点)

这一题一定要理解清楚,这题和 *3.32的题目有什么不同。

*3.32的题目是测试一个点是否在一个无限长的直线上,而这题是测试一个点是否在一个线段上,这里最容易混淆。

线段上:则看    点p2到点p1的距离 + 点p2到点p0的距离   是否等于线段p0p1的线段长度,就OK了。

import java.util.Scanner;

public class T34 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter three points for p0, p1, p2:");
		float x0=input.nextFloat();
		float y0=input.nextFloat();
		float x1=input.nextFloat();
		float y1=input.nextFloat();
		float x2=input.nextFloat();
		float y2=input.nextFloat();
		input.close();
		
		float lp0p1=(float) Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
		float lp0p2=(float) Math.sqrt((x2-x0)*(x2-x0) + (y2-y0)*(y2-y0));
		float lp1p2=(float) Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
		
		
		 if(lp0p1==lp0p2+lp1p2)
			System.out.print(" ("+x2+", "+y2+") is on the line "
					+ "segment from ("+x0+", "+y0+") to ("+x1+", "+y1+") ");
		 else 
			 System.out.print(" ("+x2+", "+y2+") is not on the line "
						+ "segment from ("+x0+", "+y0+") to ("+x1+", "+y1+") ");
		
	}

}
发布了10 篇原创文章 · 获赞 5 · 访问量 2468

猜你喜欢

转载自blog.csdn.net/cxj18867076391/article/details/104864195