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+") ");
		
	}

}

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章