《Java程序设计教程DevCloud》第二章练习答案
【示例2】给变量赋值的演示。
package javaone.programming.devcloud.chaptertwo;
public class ChapterTwoExampleTwo {
public static void main(String[] args) {
int a, b;
a = 1;
System.out.println("a="+a);
a = a + 1;
System.out.println("a="+a);
b = a + 1;
System.out.println("b="+b);
a = b + 2;
System.out.println("a="+a);
}
}
【示例3】变量初始化的演示。
package javaone.programming.devcloud.chaptertwo;
public class ChapterTwoExampleThree {
public static void main(String[] args) {
byte a = 0x78;
int b = 3;
float c = 1.23f;
double d = 3.23;
boolean e = false;
char f = 'A';
System.out.println("convert to decimal: a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
System.out.println("d="+d);
System.out.println("e="+e);
System.out.println("f="+f);
}
}
【示例4】变量的作用域演示。
package javaone.programming.devcloud.chaptertwo;
public class ChapterTwoExampleFour {
static int x=5;
public static void main(String[] args) {
int y=23;
System.out.println("x="+x);
System.out.println("y="+y);
m(6);
}
static void m(int z){
System.out.println("z="+z);
System.out.println("x="+x);
}
}
【示例5】算术运算。
package javaone.programming.devcloud.chaptertwo;
public class ChapterTwoExampleFive {
public static void main(String[] args) {
int a=3,b=5,c=5;
System.out.println("initial value:a=3,b=5,c=5");
System.out.println("b/a="+b/a);
System.out.println("c%a="+c%a);
System.out.println("-a="+(-a));
System.out.println("b++="+(b++)+", "+"b="+b);
System.out.println("++c="+(++c)+", "+"c="+c);
}
}
【示例6】逻辑运算。
package javaone.programming.devcloud.chaptertwo;
public class ChapterTwoExampleSix {
public static void main(String[] args) {
int x=1,y=2;
int numOne=5,numTwo=5,numThree=5,numFour=5;
boolean rOne,rTwo,rThree,rFour;
rOne=x>y & numOne++==5;
rTwo=x>y && numTwo++==5;
rThree=x<y | numThree++==5;
rFour=x<y || numFour++==5;
System.out.println("logical value: rOne="+rOne+", numOne="+numOne);
System.out.println("logical value: rTwo="+rTwo+", numTwo="+numTwo);
System.out.println("logical value: rThree="+rThree+", numThree="+numThree);
System.out.println("logical value: rFour="+rFour+", numFour="+numFour);
}
}
【示例7】比较3个数的大小,求出其中的最大值和最小值。
package javaone.programming.devcloud.chaptertwo;
public class ChapterTwoExampleSeven {
public static void main(String[] args) {
int a=12,b=-23,c=34;
int max,min,t;
t=a>b?a:b;
max=t>c?t:c;
t=a<b?a:b;
min=t<c?t:c;
System.out.println("max="+max);
System.out.println("min="+min);
}
}
课后习题
1. 编写一个程序,从控制台随机输入3个数,实现从小到大的排列顺序。
package javaone.programming.devcloud.chaptertwo;
import java.util.Arrays;
import java.util.Scanner;
public class ChapterTwoExerciseOne {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter three numbers: ");
double[] numbers = new double[3];
for (int i = 0; i < 3; i++) {
numbers[i] = scanner.nextDouble();
}
Arrays.sort(numbers);
System.out.println("sort numbers:");
for (int i = 0; i < 3; i++) {
System.out.print(numbers[i] + " ");
}
scanner.close();
}
}
2. 某银行提供了整存整取定期储蓄业务,存期分别为一年、两年、三年、五年。存期为一年,年利率为2.26%;存期为两年,年利率为2.7%;存期为三年,年利率为3.3%;存期为五年,年利率为3.6%。编写一个程序,输入存入的本金,计算并输出存一年、两年、三年、五年,到期取款时,银行应支付的本金和利息。
package javaone.programming.devcloud.chaptertwo;
import java.util.Scanner;
public class ChapterTwoExerciseTwo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the deposit amount: ");
double depositAmount = scanner.nextDouble();
double rateOneYear = 2.26/100;
double rateTwoYear = 2.7/100;
double rateThreeYear = 3.3/100;
double rateFiveYear = 3.6/100;
double totalOneYear = depositAmount * (1 + rateOneYear);
double totalTwoYear = depositAmount * Math.pow (1 + rateTwoYear, 2);
double totalThreeYear = depositAmount * Math.pow (1 + rateThreeYear, 3);
double totalFiveYear = depositAmount * Math.pow (1 + rateFiveYear, 5);
System.out.printf("The total amount is : %.2f\n", totalOneYear);
System.out.printf("The total amount is : %.2f\n", totalTwoYear);
System.out.printf("The total amount is : %.2f\n", totalThreeYear);
System.out.printf("The total amount is : %.2f\n", totalFiveYear);
scanner.close();
}
}
3. 公司为员工提供了基本工资、房租津贴和餐补。其中,房租津贴为基本工资的20%,餐补为基本工资的30%。要求从控制台输入基本工资后,计算并输出实领工资。
package javaone.programming.devcloud.chaptertwo;
import java.util.Scanner;
public class ChapterTwoExerciseThree {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the basic salary: ");
double basicSalary = scanner.nextDouble();
double rentAllowance = basicSalary * 0.2;
double foodAllowance = basicSalary * 0.3;
double totalSalary = basicSalary + rentAllowance + foodAllowance;
System.out.printf("RentAllowance: %.2f\n", rentAllowance);
System.out.printf("FoodAllowance: %.2f\n", foodAllowance);
System.out.printf("Total salary: %.2f\n", totalSalary);
scanner.close();
}
}
原文地址:https://blog.csdn.net/weixin_53759045/article/details/144453639
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!