尚学堂java答案解析 第二章

时间:2022-12-27 16:50:13

本答案为本人个人编辑,仅供参考,如果读者发现,请私信本人或在下方评论,提醒本人修改

一.选择题:

1.CD

解析:A public是关键字. B 第一个不能是数字

2.C

解析:j=i++  <=> j=i; i++

3.A

解析:float数据需要在尾部加f,且f不能省略

4.AB

解析:C 字符串必须是双引号 D数据类型错误

5.AB

解析:A的赋值运算是最低的14级,B的题目符号错误符号,应该是 ?: ,位于13级

二.简答题

1.bool  byte->short->char->int->long->float->double

2. j=i++ <=> j=i; i++;     j=++i  <=> i++;  j=i;

3.||是逻辑或运算符,是两个bool值的或运算;,结果是bool型  .  |是位运算,是两个数据的按二进制展开后每位进行或运算,结果是一个数

4.byte->short->char->int->long->float->double

三.编码题

1.

import java.util.Scanner;

public  class Circle {

    public static void main(String[] args) {
float radius=0.0f ;
float circumference=0.0f;
float area=0.0f; Scanner input = new Scanner(System.in);
Result result = new Result(); System.out.println("请输入圆的半径:");
radius=input.nextFloat();
circumference=result.circumference(radius);
area=result.area(radius); System.out.printf("该圆的半径为: R=%2.1f\n",radius);
System.out.printf("该圆的周长为: C=2*3.14*%2.1f=%2.1f\n",radius,circumference);
System.out.printf("该圆的面积为: S=3.14*%2.1f*%2.1f=%3.3f\n",radius,radius,area);
}
}
class Result{ final float PI=3.14f; public float circumference(float radius){ return 2*PI*radius;
}
public float area(float radius){ return PI*radius*radius;
} }

2.

import java.util.Currency;

public class InterestRate {
public static void main(String[] args) {
int principal = 10000; FinalValue finalValue = new FinalValue() ; System.out.printf("本金:%d\n",principal);
System.out.printf("活期一年本金总计:%d\n",(int)finalValue.CurrentDeposit_12(principal));
System.out.printf("定期一年本金总计:%d\n",(int)finalValue.FixedDeposit_12(principal));
System.out.printf("活期两年本金总计:%d\n",(int)finalValue.CurrentDeposit_24(principal));
System.out.printf("定期两年本金总计:%d\n",(int)finalValue.FixedDeposit_24(principal)); }
}
class FinalValue{
float currentDeposit=0.35f;
float fixedDeposit_3=1.10f;
float fixedDeposit_6=1.30f;
float fixedDeposit_12=1.50f;
float fixedDeposit_24=2.10f; float CurrentDeposit_12(int principal){
return principal*currentDeposit/100f+principal;
}
float CurrentDeposit_24(float principal){
return principal*currentDeposit*2/100f+principal;
} float FixedDeposit_12(float principal){
return principal*fixedDeposit_12/100f+principal;
}
float FixedDeposit_24(float principal){
return 2*principal*fixedDeposit_24/100f+principal;
}
}

3.

import java.util.Scanner;

public class EnCode {
public static void main(String[] args) {
int plaintext = 0;
int ciphertext = 0;
int temp=1000;
int num=0;
int bit[] = new int[4]; Scanner input = new Scanner(System.in); System.out.print("请输入4位正整数:");
plaintext = input.nextInt(); for(int i=0; temp>=1;temp=temp/10,i++){
bit[i]=(int)plaintext/temp;
num= bit[i];
System.out.printf("\tbit["+i+"]="+bit[i]);
bit[i]=(bit[i]+5)%10;
System.out.printf("\tbit["+i+"]="+bit[i]);
plaintext=plaintext-num*temp;
System.out.println(); }
for(int i=0;i<2;i++){
temp=bit[i];
bit[i]=bit[bit.length-1-i];
bit[bit.length-1-i]=temp; }
System.out.print("\n密文:");
temp=1000;
for(int i=0; temp>=1;temp=temp/10,i++) { ciphertext=bit[i]*temp+ciphertext; }
System.out.println(ciphertext);
}
}