JAVA語言程序設計(基礎篇)第十版——第四章 數學函數、字符和字符串 編程練習題(參考答案)

 

4.2節 + (4.3~4.6節)

4.2節

4.1(幾何:五邊形的面積)

import java.util.Scanner;

public class C1 {

	public static void main(String[] args) {
		
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the length from the center to a vertex: ");
		double r=input.nextFloat();
		input.close();
		
		double s=2*r*Math.sin(Math.PI/5);
		
		double area=(5*s*s)/(4*Math.tan(Math.PI/5));
		
		System.out.printf("The area of the pentagon is %5.2f ", area);
		
		
	}

}

*4.2(幾何:最大圓距離)

這題難就難在如何將從控制檯接收的一個字符串將它分割,就是將字符串裏的( , )逗號字符和()空格字符刪掉。

也就是如何從一個字符串裏獲取子串的問題,我還特地去百度了一下(,)逗號的 Unicode值爲:' \u002C ' .

然後將分割得到的數值型字符串轉化爲數值,就沒什麼難點了(#^.^#)

import java.util.Scanner;

public class C2 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		//用字符串接收s1、s2
		System.out.print("Enter point 1 (latitude and longitude) in degrees: ");
		String s1=input.nextLine();
		System.out.print("Enter point 2 (latitude and longitude) in degrees: ");
		String s2=input.nextLine();
		//input.close();
		
		String s11="";
		String s12="";
		
		//以','字符逗號爲分割點,將s1字符串分爲兩部分
		if(s1.length()!=0 && !s1.equals("")) {
			for(int i=0;i<s1.length();i++) {
				if(s1.charAt(i) == '\u002C') {
					s11=s1.substring(0,i);
					s12=s1.substring(i+1);
				}
			}
		}
		
		//用trim()方法刪去字符串兩邊的空白字符
		String s5=s11.trim();
		String s6=s12.trim();
		
		//將數值型的字符串 轉換爲 數值
		double degree1=Double.parseDouble(s5);
		double degree2=Double.parseDouble(s6);
		
		
		
		String s21="";
		String s22="";
		
		//以','字符逗號爲分割點,將s2字符串分爲兩部分
		if(s2.length()!=0 && !s2.equals("")) {
			for(int i=0;i<s2.length();i++) {
				if(s2.charAt(i) == '\u002C') {
					s21=s2.substring(0,i);
					s22=s2.substring(i+1);
				}
			}
		}
		
		//用trim()方法刪去字符串兩邊的空白字符
		String s7=s21.trim();
		String s8=s22.trim();
		
		//將數值型的字符串 轉換爲 數值
		double degree3=Double.parseDouble(s7);
		double degree4=Double.parseDouble(s8);
		
/*上面代碼是完成input.nextLine()方法在控制檯的接收,
		並將 數值型字符串 轉換爲  數值,進行下面的計算		*/

		
//下面的代碼是完成公式套用計算
		double x1=Math.toRadians(degree1);
		double y1=Math.toRadians(degree2);
		double x2=Math.toRadians(degree3);
		double y2=Math.toRadians(degree4);
		
		double d= 6371.01 * Math.acos(Math.sin(x1)*Math.sin(x2) 
				+ Math.cos(x1)*Math.cos(x2)*Math.cos(y1-y2))  ;
		
		System.out.println("The distance between the two points is " + d +" km ");
	
	
	
	
	
	}

}

*4.3(幾何:估算面積)

首先找出個地方的經緯度:

Atlanta 亞特蘭大 美國 北緯:33°46' 西經:84°25'
Orlando 奧蘭多 美國 北緯:28°3' 西經:81°22'

地名:Savannah     緯度:31.9714   經度:-81.0716

地名:Charlotte      緯度:35.2731    經度:-80.9571

然後給地名先標個點,以致等會兒計算時不會混淆

第某個點:某地名(緯度,經度)

第一個點:Atlanta(33.46, 84.25)

第二個點:Orlando(28.3, 81.22)

第三個點:Savannah(31.9714, -81.0716 )

第四個點:Charlotte(35.2731, -80.9571)

其實沒啥難度,也就是找公式、找經緯度麻煩些。

import java.util.Scanner;

public class C3 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		//用字符串接收s1、s2、s3、s4
		System.out.print("Enter point 1 (latitude and longitude) in degrees: ");
		String s1=input.nextLine();
		System.out.print("Enter point 2 (latitude and longitude) in degrees: ");
		String s2=input.nextLine();
		System.out.print("Enter point 3 (latitude and longitude) in degrees: ");
		String s3=input.nextLine();
		System.out.print("Enter point 4 (latitude and longitude) in degrees: ");
		String s4=input.nextLine();
		input.close();

//以下是 處理s1字符串···············································
		String s11="";
		String s12="";
		
		//以','字符逗號爲分割點,將s1字符串分爲兩部分
		if(s1.length()!=0 && !s1.equals("")) {
			for(int i=0;i<s1.length();i++) {
				if(s1.charAt(i) == '\u002C') {
					s11=s1.substring(0,i);
					s12=s1.substring(i+1);
				}
			}
		}
		
		//用trim()方法刪去字符串兩邊的空白字符
		String s5=s11.trim();
		String s6=s12.trim();
		
		//將數值型的字符串 轉換爲 數值
		double degree1=Double.parseDouble(s5);
		double degree2=Double.parseDouble(s6);
		
//以下是 處理s2字符串···············································
				String s21="";
				String s22="";
				
				//以','字符逗號爲分割點,將s2字符串分爲兩部分
				if(s2.length()!=0 && !s2.equals("")) {
					for(int i=0;i<s2.length();i++) {
						if(s2.charAt(i) == '\u002C') {
							s21=s2.substring(0,i);
							s22=s2.substring(i+1);
						}
					}
				}
				
				//用trim()方法刪去字符串兩邊的空白字符
				String s7=s21.trim();
				String s8=s22.trim();
				
				//將數值型的字符串 轉換爲 數值
				double degree3=Double.parseDouble(s7);
				double degree4=Double.parseDouble(s8);		
				
//以下是 處理s3字符串···············································
				String s31="";
				String s32="";
				
				//以','字符逗號爲分割點,將s3字符串分爲兩部分
				if(s3.length()!=0 && !s3.equals("")) {
					for(int i=0;i<s3.length();i++) {
						if(s3.charAt(i) == '\u002C') {
							s31=s3.substring(0,i);
							s32=s3.substring(i+1);
						}
					}
				}
				
				//用trim()方法刪去字符串兩邊的空白字符
				String s9=s31.trim();
				String s10=s32.trim();
				
				//將數值型的字符串 轉換爲 數值
				double degree5=Double.parseDouble(s9);
				double degree6=Double.parseDouble(s10);
				
//以下是 處理s4字符串···············································
				String s41="";
				String s42="";
				
				//以','字符逗號爲分割點,將s4字符串分爲兩部分
				if(s4.length()!=0 && !s4.equals("")) {
					for(int i=0;i<s4.length();i++) {
						if(s4.charAt(i) == '\u002C') {
							s41=s4.substring(0,i);
							s42=s4.substring(i+1);
						}
					}
				}
				
				//用trim()方法刪去字符串兩邊的空白字符
				String s111=s41.trim();
				String s121=s42.trim();
				
				//將數值型的字符串 轉換爲 數值
				double degree7=Double.parseDouble(s111);
				double degree8=Double.parseDouble(s121);
				
//下面是求兩點間的距離···············································
//先求 點1和點2 之間的距離
				//度數轉換成弧度
				double x1=Math.toRadians(degree1);
				double y1=Math.toRadians(degree2);
				double x2=Math.toRadians(degree3);
				double y2=Math.toRadians(degree4);
				
				double d12= 6371.01 * Math.acos(Math.sin(x1)*Math.sin(x2) 
						+ Math.cos(x1)*Math.cos(x2)*Math.cos(y1-y2))  ;
				
//再求 點2和點3 之間的距離
				//度數轉換成弧度
				double x3=Math.toRadians(degree5);
				double y3=Math.toRadians(degree6);

				
				double d23= 6371.01 * Math.acos(Math.sin(x2)*Math.sin(x3) 
						+ Math.cos(x2)*Math.cos(x3)*Math.cos(y2-y3))  ;
				
		
//再求 點1和點3 之間的距離
			
				double d13= 6371.01 * Math.acos(Math.sin(x1)*Math.sin(x3) 
						+ Math.cos(x1)*Math.cos(x3)*Math.cos(y1-y3))  ;
							
//再求 點3和點4 之間的距離
				//度數轉換成弧度
				double x4=Math.toRadians(degree7);
				double y4=Math.toRadians(degree8);

				
				double d34= 6371.01 * Math.acos(Math.sin(x3)*Math.sin(x4) 
						+ Math.cos(x3)*Math.cos(x4)*Math.cos(y3-y4))  ;
				
//再求 點1和點4 之間的距離
				
				double d14= 6371.01 * Math.acos(Math.sin(x1)*Math.sin(x4) 
						+ Math.cos(x1)*Math.cos(x4)*Math.cos(y1-y4))  ;

//距離算完了,就可以利用算三角形面積公式 求出這個四邊形的面積
/*三角形面積公式:
               s=(邊1+邊2+邊3)/2   
               面積=根號下(   (s*(s-邊1)*(s-邊2)*(s-邊3))  )
               
               */
		//第一個三角形的面積		
		double ss1=(d12+d23+d13)/2;
		double area1=Math.sqrt((ss1*(ss1-d12)*(ss1-d23)*(ss1-d13)));
		
		//第二個三角形的面積
		double ss2=(d13+d34+d14)/2;
		double area2=Math.sqrt((ss2*(ss2-d13)*(ss2-d34)*(ss2-d14)));
		
		//四邊形的面積
		double area= area1 + area2 ;
		
		System.out.println("The total Area is " + area);
	}

}

運行結果:

 

 

4.4(幾何:六邊形面積)

import java.util.Scanner;

public class C4 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the side: ");
		double s=input.nextDouble();
		input.close();
		
		double area=(6*s*s)/(4*Math.tan(Math.PI/6));
		
		System.out.printf("The area of the hexagon is %5.2f", area);
		
	}

}

*4.5(幾何:正多邊形的面積)

這個tan(-)裏面放啥我也不知道,亂編的我

我用的公式和書上的不一樣,我用自己瞎編的公式:面積=(n*s*s) / (4*tan(180°/n)

import java.util.Scanner;

public class C5 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the number of sides: ");
		int n=input.nextInt();
		System.out.print("Enter the side: ");
		double s=input.nextDouble();
		
		double area=(n*s*s)/(4*Math.tan(Math.toRadians(180)/n));
		
		System.out.println("The area of the polygon is " + area);
	}

}

結果也和書上的一樣的

 

*4.6(圓上的隨機點)

import java.util.Scanner;

public class C6 {

	public static void main(String[] args) {
	
		int r=40;//半徑
		
		//第 1個隨機點
		int a1=(int) (Math.random()*2*Math.PI);
		double x1=r*Math.cos(a1);
		double y1=r*Math.sin(a1);
		
		//第 2個隨機點
		int a2=(int) (Math.random()*2*Math.PI);
		double x2=r*Math.cos(a2);
		double y2=r*Math.sin(a2);
		
		//第 3個隨機點
		int a3=(int) (Math.random()*2*Math.PI);
		double x3=r*Math.cos(a3);
		double y3=r*Math.sin(a3);	
		
		double d12=Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
		double d23=Math.sqrt((x2-x3)*(x2-x3) + (y2-y3)*(y2-y3));
		double d13=Math.sqrt((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3));
		
		double a=d23;
		double b=d13;
		double c=d12;
		
		//求角度
		double la=Math.toDegrees( Math.acos((a*a-b*b-c*c)/(-2*b*c)) );
		double lb=Math.toDegrees( Math.acos((b*b-a*a-c*c)/(-2*a*c)) );
		double lc=Math.toDegrees( Math.acos((c*c-a*a-b*b)/(-2*a*b)) );
		
		System.out.println(" ∠A的度數爲 :" + (int)la +"°");
		System.out.println(" ∠B的度數爲 :" + (int)lb +"°");
		System.out.println(" ∠C的度數爲 :" + (int)lc +"°");
		
		
	}

}

*4.7(頂點座標)

這一題請不用糾結,直接套用上一題4.6題的公式就好了

X= r *cos(a)  and   Y= r *sin(a)  公式使用說明在*4.6題的題目裏。

import java.util.Scanner;

public class C7 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the radius of the bounding circle: ");
		double r=input.nextDouble();
		input.close();
		
		//以x軸爲基準,得出度數
		double degree1=18;
		double degree2=90;
		double degree3=162;
		double degree4=-126;
		double degree5=-54;
		
		/*與上一題*4.6一樣,運用它的 X ,Y 的轉換公式,
		你 4.6題懂了,4.7題也就自然懂了,
		很簡單的,就是套用公式,想都不用想的那種*/
		double x1=r*Math.cos(Math.toRadians(degree1));
		double y1=r*Math.sin(Math.toRadians(degree1));
		
		double x2=r*Math.cos(Math.toRadians(degree2));
		double y2=r*Math.sin(Math.toRadians(degree2));
		
		double x3=r*Math.cos(Math.toRadians(degree3));
		double y3=r*Math.sin(Math.toRadians(degree3));
		
		double x4=r*Math.cos(Math.toRadians(degree4));
		double y4=r*Math.sin(Math.toRadians(degree4));
		
		double x5=r*Math.cos(Math.toRadians(degree5));
		double y5=r*Math.sin(Math.toRadians(degree5));
		

		System.out.println("The coordinates of five points on the pentagon are ");  
		System.out.printf("( %7.4f , %7.4f) \n", x1, y1 );
		System.out.printf("( %7.4f , %7.4f) \n", x2, y2 );
		System.out.printf("( %7.4f , %7.4f) \n", x3, y3 );
		System.out.printf("( %7.4f , %7.4f) \n", x4, y4 );
		System.out.printf("( %7.4f , %7.4f) \n", x5, y5 );
	
		
	}

}

4.3~4.6節

 *4.8(給出ASCII碼對應的字符) 

我想哭了·············(我之前傻傻的想把十進制數化十六進制再化成字符,當然最後一步沒實現,也不知道可不可以)

就這兩行代碼想死我了,最後恍然大悟的感覺就是,自己之前沒有掌握這個知識點啦(¬︿̫̿¬☆)

知識點:

ASCII碼的‘十進制數’轉換爲對應的‘字符’的方法:(字符+十進制數)!!!

import java.util.Scanner;

public class C8 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter an ASCII code: integer:(0~127) :");
		int number=input.nextInt();
		
		char ch=(char) ('\u0000'+number);
		System.out.println("The character for ASCII code " + number + " is " + ch);
		
	}

}

*4.9(給出字符的Unicode碼)

有了上一題的折磨,沒錯,這一題就是那麼的快樂粗暴加簡單(●'◡'●)

知識點:

ASCII碼的‘字符’轉換爲對應的‘十進制數’的方法:(字符+十進制數)!!!

對比了一哈,好像都一樣。〒_〒

import java.util.Scanner;

public class C9 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a character :");
		String s=input.nextLine();
		input.close();
		
		char ch=s.charAt(0);
		int a=ch+0;
		System.out.println("The Unicode for the character " + ch + " is " + a);
		
		
	}

}

*4.10(猜測生日)

*4.11(十進制轉十六進制)

import java.util.Scanner;

public class C11 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a decimal value (0 to 15) :" );
		int number=input.nextInt();
		input.close();
		
		if(number>=0 && number<=15) {
			if(number>9) {
				String s = null;
				switch(number) {
				case 10:s="A";
							break;
				case 11:s="B";
							break;
				case 12:s="C";
							break;
				case 13:s="D";
							break;
				case 14:s="E";
							break;
				case 15:s="F";
							break;
				}
				System.out.println(" The hex value is " + s );
			}
			else {
				String s=""+number;
				System.out.println(" The hex value is " + s );
			}
			
			
			
		}
		else {
			System.out.println(" "+ number + " is an invalid input ");
		}
		
		
	}

}

4.12(十六進制轉二進制)

import java.util.Scanner;

public class C12 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a hex digit: ");
		String s=input.nextLine();
		input.close();
		
		char ch=s.charAt(0);
		
		if(ch>='0' && ch<='9') {
			int number= ch + 0 ;
			int remainder1=number%2;//求得第1個餘數
			int remainder2=number/2%2;//求得第2個餘數
			int remainder3=number/2/2%2;//求得第3個餘數
			int remainder4=number/2/2/2%2;//求得第4個餘數
			System.out.println("The binary value is " + 
			remainder4 + remainder3 + remainder2 + remainder1);
		}
		else if(ch>='A' && ch<='F') {
			int number=0;
			switch(ch) {
			case 'A': number += 10;
						break;
			case 'B': number += 11;
						break;
			case 'C': number += 12;
						break;
			case 'D': number += 13;
						break;
			case 'E': number += 14;
						break;
			case 'F': number += 15;
						break;
			}
			
			int remainder1=number%2;//求得第1個餘數
			int remainder2=number/2%2;//求得第2個餘數
			int remainder3=number/2/2%2;//求得第3個餘數
			int remainder4=number/2/2/2%2;//求得第4個餘數
			System.out.println("The binary value is " + 
			remainder4 + remainder3 + remainder2 + remainder1);
		
		}
		else {
			System.out.println(ch+" is an invalid input ");
		}
		
		
	}

}

*4.13(判斷元音還是輔音)

import java.util.Scanner;

public class C13 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a letter: ");
		String s=input.nextLine();
		input.close();
		
		char ch=s.charAt(0);
		
		if(ch>='A' && ch<='Z' || ch>='a' && ch<='z') {
			
			if(ch=='A'|| ch=='E'|| ch=='I'|| ch=='O'|| ch=='U'
			|| ch=='a'|| ch=='e'|| ch=='i'|| ch=='o'|| ch=='u') {
		 
				System.out.println(ch+" is a vowel");
						
			}
			else {
				System.out.println(ch+" is a consonant");
			}
			
			
		}
		else {
			System.out.println(ch + " is an invalid input");
		}
		
		
		
		
	}

}

*4.14(轉換字母等級爲數字)

import java.util.Scanner;

public class c14 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a letter grade: ");
		String s=input.nextLine();
		input.close();
		
		char ch=s.charAt(0);
		
		if(ch>='A' && ch<='D' || ch=='F') {
			int number=0;
			switch(ch) {
			case 'A':number+=4;
						break;
			case 'B':number+=3;
						break;
			case 'C':number+=2;
						break;
			case 'D':number+=1;
						break;
			case 'F':number+=0;
						break;
			}
			System.out.println("The numeric value for grade "+ch+" is "+number);
		}
		else {
			System.out.println( ch + " is an invalid grade");
		}
		
		
		
		
	}

}

*4.15(電話鍵盤)

import java.util.Scanner;

public class C15 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a letter: ");
		String s=input.nextLine();
		input.close();
		
		char ch=s.charAt(0);
		
		if(Character.isLetter(ch)) {
			String s1=""+ch;
			if(s1.equalsIgnoreCase("a") 
			|| s1.equalsIgnoreCase("b")
			|| s1.equalsIgnoreCase("c") ) {
				System.out.println("The corresponding number is " + 2 );
			}
			else if(s1.equalsIgnoreCase("d") 
					|| s1.equalsIgnoreCase("e")
					|| s1.equalsIgnoreCase("f") ) {
				System.out.println("The corresponding number is " + 3 );
			}
			else if(s1.equalsIgnoreCase("g") 
					|| s1.equalsIgnoreCase("h")
					|| s1.equalsIgnoreCase("i") ) {
				System.out.println("The corresponding number is " + 4 );
			}
			else if(s1.equalsIgnoreCase("j") 
					|| s1.equalsIgnoreCase("k")
					|| s1.equalsIgnoreCase("l") ) {
				System.out.println("The corresponding number is " + 5 );
			}
			else if(s1.equalsIgnoreCase("m") 
					|| s1.equalsIgnoreCase("n")
					|| s1.equalsIgnoreCase("o") ) {
				System.out.println("The corresponding number is " + 6 );
			}
			else if(s1.equalsIgnoreCase("p") 
					|| s1.equalsIgnoreCase("q")
					|| s1.equalsIgnoreCase("r")
					|| s1.equalsIgnoreCase("s")) {
				System.out.println("The corresponding number is " + 7 );
			}
			else if(s1.equalsIgnoreCase("t") 
					|| s1.equalsIgnoreCase("u")
					|| s1.equalsIgnoreCase("v") ) {
				System.out.println("The corresponding number is " + 8 );
			}
			else if(s1.equalsIgnoreCase("w") 
					|| s1.equalsIgnoreCase("x")
					|| s1.equalsIgnoreCase("y")
					|| s1.equalsIgnoreCase("z")) {
				System.out.println("The corresponding number is " + 9 );
			}
			
		}
		else {
			System.out.println(ch + " is an invalid input");
		}
	}

}

4.16(隨機字符)

public class C16 {

	public static void main(String[] args) {
		int random= (int) (65 + Math.random()*26);//大寫字母的十進制編碼值從65~90
		char ch=(char) ('\u0000'+random);
		System.out.println("這個隨機字符是: " + ch);
		
		
	}

}

*4.17(一個月中的日期)

我在編碼過程中發現的問題:

當我在控制檯先輸入一個整型數值,接着再輸入一個字符串時:控制檯好像不接收第二個字符串數據,其實是接收了“回車鍵”的字符串,所以顯得好像沒有接收數據。

解決方案:1.可以讓控制檯接收的數據類型都是字符串先,然後再將字符串轉換爲數值類型。

解決方案:2.可以創建兩個Scanner對象分別接收不同的數據類型。

我下面的代碼用瞭解決方案 1. 

import java.util.Scanner;

public class C17 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a year: ");
		String yearString=input.nextLine();
		
		System.out.print("Enter a month: ");
		String month=input.nextLine();
		input.close();
		
		int days=0;
		
		//將字符串轉爲整數
		int year=Integer.parseInt(yearString);
		
		//判定是否閏年
		boolean isLeapYear=( (year%4==0 && year%100!=0) || (year%400==0) );  
	
		switch(month) {
		case "Jan":days=31;
					break;
		case "Feb":	if(isLeapYear) {
							days=29;
						}
						else {
							days=28;
							}
					break;
		case "Mar":days=31;
					break;
		case "Apr":days=30;
					break;
		case "May":days=31;
					break;
		case "Jun":days=30;
					break;
		case "Jul":days=31;
					break;
		case "Aug":days=31;
					break;
		case "Sep":days=30;
					break;
		case "Oct":days=31;
					break;
		case "Nov":days=30;
					break;
		case "Dec":days=31;
					break;
		
		}  
		
		System.out.println(month +" "+ year + " has " + days +  " days");
		
	}

}

 

*4.18 (學生的專業和狀況)

import java.util.Scanner;

public class C18 {

	public static void main(String[] args) {
		Scanner input =new Scanner(System.in);
		System.out.print("Enter two character:");
		String s=input.nextLine();
		input.close();
		
		char ch1=s.charAt(0);
		char ch2=s.charAt(1);
		int number=ch2+0;
		String major=null;
		String student=null;
		
		if((ch1=='M'||ch1=='C'||ch1=='I') 
			&& (ch2=='1'||ch2=='2'||ch2=='3'||ch2=='4')) {
			
		switch(ch1) {
		case 'M':major="Mathmatics";
				break;
		case 'C':major="Computer Science";
				break;
		case 'I':major="Information Technology";
				break;
		}
		
		
		switch(ch2) {
		case '1':student="Freshman";
				break;
		case '2':student="Sophomore";
				break;
		case '3':student="Junior";
				break;
		case '4':student="Senior Student";
				break;
			}
		
		System.out.println(major+" "+student);
		
		}
		else {
			System.out.println("Invalid input");
		}
		
		
	}

}

4.19(商業:檢測ISBN-10)

4.20(字符串處理)

import java.util.Scanner;

public class C20 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("請輸入一個字符串: ");
		String s=input.nextLine();
		int length=s.length();
		char ch1=s.charAt(0);
		System.out.println("該字符串的長度爲: "+length);
		System.out.println("該字符串的第一個字符爲: "+ch1);
	}

}

 *4.21(檢查子串)

import java.util.Scanner;

public class C21 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a SSN: ");
		String s=input.nextLine();
		input.close();
		
		if(s.length()==11) {
			if(Character.isDigit(s.charAt(0)) 
				&& Character.isDigit(s.charAt(1)) 
				&& Character.isDigit(s.charAt(2)) 
				&& (s.charAt(3)=='-')
				&& Character.isDigit(s.charAt(4)) 
				&& Character.isDigit(s.charAt(5)) 
				&& (s.charAt(6)=='-') 
				&& Character.isDigit(s.charAt(7)) 
				&& Character.isDigit(s.charAt(8)) 
				&& Character.isDigit(s.charAt(9)) 
				&& Character.isDigit(s.charAt(10)) ) {
				System.out.println(s+" is a valid social security number");
			}
			else {
				System.out.println(s+" is an invalid social security number");
			}
		}
		else {
			System.out.println(s+" is an invalid social security number");
		}
		
	}

}

4.22(檢測子串)

知識點:用字符串裏的 s1.contain(s2) 方法:如果S2是s1字符串的子字符串,返回true。

import java.util.Scanner;

public class C22 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter string s1: ");
		String s1=input.nextLine();
		
		System.out.print("Enter string s2: ");
		String s2=input.nextLine();
		input.close();
		
		if(s1.contains(s2)) {
			System.out.println(s2+" is a substring of "+s1);	
		}
		else{
			System.out.println(s2+" is not a substring of "+s1);
		}
	
	}

}

*4.23(財務應用:酬金)

import java.util.Scanner;

public class C23 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter employee's name:");
		String name=input.nextLine();
		
		System.out.print("Enter number of hours worked in a week:");
		double hour=input.nextDouble();
		
		System.out.print("Enter hourly pay rate:");
		double payRate=input.nextDouble();
		
		System.out.print("Enter federal tax withholding rate:");
		double federalRate=input.nextDouble();
		
		System.out.print("Enter state tax withholding rate:");
		double stateRate=input.nextDouble();
		input.close();
		
		double grossPay=hour*payRate;
		
		System.out.printf("Employee Name: %s \n"
				+ "Hours Worked: %3.1f \n"
				+"Pay Rate: $%4.2f \n"
				+"Gross Pay: $%4.1f \n"
				+"Deductions: \n"
				+"  Federal Withholding (%4.1f%%): $%4.1f \n"
				+"  State Withholding (%3.1f%%): $%4.2f \n"
				+"  Total Dedution: $%5.2f \n"
				+"Net Pay: $%5.2f ", name, hour, payRate, grossPay, federalRate*100, federalRate*grossPay, stateRate*100, stateRate*grossPay, federalRate*grossPay+stateRate*grossPay, grossPay-(stateRate*grossPay+federalRate*grossPay) );
				
		
		
	}

}

*4.24(對三個城市排序)

import java.util.Scanner;

public class C24 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the first city: ");
		String city1=input.nextLine();
		
		System.out.print("Enter the second city: ");
		String city2=input.nextLine();
		
		System.out.print("Enter the third city: ");
		String city3=input.nextLine();
		
		if(city1.compareTo(city2)>0) {
			//System.out.println("The order is city2, city1.");
			
			if(city2.compareTo(city3)>0) {
				System.out.println("The three cities in alphabetical order are "+city3+", "+city2+", "+city1);
				
			}
			else if(city2.compareTo(city3)<0) {
				if(city1.compareTo(city3)<0) {
					System.out.println("The three cities in alphabetical order are "+city2+", "+city1+", "+city3);
				}
				else if(city1.compareTo(city3)>0){
					System.out.println("The three cities in alphabetical order are "+city2+", "+city3+", "+city1);
				}
			}
			
		}
		else if (city1.compareTo(city2)<0){
			//System.out.println("The order is city1, city2.");
			
			if(city1.compareTo(city3)>0) {
				System.out.println("The three cities in alphabetical order are "+city3+", "+city1+", "+city2);
			}
			else if(city1.compareTo(city3)<0) {
				
				if(city2.compareTo(city3)>0) {
					System.out.println("The three cities in alphabetical order are "+city1+", "+city3+", "+city2);
	
				}
				else if(city2.compareTo(city3)<0){
					System.out.println("The three cities in alphabetical order are "+city1+", "+city2+", "+city3);

				}
			}
			
		}
			
		
		
	}

}

*4.25(生成車牌號碼)

public class C25 {

	public static void main(String[] args) {

		//產生3個隨機大寫字母 和 4個隨機數字
		char ch1=(char) (65+Math.random()*26);
		char ch2=(char) (65+Math.random()*26);
		char ch3=(char) (65+Math.random()*26);
		int number=(int) (1000+Math.random()*9000);
		System.out.println("一個隨機的車牌號是: "+ch1+ch2+ch3+number);

	}

}

*4.26(財務應用:貨幣單位)

 

 

 

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