Java语言程序设计与数据结构(基础篇)课后练习题 第四章(一)

【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

**开源地址:https://docs.qq.com/doc/DSmxTbFJ1cmN1R2dB **

System.out.println("The area of the hexagon is "+a);

}

}

4.5

================================================================

import java.util.Scanner;

public class disizhang {

public static void main(String[] args){

final double PI = 3.141592654;

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 a = nss/(4*Math.tan(PI/n));

System.out.println("The area of the polygon is "+a);

}

}

4.6

================================================================

public class disizhang {

public static void main(String[] args){

final double PI = 3.141592654;

double alpha1 = Math.random()2PI;

double alpha2 = Math.random()2PI;

double alpha3 = Math.random()2PI;

double x1 = 40*Math.cos(alpha1);

double y1 = 40*Math.sin(alpha1);

double x2 = 40*Math.cos(alpha2);

double y2 = 40*Math.sin(alpha2);

double x3 = 40*Math.cos(alpha3);

double y3 = 40*Math.sin(alpha3);

double a = Math.sqrt((x1-x2)(x1-x2)+(y1-y2)(y1-y2));

double b = Math.sqrt((x2-x3)(x2-x3)+(y2-y3)(y2-y3));

double c = Math.sqrt((x1-x3)(x1-x3)+(y1-y3)(y1-y3));

double aa = Math.toDegrees(Math.acos((aa-bb-cc)/(-2b*c)));

double ab = Math.toDegrees(Math.acos((bb-aa-cc)/(-2a*c)));

double ac = Math.toDegrees(Math.acos((cc-bb-aa)/(-2a*b)));

System.out.println("The three angles are: “+aa+” “+ab+” "+ac);

}

}

4.7

================================================================

import java.util.Scanner;

public class disizhang {

public static void main(String[] args){

final double PI = 3.141592654;

Scanner input = new Scanner(System.in);

System.out.print("Enter the radius of the bounding circle: ");

double r = input.nextDouble();

double base = 0.1*PI;

System.out.println(“The coordinates of fibe points on the pentagon are”);

for(int i=0;i<5;i++)

{

System.out.printf(“(%.2f, %.2f)%n”,rMath.cos(base),rMath.sin(base));

base+=0.4*PI;

}

}

}

4.8

================================================================

import java.util.Scanner;

public class disizhang {

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print("Ent6er an ASCII code: ");

int num = input.nextInt();

char str = (char)num;

System.out.printf(“The character for ASCII code %d is %c”,num,str);

}

}

4.9

================================================================

import java.util.Scanner;

public class disizhang {

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print("Enter a character: ");

String jb = input.nextLine();

char str = jb.charAt(0);

int str2 = (int)str;

System.out.printf(“The Unicode for the character %c is %d”,str,str2);

}

}

4.10

=================================================================

public class disizhang {

public static void main(String[] args)

{

//在原来代码中进行修改

int answer = input.nextInt()修改为:char answer = input.nextLine().charAt(0)

if(answer1)—>if(answer’Y’)

}

}

4.11

=================================================================

import java.util.Scanner;

public class disizhang {

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();

if(num>=0&&num<=15){

if(num<10)

System.out.printf(“The hex value is %d”,num);

else

System.out.printf(“The hex value is %c”,‘A’+num-10);

}else

System.out.printf(“%d is an invalid input”,num);

}

}

4.12

=================================================================

import java.util.Scanner;

public class disizhang {

public static void main(String[] args){

String[] bins = {“0”,“1”,“10”,“11”,“100”,“101”,“110”,“111”,“1000”,“1001”,“1010”,“1011”,“1100”,“1101”,“1110”,“1111”};

System.out.print("Enter a hex digit: ");

Scanner input = new Scanner(System.in);

char str = input.nextLine().charAt(0);

if(str>=‘0’&&str<=‘9’)

System.out.printf(“The binary value is %s\n”,bins[str-‘0’]);

else if(str>=‘A’&&str<=‘F’)

System.out.printf(“The binary value is %s\n”,bins[str-‘A’+10]);

else

System.out.printf(“%c is an invalid input\n”,str);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值