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

6.9英尺和米之间的转换 public class Meters {

public static void main(String[] args) {

System.out.println(\英尺\\t\\t米\\t\\t\\t\\t米\\t\\t英尺\ for (int i=1,j=20;i<11;i++,j+=5){

System.out.println(i+\ } }

public static double footToMeters(double foot){ double meter=0.305*foot; return meter; }

public static double meterToFoot(double meter){ double foot=3.279*meter; return foot; } }

6.14估算π public class Pi {

public static void main(String[] args) { mathToPi(1); }

public static void mathToPi(int j){ System.out.println(\ for (;j<1000;j+=100){ double sum=0; double m;

for (int i=1;i<=j;i++){

sum=sum+Math.pow(-1,i+1)/(2*i-1); }

m=4*sum;

System.out.println(j+\ } } }

6.18检测密码 public class Pass {

public static void main(String[] args) { Scanner in=new Scanner(System.in); System.out.print(\请输入密码:\ String str=in.next(); password(str); }

public static void password(String str){

boolean a=str.matches(\-Za-z0-9]{8,}\ int num=0;

for (int i=0;i

if (ch>= '0' &&ch<= '9') {num++;} }

if (a&&num>2){ System.out.println(\密码正确\ }else System.out.print(\密码错误\ } }

6.30游戏:双骰子游戏 public class Ace {

public static void main(String[] args) { int sum=random(); rusult(sum); }

public static int random(){

int a=(int)(Math.random()*6+1); int b=(int)(Math.random()*6+1); int sum=a+b;

System.out.println(a+\ return sum; }

public static void rusult(int sum){

if (sum==2||sum==3||sum==12){ System.out.println(\你输了\ }else if (sum==7||sum==11){

System.out.println(\你赢了\ }else compare(sum); }

public static void compare(int sum){ int a=sum; int b=0;

System.out.println(\确定的点数为:\开始继续掷骰子\ while (a!=b||b!=7){ b=random(); if (a==b){

System.out.println(\你赢了\ break; }else if (b==7){

System.out.println(\你输了\ break; } } } }