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(财务应用:货币单位)
略