发布于2020-11-19 20:24 阅读(1536) 评论(0) 点赞(10) 收藏(1)
在这里,我选择了几个知识点涉及稍微较多的课后习题进行求解(若有错误,欢迎斧正)
import java.util.Scanner;
public class test4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入正五边形中点到顶点的距离:");
double r=input.nextDouble();
double s=2*r*Math.sin(Math.PI/5);
System.out.printf("五边形的边长为:%.2f",s);
double area=5*s*s/(4*Math.tan(Math.PI/5));
System.out.printf("\n五边形的面积为:%.2f",area);
}
}
import java.util.Scanner;
public class test5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("Enter point 1(latitude and longitude)in degrees:");
double pointx1=input.nextDouble();
double pointy1=input.nextDouble();
System.out.println("Enter point 2(latitude and longitude)in degrees:");
double pointx2=input.nextDouble();
double pointy2=input.nextDouble();
double r=6371.01;
double d=r*Math.acos(Math.sin(Math.toRadians(pointx1))*Math.sin(Math.toRadians(pointx2))+Math.cos(Math.toRadians(pointx1))*Math.cos(Math.toRadians(pointx2))*Math.cos(Math.toRadians(pointy1-pointy2)));
System.out.println("The distance between the two points is "+d+" km");
}
}
import java.util.Scanner;
public class tset6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("Ener the number of sides:");
int n=input.nextInt();
System.out.println("Enter the side:");
double r=input.nextDouble();
double area = n*r*r/(4*Math.tan(Math.PI/n));
System.out.println("The area of the polygon is "+area);
}
}
import java.util.Scanner;
public class test7 {
public static void main(String[] args) {
// TODO Auto-generated method stub
double r=40;
double x1,y1,x2,y2,x3,y3;
double angle1,angle2,angle3,alpha;
double d1,d2,d3;
//第一个随机点
alpha=Math.random()*Math.PI*2;
x1=r*Math.cos(alpha);
y1=r*Math.sin(alpha);
//第二个随机点
alpha=Math.random()*Math.PI*2;
x2=r*Math.cos(alpha);
y2=r*Math.sin(alpha);
//第三个随机点
alpha=Math.random()*Math.PI*2;
x3=r*Math.cos(alpha);
y3=r*Math.sin(alpha);
d1=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
d2=Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
d3=Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
angle1=Math.toDegrees(Math.acos((d1*d1+d2*d2-d3*d3)/(2*d1*d2)));
angle2=Math.toDegrees(Math.acos((d1*d1+d3*d3-d2*d2)/(2*d1*d3)));
angle3=Math.toDegrees(Math.acos((d2*d2+d3*d3-d1*d1)/(2*d2*d3)));
System.out.println("angle1:"+angle1+"\nangle2:"+angle2+"\nangle3:"+angle3);
}
}
//代码1
import java.util.Scanner;
public class test8 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("Enter the radius of the bounding circle:");
double r=input.nextDouble();
double x1,y1,x2,y2,x3,y3,x4,y4,x5,y5;
double alpha,angle2,angle3,angle4,angle5;
int n=5;
//随机生成一个初始点
alpha=Math.random()*(2*Math.PI);
x1=r*Math.cos(alpha);
y1=r*Math.sin(alpha);
//角度一次加72度即可得到其余四点
angle2=alpha+Math.PI/n;
x2=r*Math.cos(angle2);
y2=r*Math.sin(angle2);
angle3=angle2+Math.PI/n;
x3=r*Math.cos(angle3);
y3=r*Math.sin(angle3);
angle4=angle3+Math.PI/n;
x4=r*Math.cos(angle4);
y4=r*Math.sin(angle4);
angle5=angle4+Math.PI/n;
x5=r*Math.cos(angle5);
y5=r*Math.sin(angle5);
System.out.println("The coordinates of five points on the pentagon are:\n"+x1+" "+y1+"\n"+x2+" "+y2+"\n"+x3+" "+y3+"\n"+x4+" "+y4+"\n"+x5+" "+y5);
}
}
//代码2
import java.util.Scanner;
public class test8 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("Enter numer of sides:");
int n=input.nextInt();
System.out.println("Enter the radius of the bounding circle:");
double r=input.nextDouble();
double x0,y0,x,y;
double alpha;
System.out.println("The coordinates of five points on the pentagon are:\\n");
//随机生成一个初始点
alpha=Math.random()*(2*Math.PI);
x0=r*Math.cos(alpha);
y0=r*Math.sin(alpha);
System.out.println(x0+" "+y0);
for(int i=1;i<n;i++)
{
alpha=alpha+Math.PI/n;
x=r*Math.cos(alpha);
y=r*Math.sin(alpha);
System.out.println(+x+" "+y);
}
}
}
给出(ASCII码对应的字符)编写一个程序,得到一个ASCII码的输入(0~127之间的一个整数),然后显示该字符
import java.util.Scanner;
public class test9 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter an ASCII code:");
int number=input.nextInt();
char numchar=(char)number;
System.out.print("The character for ASCII code "+number+" is "+numchar);
}
}
(给出字符的Unicode码)编写一个程序,得到一个字符的输入,然后显示其Unicode值。
import java.util.Scanner;
public class test10 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter a character:");
Scanner input=new Scanner(System.in);
String string=input.next();
char char1=string.charAt(0);
int number=(int)char1;
System.out.println("The Unicode for the character "+char1+" is "+number);
}
}
猜测生日,用户输入字符Y代表‘是’,输入N代表‘不是’,代替之前输入的1和0
import java.util.Scanner;
public class test11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
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;
//Create a Scanner
Scanner input = new Scanner(System.in);
//prompt the user to anser questions
System.out.println("Is your birthday in Set1?\n");
System.out.println(set1);
System.out.println("\n Enter N for No and Y for Yes:");
String answer = input.next();
char answer1=answer.charAt(0);
if(answer1 == 'Y')
day+=1;
//prompt the user to anser questions
System.out.println("Is your birthday in Set2?\n");
System.out.println(set2);
System.out.println("\n Enter N for No and Y for Yes:");
answer = input.next();
answer1=answer.charAt(0);
if(answer1 == 'Y')
day+=2;
//prompt the user to anser questions
System.out.println("Is your birthday in Set3?\n");
System.out.println(set3);
System.out.println("\n Enter N for No and Y for Yes:");
answer = input.next();
answer1=answer.charAt(0);
if(answer1 == 'Y')
day+=4;
//prompt the user to anser questions
System.out.println("Is your birthday in Set4?\n");
System.out.println(set4);
System.out.println("\n Enter N for No and Y for Yes:");
answer = input.next();
answer1=answer.charAt(0);
if(answer1 == 'Y')
day+=8;
//prompt the user to anser questions
System.out.println("Is your birthday in Set5?\n");
System.out.println(set5);
System.out.println("\n Enter N for No and Y for Yes:");
answer = input.next();
answer1=answer.charAt(0);
if(answer1 == 'Y')
day+=16;
System.out.println("\nYour birthday is:"+day+"!");
}
}
编写一个程序,提示用户输入一个十六进制数,显示其对应的二进制数。
import java.util.Scanner;
public class test12 {
public static void main(String[] args) {
// TODO Auto-generated method stub
char ch;
String hexString;
int value,a,b,c,d;
Scanner input=new Scanner(System.in);
System.out.print("Enter a hex digit:");
hexString = input.next();
ch=hexString.charAt(0);
if(ch>='A' && ch<='F')
{
value=ch-'A'+10;
System.out.println("The binary value is "+Integer.toBinaryString(value));
}
else if(ch>='0' && ch<='9')
{
value=ch-'0';
System.out.println("The binary value is "+Integer.toBinaryString(value));
}
else
System.out.println(ch+" is an invalid input");
}
}
电话上的国际标准字母/数字映射
import java.util.Scanner;
public class test13 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String string1;
char ch;
int value,number;
Scanner input=new Scanner(System.in);
System.out.println("Enter a letter:");
string1=input.next();
if(string1.length()!=1)
{
System.exit(1);
}
else {
ch=string1.charAt(0);
if(ch>='a' && ch<='z')
{
value=ch-'a';
if(value<='m'-'a')
{
number=value/3+2;
}
else if(value>='p'-'a' && value<='s'-'a')
number=7;
else if(value>='t'-'a' && value<='v'-'a')
number=8;
else
number=9;
System.out.println("The corresponding number is "+number);
}
else if(ch>='A' &&ch<='Z')
{
value=ch-'A';
if(value<='M'-'A')
{
number=value/3+2;
}
else if(value>='P'-'A' && value<='S'-'A')
number=7;
else if(value>='T'-'A' && value<='V'-'A')
number=8;
else
number=9;
System.out.println("The corresponding number is "+number);
}
else
System.out.println("Enter a letter:"+ch+" is an invalid input");
}
}
}
编写一个程序,提示用户输入一个年份和一个月份名称的前三个字母(第一个字母使用大写形式),显示该月中的天数。
import java.util.Scanner;
public class test14 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int year,days=0;
String string1;
Scanner input=new Scanner(System.in);
System.out.println("Enter a year:");
year = input.nextInt();
System.out.println("Enter a month:");
string1=input.next();
if(string1.equals("Jan") || string1.equals("March")||string1.equals("May")||string1.equals("Jul")||string1.equals("Aug")||string1.equals("Oct")||string1.equals("Dec"))
days=31;
else if(string1.equals("Apr")||string1.equals("Jun")||string1.equals("Sep")||string1.equals("Nov"))
days=30;
else if(string1.equals("Feb"))
if((year%4==0 && year%100!=0)||(year%400==0))
days=29;
else
days=28;
else
{
System.out.println("Enter a error");
System.exit(0);
}
System.out.println(days);
}
}
编写一个程序,提示用户输入两个字符,显示这两个字符代表的专业以及状况。第一个字符表示专业,第二个字符表示年级。
import java.util.Scanner;
public class test15 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String string1,string2="00",string3="00";
char ch1,ch2;
Scanner input=new Scanner(System.in);
System.out.println("Enter two characters:");
string1=input.next();
ch1=string1.charAt(0);
ch2=string1.charAt(1);
if(ch1=='M')
string2="Mathematics";
else if(ch1=='C')
string2="Computer Science";
else if(ch1=='I')
string2="Information technology";
else
{
System.out.print("Invalid input");
System.exit(1);
}
if(ch2=='1')
string3="Freshman";
else if(ch2=='2')
string3="Sophomore";
else if(ch2=='3')
string3="Junior";
else if(ch2=='4')
string3="Senior";
else
{
System.out.println("Ener error");
System.exit(1);
}
System.out.println(string2+" "+string3);
}
}
编写一个程序,提示用户输入一个社保号码,它的格式是DDD-DD-DDDD,其中D是一个数字。你的程序应该判断输入是否合法
import java.util.Scanner;
public class test16 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String string,string1,string2,string3;
char ch1,ch2;
int a,b,c;
Scanner input=new Scanner(System.in);
System.out.println("Enter a SSN:");
string=input.next();
if(string.length()!=11)
System.out.println(string+" isn't an invalid social securit number");
else
{
string1=string.substring(0,2);
a=Integer.parseInt(string1); //数字字符串变为数值
ch1=string.charAt(3);
string2=string.substring(4,5);
b=Integer.parseInt(string2); //数字字符串变为数值
ch2=string.charAt(6);
string3=string.substring(7,11); //数字字符串变为数值
c=Integer.parseInt(string3);
if(a>0&&a<1000&&b>0&&b<100&&c>0&&c<10000&&ch1=='-'&&ch2=='-')
System.out.println(string+" is a valid social security number");
else
System.out.println(string+" isn't an invalid social securit number");
}
}
}
import java.util.Scanner;
public class test17 {
public static void main(String[] args) {
String employeeName;
double hoursWorked,hourlyPayRate,federalTaxRate,stateTaxRate;
Scanner input = new Scanner(System.in);
System.out.print("Enter employee's name: ");
employeeName = input.nextLine();
System.out.print("Enter number of hours worked in a week: ");
hoursWorked = input.nextDouble();
System.out.print("Enter hourly pay rate: ");
hourlyPayRate = input.nextDouble();
System.out.print("Enter federal tax withholding rate: ");
federalTaxRate = input.nextDouble();
System.out.print("Enter state tax withholding rate: ");
stateTaxRate = input.nextDouble();
System.out.printf("Employee Name: %s\n", employeeName);
System.out.printf("Hours Worked: %.1f\n", hoursWorked);
System.out.printf("Pay Rate: %.2f\n", hourlyPayRate);
System.out.printf("Gross Pay: %.2f\n", hoursWorked * hourlyPayRate);
System.out.printf("Deductions:\n");
System.out.printf(" Federal Withholding (%.1f%%): $%.2f\n", federalTaxRate * 100, federalTaxRate * hoursWorked * hourlyPayRate);
System.out.printf(" State Withholding (%.1f%%): $%.2f\n", stateTaxRate * 100, stateTaxRate * hoursWorked * hourlyPayRate);
System.out.printf(" Total Deduction: $%.2f\n", federalTaxRate * hoursWorked * hourlyPayRate
+ stateTaxRate * hoursWorked * hourlyPayRate);
System.out.printf("Net pay: $%.2f\n", hoursWorked * hourlyPayRate
- federalTaxRate * hoursWorked * hourlyPayRate
- stateTaxRate * hoursWorked * hourlyPayRate);
input.close();
}
}
假设一个车牌号码由三个大写字母和后面的四个数字组成。编写一个程序,生成一个车牌号码。
public class test18 {
public static void main(String[] args) {
// TODO Auto-generated method stub
char a,b,c;
int d;
a=(char)(Math.random()*25+65);
b=(char)(Math.random()*25+65);
c=(char)(Math.random()*25+65);
d=(int) (Math.random()*10+Math.random()*100+Math.random()*1000+Math.random()*10000);
System.out.println(""+a+b+c+d);
}
}
原文链接:https://blog.csdn.net/mynameisgt/article/details/109742798
作者:java小王子
链接:http://www.javaheidong.com/blog/article/928/2055520384039c5e7b34/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!