第四章 数学函数、字符和字符串
目录
第四章 数学函数、字符和字符串
4.2几何:最大圆距离
最大圆距离是指球面上两点的距离
代码:
public class Exercise04_02 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter point 1 (latitude and longitude) in degrees: ");
double x1= in.nextDouble();
double y1= in.nextDouble();
System.out.print("Enter point 2 (latitude and longitude) in degrees: ");
double x2= in.nextDouble();
double y2= in.nextDouble();
in.close();
double r=6371.01;
x1=Math .toRadians(x1);
x2=Math .toRadians(x2);
y1=Math .toRadians(y1);
y2=Math .toRadians(y2);
double d=r *(Math.acos(Math.sin(x1) * Math.sin(x2) + Math.cos(x1) * Math.cos(x2) * Math.cos(y1- y2)));
System.out.printf("The distance between the two points is "+d+"km");
}}
4.5几何:正多边形的面积
求正多边形的面积:
代码:
import java.util.*;
public class Exercise04_05 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter the number of sides:");
int n=in.nextInt();
System.out.print("Enter the side: ");
double s=in.nextDouble();
in.close();
double area=(n*s*s)/(4.0*Math.tan(Math.PI/n));
System.out.printf("The area of the polygon is "+area);
}
}
4.6圆上的随机点
产生一个圆心在(0,0 )、半径为 40的圆上面的3个随机点,显示由这三个随机点组成的三角形的三个角的度数。
代码:
package firstprogram;
import java.util.Scanner;
public class Exercise04_06 {
public static void main(String[] args) {
float rad1;
float rad2;
float rad3;
while(true) {
//随机产生三个弧度
rad1=2*(float)(Math.PI*Math.random());
rad2=2*(float)(Math.PI*Math.random());
rad3=2*(float)(Math.PI*Math.random());
//防止弧度相等
if(rad1==rad2||rad2==rad3||rad1==rad3) {
continue;
}
else
break;
}
float x1=40*(float)Math.cos(rad1);
float x2=40*(float)Math.cos(rad2);
float x3=40*(float)Math.cos(rad3);
float y1=40*(float)Math.sin(rad1);
float y2=40*(float)Math.sin(rad2);
float y3=40*(float)Math.sin(rad3);
System.out.println("Three points are :");
System.out.printf("A(%.2f,%.2f) B(%.2f,%.2f) C(%.2f,%.2f)",x1,y1,x2,y2,x3,y3);//精度2
//边长
float s1=(float)Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2, 2));
float s2=(float)Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3, 2));
float s3=(float)Math.sqrt(Math.pow(x1-x3,2)+Math.pow(y1-y3, 2));
//角度
float A=(float)Math.toDegrees(Math.acos((s2*s2+s3*s3-s1*s1)/(2*s2*s3)));
float B=(float)Math.toDegrees(Math.acos((s1*s1+s3*s3-s2*s2)/(2*s1*s3)));
float C=(float)Math.toDegrees(Math.acos((s2*s2+s1*s1-s3*s3)/(2*s2*s1)));
System.out.println("Three angles are: ");
System.out.printf("A:%.2f B:%.2f C:%.2f",A,B,C);
}
}
4.7顶点坐标
输入正五边形外切圆的半径,中心(0,0),显示五个顶点坐标。
代码:
import java.util.Scanner;
public class Exercise04_07 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter the radius of the bounding circle: ");
double r=in.nextDouble();
System.out.println("The coordinates of five points on the pentagon are");
double angle=Math.PI/2-2*Math.PI/5;
double x=0;
double y=0;
for(int i=0;i<5;i++) {
x=r*Math.cos(angle);
y=r*Math.sin(angle);
System.out.printf("(%.4f,%.4f)\n",x,y);
angle+=2*Math.PI/5;
}
in.close();
}
}
4.8输出ACSII码对应的字符
代码:
public class Exercise04_08 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter an ASCII code: ");
int c=in.nextInt();
System.out.println("The character for ASCII code "+c+" is "+(char)c);
}
}
4.9输出字符的Unicode码
代码:
import java.util.Scanner;
public class Exercise04_09 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter a character: ");
char c=in.next().charAt(0);
System.out.println("The Unicode for the character "+c+" is "+(int)c);
}
}
4.11十进制转十六进制(0-15之间的整数)
(注意:10以上的数字的转化)
代码:
import java.util.Scanner;
public class Exercise04_11 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter a decimal value (0 to 15): ");
int num=in.nextInt();
if(num>15||num<0) {
System.out.println(num+" is an invalid input");
}
else if(num<10){
System.out.println("The hex value is "+num);
}
else {
System.out.println("The hex value is "+(char)('A'+num-10));
}
}
}
4.13判断元音还是辅音
输入字母,判断元音辅音
代码:
import java.util.Scanner;
public class Exercise04_08 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter a letter: ");
char l=in.next().charAt(0);
if(Character.toUpperCase(l)=='A'||Character.toUpperCase(l)=='O'||Character.toUpperCase(l)=='E'||Character.toUpperCase(l)=='I'||Character.toUpperCase(l)=='U')
{
System.out.println(l+" is a vowel");
}
else if(Character.isLetter(l))
{System.out.println(l+" is a consonant");}
else
System.out.println(l+" is an invalid input");
}
}
4.17一个月中的日期
输入一个年份名称的前三个字母,显示该月的天数。
public class Exercise04_17 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter a year: ");
int year=in.nextInt();
System.out.print("Enter a month: ");
String month=in.next();
if(month.equals("Jan")||month.equals("Mar")||month.equals("May")||month.equals("Jul")||
month.equals("Agu")||month.equals("Oct")||month.equals("Dec"))
{
System.out.println(month+" "+year+" has 31 days");
}
else if(month.equals("Nov")||month.equals("Apr")||month.equals("Jun")||month.equals("Sep"))
{
System.out.println(month+" "+year+" has 30 days");
}
else if(month.equals("Feb")) {
if((year%400==0)||(year%4==0&&year%100!=0)) {
System.out.println(month+" "+year+" has 29 days");}
else {
System.out.println(month+" "+year+" has 28 days");}
}
else {
System.out.println(month+" is not a correct month name");}
}}
4.18学生的专业和状况
public class Exercise04_18 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter two characters: ");
String s=in.nextLine();
if(s.charAt(0)=='M')
System.out.print("Mathematics ");
else if(s.charAt(0)=='C') {
System.out.print("Computer Science ");
}
else if(s.charAt(0)=='I') {
System.out.print("Information technology ");
}
else {
System.out.print("Invalid input ");
System.exit(1);
}
if(s.charAt(1)=='1') {
System.out.print("Freshman");
}
else if(s.charAt(1)=='2') {
System.out.print("Sophomore ");
}
else if(s.charAt(1)=='3') {
System.out.print("Junior");
}
else if(s.charAt(1)=='4') {
System.out.print("Senior");
}
else {
System.out.print("Invalid status code ");
System.exit(2);
}
}}
4.21检测SSN
社保号码,格式是DDD-DD-DDDD,D是数字,判断输入是否合法。
public class Exercise04_18 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter a SSN: ");
String s=in.nextLine();
if((s.charAt(3)=='-')&&(s.charAt(6)=='-')&&(Character.isDigit(s.charAt(0)))
&&(Character.isDigit(s.charAt(1)))&&(Character.isDigit(s.charAt(2)))
&&(Character.isDigit(s.charAt(4)))&&(Character.isDigit(s.charAt(5)))
&&(Character.isDigit(s.charAt(7)))&&(Character.isDigit(s.charAt(8)))
&&(Character.isDigit(s.charAt(9))))
{
System.out.println(s+" is a valid social security number");
}
else {
System.out.println(s+" is an invalid social security number");
}
}}
4.23财务应用:酬金
输出酬金声明。
import java.util.Scanner;
public class Exercise04_23 {
public static void main(String[] args) {
String name;
double hours;
double hpay;
double ftaxr;//联邦税率
double staxr;//州税率
System.out.print("Enter employee's name: ");
Scanner in=new Scanner(System.in);
name=in.nextLine();
System.out.print("Enter number of hours worked in a week: ");
hours=in.nextDouble();
System.out.print("Enter hourly pay rate: ");
hpay=in.nextDouble();
System.out.print("Enter federal tax withholding rate: ");
ftaxr=in.nextDouble();
System.out.print("Enter state tax withholding rate: ");
staxr=in.nextDouble();
double total=hours*hpay;
double ftax=ftaxr*total;
double stax=staxr*total;
double alltax=ftax+stax;
double net=total-alltax;
System.out.println("Employee Name: "+name);
System.out.println("Hours Worked: "+hours);
System.out.println("Pay Rate: $"+hpay);
System.out.println("Gross Pay: $"+total);
System.out.println("Deductions:\n"
+ " Federal Withholding ("+(int)(ftaxr*100)/100.0+"%): $"+ftax);
System.out.println(" State Withholding ("+(int)(staxr*100)/100.0+"%): $"+(int)(stax*100)/100.0);
System.out.println(" STotal Deduction: $"+(int)(alltax*100)/100.0);
System.out.print("Net Pay: $"+(int)(net*100)/100.0);
}
}
补充知识点:
1.三角函数
习题4.2 使用了Math.toRadians()方法将角度转化为弧度,
还使用了acos()方法计算arcos反三角函数。
习题4.6 使用了Math.toDegrees()方法将弧度转化为角度。
2.读取字符串
-
如果只需要读取字符串中的一个字符,可以使用charAt()方法。
例:
String str = “abc”;
从字符串中取字符
char ch = str.charAt(0); 第一个字符
char ch2 = str.charAt(1); 第二个字符
ch是a,ch2是b; -
使用next()方法时,输入的字符串不能含有空格。
-
nextLine()方法可以一次性读取一行数据,不受空格的影响。
-
java接收字符串还可以使用BufferedReader类,调用readLine()方法来获得字符串
3.字符串转化大小写的方法
习题4.11 使用了Character.toUpperCase()方法,返回一个新的字符串,其中所有的字母大写,方便判断。
Character.isLetter(ch)如果指定的字符是一个字母,返回true。
4.exit退出
system.exit(int status) 。
正常退出:
status为0时为正常退出程序,也就是结束当前正在运行中的java虚拟机。
非正常退出:
status为非0的其他整数(包括负数,一般是1或者-1),表示非正常退出当前程序。
5.保留小数位
见例题4.23
(int)(net*100)/100.0)保留小数点后2位