首页 > 代码库 > java语言的科学与艺术 第六章 课后编程
java语言的科学与艺术 第六章 课后编程
欢迎大家转载,为保留作者成果,转载请注明出处,http://blog.csdn.net/netluoriver,有些文件在资源中也可以下载!如果你没有积分,可以联系我索要!
1、
package SixthCharter; /* * File: Poker.java * --------------------------- * 这是第6章的第一题 * Author luoriver */ import acm.program.*; import acm.util.*; public class Poker extends ConsoleProgram { public void run(){ println("这个程序是随机显示选择的扑克"); while(true){ println(); int i = readInt(); showPoker(); if (i==2) break; } } /* * 显示随机的扑克牌 */ private void showPoker(){ int num = rgen.nextInt(1,13); // rgen.setSeed(2); switch(num){ case 1: print("Ace"); break; case 11: print("Jack"); break; case 12: print("Queen"); break; case 13: print("King"); break; default: print(num); } print(" of "); int color = rgen.nextInt(1,4); switch(color){ case 1: print ("Spades"); break; case 2: print("Diamonds"); break; case 3: print("Clubs"); break; case 4: print("Hearts"); break; } } /*create an instance variable for the random number generator */ private RandomGenerator rgen =RandomGenerator.getInstance(); }
2、
package SixthCharter; /* * File: TossCoin.java * --------------------------- * 这是第6章的第2题 * Author luoriver */ import acm.program.*; import acm.util.*; public class TossCoin extends ConsoleProgram { public void run(){ println("这个程序是抛硬币,一直抛到出现3个连续的下面为止,并显示被抛的总次数:"); int toss_num = 0; //设置检查点,是否到了连续的次数 int n = 0; while(n < CHECK_NUM){ tossCoin(); toss_num ++; if(tossCoin().equals("Head")){ println("Head"); n++; }else{ println("Tails"); n = 0; } } println("一共运行了" + toss_num + "次。"); } /** * 这里是进行的投币方法 */ private String tossCoin(){ int n = rgen.nextInt(1, 2); return returnContent(n); } /** * 这里是显示返回硬币的哪一面 * @Head * @Tails */ private String returnContent(int num){ if(num == 1){ return "Head"; }else{ return "Tails"; } } /*create an instance variable for the random number generator */ private RandomGenerator rgen =RandomGenerator.getInstance(); private static final int CHECK_NUM = 3; }
3、
package SixthCharter; /* * File: ThrowDart.java * --------------------------- * 这是第6章的第3题 * Author luoriver */ import acm.program.*; import acm.graphics.*; import acm.util.*; public class ThrowDart extends ConsoleProgram { public void run(){ println("这个程序来用投镖来模拟正方形与圆形的面积之比是一个近似的 PI值:"); double SquareArea = 4 * (R * R); double CircleArea = PI * (R * R); println("圆形与正方形的面积之比为" + CircleArea / SquareArea); println("投镖时落在圆形与正方形内的次数之比为" + throwDart()); //println(3.14/4); //throwDart(); } private double throwDart(){ double circlenum = 0; double squarenum = 0; while(squarenum < ThrowNum){ double rx = rgen.nextDouble(-1.0, 1.0); double ry = rgen.nextDouble(-1.0, 1.0); // rgen.setSeed(2); if(((rx * rx) + (ry * ry)) < 1){ circlenum ++; squarenum ++; }else{ squarenum ++; } } println(); println("投在圆形内的次数为" + circlenum); println("投在正方形内的次数为" +squarenum); return circlenum / squarenum; } /*create an instance variable for the random number generator */ private RandomGenerator rgen =RandomGenerator.getInstance(); //设置投镖的次数 private static final int ThrowNum = 10000; private static final double PI = 3.14; private static final double R= 1; }
4、
package SixthCharter; /* * File: Poker.java * --------------------------- * 这是第6章的第一题 * Author luoriver */ import acm.program.*; import acm.util.*; public class HalfLife extends ConsoleProgram { public void run(){ println("这个程序是模拟原子的半衰期"); println("There are " + RADIOACTIVE_ACTOM_NUM + "actom initially"); int left_num = RADIOACTIVE_ACTOM_NUM; int year = 0; while(left_num > 0){ int new_actom_num = left_halflife_actom_num(left_num); left_num -= new_actom_num; year ++; println("There are " + left_num + " actom at the end of year" + year); new_actom_num = 0; } } //定义衰减原子的数量 private int left_halflife_actom_num(int num){ int actom_num = 0; for(int i = num; i > 0; i -- ){ while(rgen.nextBoolean()){ actom_num ++; break; } } return actom_num; } /*create an instance variable for the random number generator */ private RandomGenerator rgen =RandomGenerator.getInstance(); /** * 定义放射性原子的数量 * @RADIOACTIVE_ACTOM_NUM */ private static final int RADIOACTIVE_ACTOM_NUM = 10000; }
5、
package SixthCharter; /* * File: Poker.java * --------------------------- * 这是第6章的第5题 * Author luoriver */ import acm.program.*; import java.util.Scanner; import acm.util.*; public class Bingo extends ConsoleProgram { public void run() { /* * 首先询问是否要介绍下赌博规则,这里以后再写 读取文字不会先用数字代替吧,1代表继续,2代表不玩了 */ println(); int know_rules = readInt("你想了解赌博机的规则吗?1代表为想了解,2代表已经知道规则:"); if (know_rules == 1) { println(/*"内容太多,我没有写呢,看看Java语言的科学与艺术第172页吧"*/ RULES); } println("开始玩吧!祝玩的愉快!"); start_play(); } private void start_play(){ int remain_money = CLIENT_INIT_MONEY; print_remain_money(remain_money); while(remain_money > 0){ remain_money --; if(remain_money == 0){ println(""); } String first_show = show_result(); // print("111111" + first_show); String second_show = show_result(); // println("22222" + first_show); String third_show = show_result(); // print("333333333" + first_show); /*Scanner num = new Scanner(System.in); int n = num.nextInt();*/ int n = readInt(); if(n == 2) break; // print_remain_money(remain_money); if(first_show.equals("BAR") && second_show.equals("BAR") && third_show.equals("BAR")){ remain_money += 250; println(first_show + " " + second_show + " " + third_show +" you win" ); print_remain_money(remain_money); }else if(first_show.equals("BELL") && second_show.equals("BELL") && (third_show.equals("BELL") || third_show.equals("BAR"))){ remain_money += 20; println(first_show + " " + second_show + " " + third_show +" you win" ); print_remain_money(remain_money); }else if(first_show.equals("PLUM") && second_show.equals("PLUM") && (third_show.equals("PLUM") || third_show.equals("BAR"))){ remain_money += 14; println(first_show + " " + second_show + " " + third_show +" you win" ); print_remain_money(remain_money); }else if(first_show.equals("ORANGE") && second_show.equals("ORANGE") && (third_show.equals("ORANGE") || third_show.equals("BAR"))){ remain_money += 10; println(first_show + " " + second_show + " " + third_show +" you win" ); print_remain_money(remain_money); }else if(first_show.equals("CHERY") && second_show.equals("CHERY") && third_show.equals("CHERY")){ remain_money += 7; println(first_show + " " + second_show + " " + third_show +" you win" ); print_remain_money(remain_money); }else if(first_show.equals("CHERY") && second_show.equals("CHERY")){ remain_money += 5; println(first_show + " " + second_show + " " + third_show +" you win" ); print_remain_money(remain_money); }else if(first_show.equals("CHERY")){ remain_money += 2; println(first_show + " " + second_show + " " + third_show +" you win" ); print_remain_money(remain_money); }else{ println(first_show + " " + second_show + " " + third_show +" you loss" ); print_remain_money(remain_money); } } // print_remain_money(remain_money); } private void print_remain_money(int remain){ println("you have $" + remain + " would you like to play?"); } private String show_result() { int random_show = rgen.nextInt(1, 6); switch (random_show) { case 1: return "BAR"; case 2: return "LEMON"; case 3: return "ORANGE"; case 4: return "PLUM"; case 5: return "BELL"; default: return "CHERRY"; } } /* * 设定系统给用户初始钱 */ private static final int CLIENT_INIT_MONEY = 50; /* * create an instance variable for the random number generator */ private RandomGenerator rgen = RandomGenerator.getInstance(); private static final String RULES= "典型的赌博机有3个轮子,他们在一个狭窄的窗口后面旋转。每个轮子都标记为CHERRY、LEMON、ORANGE、PLUM、BELL和BAR。然而,窗口每次只允许看到一个轮子的一个符号。" +"下面显示的是典型的获胜模式及其相关回报:" +"BAR BAR BAR -- 支付 $250" + "BELL BELL BELL/BAR -- 支付 $20" + "PLUM PLUM PLUM/BAR -- 支付 $14" + "ORANGE - ORANGE - ORANGE/BAR -- 支付 $10" + "CHERRY - CHERRY - CHERRY -- 支付 $7" + "CHERRY - CHERRY - -- -- 支付 $5" + "CHERRY - CHERRY - -- -- 支付 $2"; };
6、7合并了
package SixthCharter; /* * File: Poker.java * --------------------------- * 这是第6章的第5题 * Author luoriver */ import acm.program.*; import java.util.Scanner; import acm.util.*; public class Calculate extends ConsoleProgram { public void run() { int first = rgen.nextInt(0, 20); int second = rgen.nextInt(0, 20); sys_question(first, second); } /* * 定义系统提出问题的方法 */ private void sys_question(int first, int second) { for (int i = 0; i < PROBLEM_NUM; i++) { first = rgen.nextInt(0, 20); second = rgen.nextInt(0, 20); int sys_answer = first + second; if (sys_answer <= 20) { print(first + " + " + second + " = "); answer(sys_answer); } else { i--; } } } /* * 定义学生回答的方法 */ private void answer(int sys_answer) { int student_answer = readInt(); if (student_answer == sys_answer) { println(success()); } else { for (int i = 0; i < ANSWER_NUM; i++) { println("答案不正确,请再输入一遍"); student_answer = readInt(); } println("正确答案是" + sys_answer); } } /* * 显示输入正确答案后的鼓励信息 */ private String success() { int n = rgen.nextInt(1, 3); switch (n) { case 1: return "You got it"; case 2: return "connect"; default: return "That's the answer"; } } /* * create an instance variable for the random number generator */ private RandomGenerator rgen = RandomGenerator.getInstance(); /* * 定义出题的个数 */ private static final int PROBLEM_NUM = 5; /* * 定义学生错误回答问题的次数 */ private static final int ANSWER_NUM = 3; }
8、
package SixthCharter; /* * File: Poker.java * --------------------------- * 这是第6章的第9题 * Author luoriver */ import acm.program.*; import acm.util.*; public class NewPoker extends ConsoleProgram { public void run(){ println("这个程序是随机显示选择的扑克"); int level = level(); String color = color(); Card show = new Card(level, color); println(); println("直接显示show类为" + show); println(); print(show.toString(level, color)); } public class Card{ public Card (int level, String color){ println("等级是" + level + "花色是" + color); } public String toString(int level, String color){ return "toString 方法显示等级为" + level + "花色是" + color; } } public int getLevel(){ return level(); } public String getColor(){ return color(); } /* * 显示随机的扑克牌 */ private void showPoker(){ int num = rgen.nextInt(1,13); // rgen.setSeed(2); switch(num){ case 1: print("Ace"); break; case 11: print("Jack"); break; case 12: print("Queen"); break; case 13: print("King"); break; default: print(num); } } private String color(){ int color = rgen.nextInt(1,4); switch(color){ case 1: // print("Spades"); return "Spades"; case 2: // print("Diamonds"); return "Diamonds"; case 3: // print("Clubs"); return "Clubs"; default: // print("Hearts"); return "Hearts"; } } private int level(){ int n = rgen.nextInt(1, 4); switch(n){ case 1: return LEVEL1; case 2: return LEVEL2; case 3: return LEVEL3; default: return LEVEL4; } /*private void color(){ int color = rgen.nextInt(1,4); switch(color){ case 1: print("Spades"); case 2: print("Diamonds"); case 3: print("Clubs"); case 4: print("Hearts"); } */ } /*create an instance variable for the random number generator */ private RandomGenerator rgen =RandomGenerator.getInstance(); /* * 定义扑克的花色常量 */ private static final String CLUBS = "CLUBS"; private static final String DIMONDS = "DIMONDS"; private static final String HEARTS = "HEARTS"; private static final String SPADES = "SPADES"; /* * 定义等级常量的值 */ private static final int LEVEL1 = 1; private static final int LEVEL2 = 11; private static final int LEVEL3 = 12; private static final int LEVEL4 = 13; }
9、
package SixthCharter; public class LibaryRecord { public LibaryRecord(String book_name, String author, int catalog_number, String publisher, int public_date, boolean isCurrent){ this.book_name = book_name; this.author = author; this.catalog_number = catalog_number; this.publisher = publisher; this.public_date = public_date; this.isCurrent = isCurrent; } public LibaryRecord(String book_name, String author, int catalog_number, String publisher, int public_date){ this.book_name = book_name; this.author = author; this.catalog_number = catalog_number; this.publisher = publisher; this.public_date = public_date; isCurrent = true; } public String getBookName(){ return book_name; } public String getAuthor(){ return author; } public int getCatalogNum(){ return catalog_number; } public String getPublisher(){ return publisher; } public int getPublic_date(){ return public_date; } public boolean getisCurrent(){ return isCurrent; } public void setisCurrent(boolean isCurrent){ this.isCurrent = isCurrent; } @Override public String toString(){ return "Book_name" + book_name + " author: " + author + " 目录号: " + catalog_number + "出版者" + publisher + " 出版年份: " + public_date + " 当前是否流通: " + isCurrent; } /* * 定义书名 */ private String book_name; private String author; private int catalog_number; private String publisher; private int public_date; private boolean isCurrent; }
10、没有做
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。