java語言程式設計基礎篇第四章程式設計練習題
阿新 • • 發佈:2019-01-05
1 import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); double r = input.nextDouble(); double s = 2*r*Math.sin(Math.PI/5); double area = (5*Math.pow(s, 2))/(4*Math.tan(Math.PI/5)); System.out.println(area); } } 2 import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); double R = 6371.01; double x1 = input.nextDouble(); x1 = Math.toRadians(x1); double y1 = input.nextDouble(); y1 = Math.toRadians(y1); double x2 = input.nextDouble(); x2 = Math.toRadians(x2); double y2 = input.nextDouble(); y2 = Math.toRadians(y2); double distance = R*Math.acos(Math.sin(x1)*Math.sin(x2) + Math.cos(x1)*Math.cos(x2)*Math.cos(y1-y2)); System.out.println(distance); } }
3 這是個悲劇,給出的地圖網址打不開,雖然百度地圖也可以找到,但是我可真不想去美國地圖上找城市,還要算經緯度,直接放棄這題了。 4 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the side:"); double side = input.nextDouble(); double s = (6*side*side)/(4*Math.tan(Math.PI/6)); System.out.println("The area of the hexagon is " + s); } } 5 書上的公式給錯了,正確公式:面積 = (n*s*s)/(4*tan(π/n)) import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of sides:"); int numberOfTheSide = input.nextInt(); System.out.print("Enter the sides:"); double side = input.nextDouble(); double s = (numberOfTheSide*side*side)/(4*Math.tan(Math.PI/numberOfTheSide)); System.out.println("The area of the ploygon is " + s); } } 6 import java.util.Scanner; public class Main { public static void main(String[] args) { double radius = 40.0; double radians = Math.random()*Math.PI*2; double x1 = radius * Math.cos(radians); double y1 = radius * Math.sin(radians); radians = Math.random()*Math.PI*2; double x2 = radius * Math.cos(radians); double y2 = radius * Math.sin(radians); radians = Math.random()*Math.PI*2; double x3 = radius * Math.cos(radians); double y3 = radius * Math.sin(radians); double sideA = Math.sqrt(Math.pow(x1-x2,2.0) + Math.pow(y1-y2, 2.0)); double sideB = Math.sqrt(Math.pow(x1-x3,2.0) + Math.pow(y1-y3, 2.0)); double sideC = Math.sqrt(Math.pow(x3-x2,2.0) + Math.pow(y3-y2, 2.0)); double cosA = (sideB*sideB + sideC*sideC - sideA*sideA)/(2*sideB*sideC); double cosB = (sideA*sideA + sideC*sideC - sideB*sideB)/(2*sideA*sideC); double cosC = (sideB*sideB + sideA*sideA - sideC*sideC)/(2*sideB*sideA); System.out.println("cosA: " + Math.toDegrees(Math.acos(cosA))); System.out.println("cosB: " + Math.toDegrees(Math.acos(cosB))); System.out.println("cosC: " + Math.toDegrees(Math.acos(cosC))); } } 7 import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner cin = new Scanner(System.in); double oneDegree = Math.PI/180; System.out.print("Enter the radius of the bounding circle:"); double radius = cin.nextDouble(); System.out.println("The coordinating of five points on the pentagon are"); //y軸上那個點 System.out.println("(" + 0 + "," + radius + ")"); double x = Math.sqrt((radius*radius)/(1+Math.tan(18*oneDegree)*Math.tan(18*oneDegree))); double y = x*Math.tan(18*oneDegree); System.out.println("(" + x + "," + y + ")"); System.out.println("(" + -x + "," + y + ")"); x = Math.sqrt((radius*radius)/(1+Math.tan(54*oneDegree)*Math.tan(54*oneDegree))); y = x*Math.tan(54*oneDegree); System.out.println("(" + x + "," + y + ")"); System.out.println("(" + -x + "," + y + ")"); } } 8 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an ACSII code:"); byte num = input.nextByte(); char c = (char)num; System.out.println("The character for ACSII code " + num + " is " + c); } } 9 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a charater:"); String s = input.nextLine(); char c = s.charAt(0); int num = (int)c; System.out.println("The Unicode for the character " + c + " is " + num); } } 10 import java.util.Scanner; public class Main { public static void main(String[] args) { String set1 = " 1 3 5 7\n"+ " 9 11 13 15\n"+ "17 19 21 23\n"+ "25 27 29 31"; String set2 = " 2 3 6 7\n"+ "10 11 14 15\n"+ "18 19 22 23\n"+ "26 27 30 31"; String set3 = " 4 5 6 7\n"+ "12 13 14 15\n"+ "20 21 22 23\n"+ "28 29 30 31"; String set4 = " 8 9 10 11\n"+ "12 13 14 15\n"+ "24 25 26 27\n"+ "28 29 30 31"; String set5 = "16 17 18 19\n"+ "20 21 22 23\n"+ "24 25 26 27\n"+ "28 29 30 31"; int day = 0; Scanner input = new Scanner(System.in); System.out.println("Is your birthday in set1?"); System.out.print(set1); System.out.print("\nEnter N for No and Y for Yes:"); String s = input.nextLine(); if(s.charAt(0) == 'Y') ++day; System.out.println("Is your birthday in set2?"); System.out.print(set2); System.out.print("\nEnter N for No and Y for Yes:"); s = input.nextLine(); if(s.charAt(0) == 'Y') day += 2; System.out.println("Is your birthday in set3?"); System.out.print(set3); System.out.print("\nEnter N for No and Y for Yes:"); s = input.nextLine(); if(s.charAt(0) == 'Y') day += 4; System.out.println("Is your birthday in set4?"); System.out.print(set4); System.out.print("\nEnter N for No and Y for Yes:"); s = input.nextLine(); if(s.charAt(0) == 'Y') day += 8; System.out.println("Is your birthday in set5?"); System.out.print(set5); System.out.print("\nEnter N for No and Y for Yes:"); s = input.nextLine(); if(s.charAt(0) == 'Y') day += 16; System.out.println("\nYour birthday is " + day + "!"); } }
11 import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.print("Enter a decimal value(0 to 15):"); Scanner input = new Scanner(System.in); int num = input.nextInt(); char ch = (char)num; char hex = (char)(ch + 'A' - 10); System.out.println("The hex value is " + hex); } }
12
import java.util.*;
public class Main {
public static void main(String[] args) {
String[] hexs = {"0000", "0001", "0010", "0011", "0100", "0101",
"0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101",
"1110", "1111"};
Scanner input = new Scanner(System.in);
String hex = input.nextLine();
if (hex.charAt(0) > 'F' || hex.charAt(0) < '0')
System.out.println(hex.charAt(0) + " is an invalid input.");
else {
char ch = hex.charAt(0);
if(ch >= 'A' && ch <= 'F')
System.out.println(hexs[(int)(hex.charAt(0) - 'A' + 10)]);
else
System.out.println(hexs[(int)(ch-'0')]);
}
}
}
13
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a letter:");
String alpha = input.nextLine();
char ch = alpha.charAt(0);
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 if(Character.isLetter(ch))
System.out.println(ch + " is a consonant");
else
System.out.println(ch + " is a invalid input");
}
}
14
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] grades = {4,3,2,1,0,0};
Scanner input = new Scanner(System.in);
System.out.print("Enter a letter grade:");
String letter = input.nextLine();
char alpha = letter.charAt(0);
if(alpha == 'A' || alpha == 'B' || alpha == 'C' || alpha == 'D' || alpha == 'F')
System.out.println("The numeric value for grade " + alpha + " is " + grades[(int)(alpha-'A')]);
else
System.out.println(alpha + " is an invalid grade");
}
}
15
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] key = {"abcABC","defDEF","ghiGHI","jklJKL","mnoMNO",
"pqrsPQRS","tuvTUV","wxyzWXYZ"};
Scanner input = new Scanner(System.in);
System.out.print("Enter a letter:");
String letters = input.nextLine();
char letter = letters.charAt(0);
if(Character.isLetter(letter)){
for(int i = 0; i < 8; ++i){
if(key[i].indexOf(letter) != -1){
System.out.println("The corresponding number is " + (i+2));
break;
}
}
}
else
System.out.println(letter + " is an invalid input");
}
}
16
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int num = (int)(Math.random()*26);
System.out.println((char)(num+'A'));
}
}
17
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] months = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
int[] dayOfMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
Scanner input = new Scanner(System.in);
System.out.print("Enter a year:");
int year = input.nextInt();
System.out.print("Enter a month:");
//吃回車的
String temp = input.nextLine();
String month = input.nextLine();
int days = 0;
for(int i = 0; i < 12; ++i)
if(months[i].equals(month)){
days = dayOfMonth[i];
break;
}
if(month.equals("Feb")){
if((year%400 == 0) || (year%4 == 0 && year%100 != 0))
++days;
}
System.out.println(month + " " + year + " has " + days + " days");
}
}
18
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter two characters:");
String str = input.nextLine();
char ch = str.charAt(0);
int num = Integer.parseInt((str.charAt(1)+""));
if(ch != 'M' && ch != 'C' && ch != 'I'){
System.out.println("Invalid input");
return ;
}
if(num < 1 || num > 4){
System.out.println("Invalid input");
return ;
}
int flag = 0;
if(ch == 'M' && num == 1){
System.out.print("Mathematics ");
flag = 1;
}
else if(ch == 'C' && num == 1){
System.out.print("Computer Science ");
flag = 1;
}
else if(ch == 'I' && num == 1){
System.out.print("Information Technology ");
flag = 1;
}
if(flag == 1){
System.out.println("Freshman");
return ;
}
if(ch == 'M')
System.out.print("Mathematics ");
else if(ch == 'C')
System.out.print("Computer Science ");
else if(ch == 'I')
System.out.print("Information Technology ");
System.out.println("Junior");
}
}
19
程式設計練習題3.9,字元轉數字
20
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a string:");
String str = input.nextLine();
System.out.println("length:" + str.length() + "\n" + "The first character:" + str.charAt(0));
}
}
21
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a SSN:");
String str = input.nextLine();
if(str.indexOf('-') == 3 && str.lastIndexOf('-') == 6){
System.out.println(str + " is a valid social security number");
}
else
System.out.println(str + " is an ivalid social security number");
}
}
22
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter string s1:");
String str1 = input.nextLine();
System.out.print("Enter string s2:");
String str2 = input.nextLine();
if(str1.indexOf(str2) != -1){
System.out.println(str2 + " is a substring of " + str1);
}
else
System.out.println(str2 + " is not a substring of " + str1);
}
}
23
。。。。。
24
import java.util.Scanner;
public class Main{
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();
String temp = null;
if(city1.compareTo(city2) < 0){
temp = city1;
city1 = city2;
city2 = temp;
}
if(city1.compareTo(city3) < 0){
temp = city1;
city1 = city3;
city3 = temp;
}
if(city2.compareTo(city3) < 0){
temp = city2;
city2 = city3;
city3 = temp;
}
System.out.print(city1 + "\n" + city2 + "\n" + city3);
}
}
25
import java.util.Scanner;
public class Main{
public static void main(String[] args){
char ch1 = (char)((int)(Math.random()*26) + 'A');
char ch2 = (char)((int)(Math.random()*26) + 'A');
char ch3 = (char)((int)(Math.random()*26) + 'A');
String num = ch1 + "" + ch2 + "" + ch3 + "" + (int)(Math.random()*10) + ""
+ (int)(Math.random()*10) + "" + (int)(Math.random()*10) + "" + (int)(Math.random()*10);
System.out.println(num);
}
}
26
indexOf定位,substring提取