第004天作业改正: Craps赌博游戏
import java.util.Scanner;
//lang - language java核心包 自动导入
// CoC - Convention over Configuration 约定优于配置
public class CrapsGame {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int money = 1000;
do {
System.out.println("你的总资产: " + money + "元.");
int bet;
do {
System.out.print("请下注: ");
bet = input.nextInt();
} while (bet <= 0 || bet > money);
// [min, max] - (int) (Math.random() * (max - min + 1)) + min);
// [min, max) - (int) (Math.random() * (max - min)) + min);
// (min, max) - (int) (Math.random() * (max - min)) + min + 1);
// [50, 80] (80 - 50) 50 上限-下限 下限
int face1 = (int) (Math.random() * 6 + 1);
int face2 = (int) (Math.random() * 6 + 1);
int firstPoint = face1 + face2;
System.out.println("玩家摇出了" + firstPoint + "点.");
boolean needsGoOn = false;
switch (firstPoint) {
case 7:
case 11:
money += bet;
System.out.println("玩家胜!");
break;
case 2:
case 3:
case 12:
money -=bet;
System.out.println("庄家胜!");
break;
default:
needsGoOn = true;
}
while (needsGoOn) {
face1 = (int) (Math.random() * 6 + 1);
face2 = (int) (Math.random() * 6 + 1);
int currentPoint = face1 + face2;
System.out.println("玩家摇出了" + currentPoint + "点.");
if (currentPoint == 7) {
money -= bet;
System.out.println("庄家胜!");
needsGoOn = false;
} else if (currentPoint == firstPoint) {
money += bet;
System.out.println("玩家胜!");
needsGoOn = false;
}
}
} while (money > 0);
System.out.println("你已经破产!");
input.close();
}
}
练习
- 1.输出如下:
1 1
2 4
3 9
... ...
10 100
for (int i = 1; i <= 100; i++) {
// \t 表示制表符 , 每列左对齐
System.out.println(i + "\t" + i * i);
// 不够x位 左边补 空格 右对齐
System.out.printf("%3d %5d\n", i, i * i);
// 补0
System.out.printf("%03d %05d\n", i, i * i);
}
- 2.输入一个正整数,判断是不是素数
提示:检查2~(n - 1)之间有没有n的因子
Scanner input = new Scanner(System.in);
System.out.print("请输入一个数: ");
int n = input.nextInt();
boolean isPrime = true; // 先假设n是素数
for (int i = 2; i <= n-1; ++i) {
if (n % i == 0) {
isPrime = false;
break;
}
}
System.out.println(n + (isPrime ? "是" : "不是") +"素数");
input.close();
- 3.把1~100之间的所有素数打印出来
public class Ex03 {
public static boolean isPrime(int i) {
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
for (int i = 1; i <= 100 ; i++) {
/*boolean isPrime = true;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println(i);
}*/
if (isPrime(i)) {
System.out.println(i);
}
}
}
}
- 4.打印乘法口诀表
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
//System.out.printf("%d * %d = %2d ", j, i, i * j);
System.out.printf("%d*%d=%d\t", j, i, i * j);
//System.out.print(j + "*" + i + "=" + j * i + "\t");
}
System.out.println("");
}
- 5.猴子吃桃子
第一天 吃所有桃子的一半 多吃一个
第二天 吃剩下桃子的一半 多吃一个
...
第十天 每吃 只有 1 个
算总共多少桃子
int total = 1;
for (int i = 9; i >= 1; i--) {
total = (total + 1) * 2;
}
System.out.println(total);
}
- 6.百钱百鸡
公鸡 5块一只 母鸡 3块一只 小鸡一块买3只
100块买100只鸡
公鸡、母鸡、小鸡各有多少只
// 穷举法 - 穷尽所有的可能性直到找到正确答案
for (int i = 0; i <= 100 / 5; i++) {
for (int j = 0; j <= 100 / 3; j++) {
for (int k = 0; k <= 99; k += 3) {
if (5 * i + 3 * j + k / 3 == 100 && i + j + k == 100) {
System.out.printf("公鸡有%d只,母鸡有%d只,小鸡有%d只\n",
i, j, k);
}
}
}
}
//
for (int x = 0; x <= 20; x++) {
for (int y = 0; y <= 33; y++) {
int z = 100 - x - y;
if (5 * x + 3 * y + z / 3 == 100 && z % 3 == 0) {
System.out.printf("公鸡有%d只,母鸡有%d只,小鸡有%d只\n",
x, y, z);
}
}
}
- 7.完美数
6 = 1 + 2 + 3
28 = 1 + 2 + 7 + 14
找1~10000 之间的完美数
for (int i = 2; i <= 10000; i++) {
int sum = 1;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
sum += j;
if (i / j != j) {
sum += i / j;
}
}
}
if (sum == i) {
System.out.println(i);
}
}
- 8.输入年月日,告诉这是这年的第几天
import java.util.Scanner;
// 输入年月日,告诉这是这年的第几天
public class Ex10 {
//如果程序中出现了重复的或相对独立的功能 那么应该将这些功能单独写出一个方法
public static boolean isLeapYear(int year) {
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
public static int daysOfMonth(int year, int month) {
if (month == 2) {
if (isLeapYear(year)) {
return 29;
}
else {
return 28;
}
}
else if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
}
else {
return 31;
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入年、月、日: ");
int year = input.nextInt();
int month = input.nextInt();
int day = input.nextInt();
int days = 0;
for (int i = 1; i < month; i++) {
/*
switch (i) {
case 4: case 6: case 9: case 11:
days += 30;
break;
case 2:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
days += 29;
}
else {
days += 28;
}
break;
default:
days +=31;
break;
}
*/
days += daysOfMonth(year, i);
}
days += day;
//System.out.println("这天是这一年的第" + days + "天");
System.out.printf("%d年%d月%d日是这一年的第%d天.", year, month, day, days);
input.close();
} }
作业
- **1.输出如下各种图形:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= 1; j--) {
if (j > i) {
System.out.print(" ");
}
else {
System.out.print("*");
}
}
for (int j = 4; j >= i; j--) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= 5; i++) {
for (int j = 4 - i; j >= 0; j--) {
System.out.print(" ");
}
for (int k = i * 2 - 1; k > 0; k--) {
System.out.print("*");
}
System.out.println();
}
-
2. 5个人去打鱼,打了很多鱼
a、b、c、d、e
a 先醒过来,分5份,多了一条,扔掉,拿走自己的一份
b 然后醒过来,分5份,多了一条,扔掉,拿走自己的一份
...
最少要捕多少鱼
public class Test01 {public static void main(String[] args) { int fish = 1; boolean flag = true; do { int fish2 = fish; for (int i = 1; i <= 5; i++) { if (fish2 % 5 == 1) { fish2 -= myOwn(fish2) + 1; } else { break; } if (i == 5) { flag = false; } } fish += 1; } while (flag); System.out.println("最少要捕: " + (fish - 1) + "条鱼"); } public static int myOwn(int fish) { return (fish - 1) / 5; } }
-
3.2个日期差多少天
public class Ex11 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入第一个日期的年、月、日: "); int year1 = input.nextInt(); int month1 = input.nextInt(); int day1 = input.nextInt(); System.out.print("请输入第二个日期的年、月、日: "); int year2 = input.nextInt(); int month2 = input.nextInt(); int day2 = input.nextInt(); int total = 0; if (year1 == year2) { total += theSameYearDifferenceBetweenMonth(year1, month1, day1, month2, day2); } else if (year1 < year2) { total += smallYearBigYearOfDays(year1, month1, day1, year2, month2, day2); } else { total += smallYearBigYearOfDays(year2, month2, day2, year1, month1, day1); } System.out.printf("%d年%d月%d日和%d年%d月%d日相差了%d天.", year1, month1, day1, year2, month2, day2 , total); input.close(); } public static boolean isLeapYear(int year) { return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; } public static int daysOfMonth(int year, int month) { int days; switch (month) { case 2: days=isLeapYear(year) ? 29 : 28; break; case 4: case 6: case 9: case 11: days = 30; break; default: days = 31; break; } return days; } public static int daysOfYear(int year) { return isLeapYear(year) ? 366 : 365; } public static int theSameYearDifferenceBetweenMonth(int year, int month1, int day1, int month2,int day2 ) { int total = 0; if (month1 == month2) { if (day1 >= day2) { total += day1 - day2; } else { total += day2 - day1; } } else if (month1 < month2) { for (int i = month1 + 1; i < month2; i++) { total += daysOfMonth(year, i); } total += daysOfMonth(year, month1) - day1 + day2; } else { for (int i = month2 + 1; i < month1; i++) { total += daysOfMonth(year, i); } total += daysOfMonth(year, month2) - day2 + day1; } return total; } public static int smallYearBigYearOfDays(int year1, int month1, int day1, int year2, int month2, int day2) { int total = 0; for (int i = year1 + 1; i < year2; i++) { total += daysOfYear(i); } if (month1 < 12) { total += theSameYearDifferenceBetweenMonth(year1, month1, day1, 12, 31); } else { total += daysOfMonth(year1, month1) - day1; } if (month2 > 1) { total += theSameYearDifferenceBetweenMonth(year2, 1, 1, month2, day2); } else { total += day2; } return total + 1; } }