Java语言程序设计(基础篇)原书第十一版 梁勇 第5、6章 课后题答案 下载本文

}else if (n1==0){

if (n2==1){j++; System.out.println(\你输了!\ } else {i++; System.out.println(\你赢了!\ }else if(n1==1){

if (n2==0){i++; System.out.println(\你赢了!\ } else {j++; System.out.println(\你输了!\ }else {

if (n2==0){j++; System.out.println(\你输了!\ } else {i++; System.out.println(\你赢了!\ } } }

5.45计算平均值和标准方差 public class TongJi {

public static void main(String[] args) { int i=10;

double sum=0; double mean; double num=0; double standard;

double list[]=new double[i];

System.out.print(\请输入10个数:\ Scanner in=new Scanner(System.in); for (i=0;i<10;i++) {

list[i] = in.nextDouble(); sum += list[i];

num += Math.pow(list[i],2); }

mean=sum/10;

double a=num-Math.pow(sum,2)/i; standard=Math.pow(a/(i-1),0.5);

System.out.println(\平均值是:\ System.out.print(\方差是:\ } }

5.47商业:检测ISBN-13

public class Text {

public static void main(String[] args) { Scanner in=new Scanner(System.in); System.out.print(\请输入ISBN-13:\ String str=in.next(); int length=str.length();

if (length!=12) {System.out.println(\请输入正确的ISBN-13码\ char d1=str.charAt(0);char d2=str.charAt(1);char d3=str.charAt(2); char d4=str.charAt(3);char d5=str.charAt(4);char d6=str.charAt(5); char d7=str.charAt(6);char d8=str.charAt(7);char d9=str.charAt(8);

char d10=str.charAt(9);char d11=str.charAt(10);char d12=str.charAt(11); int d=10-(d1-48+(d2-48)*3+d3+(d4-48)*3+d5+(d6-48)*3

+d7+(d8-48)*3+d9+(d10-48)*3+d11+(d12-48)*3);

if (d == 10){ int d13=0;

System.out.print

(\-13是:\

} else {

System.out.print

(\-13是:\

} } }

6.7计算未来投资回报值 public class Money {

public static void main(String[] args) { Scanner in=new Scanner(System.in); System.out.print(\请输入本金:\

double investmentAmount=in.nextDouble(); System.out.print(\请输入利率:\

double monthlyInterestRate=in.nextDouble(); System.out.print(\请输入年份:\ int years=in.nextInt();

futureInvestementValue(investmentAmount,monthlyInterestRate,years); }

public static double futureInvestementValue(double investmentAmount,double monthlyInterestRate,int years){

System.out.println(\ for (int i=1;i

value=investmentAmount*Math.pow(1+monthlyInterestRate/1200,12); investmentAmount=value;

System.out.println(i+\ }

return 0; } }