第四天:程序邏輯-2(分支和循環)
練習1:猜數字
計算機出一個1-100之間的隨機數,玩家輸入猜測的數字,計算機會給出相應的提示:如果玩家猜測的數字大於計算機出的數字,則提示"小一點";如果玩家猜測的數字小於計算機出的數字,則提示"大一點";如果猜對了就給出恭喜信息和猜的次數,遊戲結束。
package com.lovoinfo;
import java.util.Scanner;
/**
* 猜數字
* @author jackfrued
*
*/
public class Test04 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int correctAnswer = (int) (Math.random() * 100 + 1);
int counter = 0;
int thyAnswer;
do {
System.out.print("請輸入你猜的數字: ");
thyAnswer = sc.nextInt();
counter += 1;
if (thyAnswer == correctAnswer) {
System.out.println("恭喜你猜對了!總共猜了" + counter + "次");
if(counter > 7) {
System.out.println("智商拙計!!!");
}
} else if (thyAnswer > correctAnswer) {
System.out.println("小一點!");
} else {
System.out.println("大一點");
}
} while (thyAnswer != correctAnswer);
sc.close();
}
}
練習2:人機猜拳
package com.lovoinfo;
import java.util.Scanner;
/**
* 人機猜拳
* @author jackfrued
*
*/
public class Test05 {
/**
* 將出拳對應的數字變成中文
* @param fist 出拳的數字
* @return 出拳字對應的中文
*/
public static String getFist(int fist) {
String str;
if(fist == 1) {
str = "剪刀";
}
else if(fist == 2) {
str = "石頭";
}
else {
str = "布";
}
return str;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int money = 1000;
do {
int debt;
do {
System.out.println("你總共有" + money + "元");
System.out.print("請下注: ");
debt = sc.nextInt();
} while(debt <= 0 || debt > money);
int computerFist = (int) (Math.random() * 3 + 1);
int thyFist;
do {
System.out.print("請出拳(1. 剪刀; 2. 石頭; 3. 布): ");
thyFist = sc.nextInt();
} while (thyFist < 1 || thyFist > 3);
System.out.println("計算機出的是" + getFist(computerFist));
System.out.println("你出的是" + getFist(thyFist));
if(computerFist == thyFist) {
System.out.println("平局!");
}
else {
if(computerFist == 1) {
if(thyFist == 2) {
money += debt;
System.out.println("你贏了!");
}
else {
money -= debt;
System.out.println("計算機贏了!");
}
}
else if(computerFist == 2) {
if(thyFist == 1) {
money -= debt;
System.out.println("計算機贏了!");
}
else {
money += debt;
System.out.println("你贏了!");
}
}
else {
if(thyFist == 1) {
money += debt;
System.out.println("你贏了!");
}
else {
money -= debt;
System.out.println("計算機贏了!");
}
}
}
} while(money > 0);
System.out.println("你破產了!遊戲結束!");
sc.close();
}
}
練習3:Craps賭博遊戲。
玩家搖兩顆色子,如果第一次搖出了7點和11點,則玩家勝;如果第一次搖出了2點、3點、12點,則莊家勝;如果搖出其他點數,遊戲繼續,在繼續的過程中,如果玩家搖出了第一次搖的點數,則玩家勝;如果搖出了7點,則莊家勝;否則遊戲繼續,直到分出勝負。
package com.lovoinfo;
import java.util.Scanner;
/**
* Craps賭博遊戲
* @author jackfrued
*
*/
public class Test03 {
/**
* 搖兩顆色子
* @return 兩個色子搖出的點數之和
*/
public static int rollDice() {
int face1 = (int) (Math.random() * 6 + 1);
int face2 = (int) (Math.random() * 6 + 1);
return face1 + face2;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int money = 1000;
do {
int debt = 0; // 下注的金額
do {
System.out.println("你的餘額" + money + "元");
System.out.print("請下注: ");
if(sc.hasNextInt()) { // 判斷能否讀到一個整數
debt = sc.nextInt();
}
else {
System.out.println("輸入錯誤!");
sc.nextLine(); // 把錯誤的輸入讀走
}
} while (debt <= 0 || debt > money);
int firstPoint = rollDice();
System.out.println("玩家搖出了" + firstPoint + "點");
boolean gameOver = true; // 表示遊戲是否應該結束的布爾值
switch(firstPoint) {
case 7:
case 11:
money += debt;
System.out.println("玩家勝!");
break;
case 2:
case 3:
case 12:
money -= debt;
System.out.println("莊家勝!");
break;
default:
gameOver = false; // 如果第一次沒有分出勝負遊戲就沒有結束
}
while(!gameOver) { // 只要遊戲沒有結束就要繼續搖色子
int currentPoint = rollDice();
System.out.println("玩家搖出了" + currentPoint + "點");
if(currentPoint == 7) {
money -= debt;
System.out.println("莊家勝!");
gameOver = true;
}
else if(currentPoint == firstPoint) {
money += debt;
System.out.println("玩家勝!");
gameOver = true;
}
}
} while(money > 0);
System.out.println("恭喜你, 破產了!");
sc.close();
}
}
練習4:九九表
package com.lovoinfo;
public class Test09 {
public static void main(String[] args) {
for(int i = 1; i <= 9; i++) {
for(int j = 1; j <= i; j++) {
System.out.printf("%d*%d=%d\t", i, j, i * j);
}
System.out.println();
}
}
}
作業:輸出直角三角形圖案。
package com.lovoinfo;
public class Test10 {
private static final int ROW = 10;
public static void main(String[] args) {
for(int i = 1; i <= ROW; i++) {
for(int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
System.out.println("\n---華麗的分隔線---\n");
for(int i = 1; i <= ROW; i++) {
for(int j = ROW; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}
System.out.println("\n---華麗的分隔線---\n");
for(int i = 1; i <= ROW; i++) {
for(int j = 1; j <= ROW; j++) {
if((ROW - i) >= j) {
System.out.print(" ");
}
else {
System.out.print("*");
}
}
System.out.println();
}
}
}
```
練習5:百錢百雞。
公雞5元一隻,母雞3元一隻,小雞1元三隻,用100元買100只雞,問公雞、母雞和小雞各有多少隻?
```Java
package com.lovoinfo;
/**
* 百錢買百雞(窮舉法)
* @author jackfrued
*
*/
public class Test14 {
public static void main(String[] args) {
System.out.println("公雞\t母雞\t小雞");
// 假設公雞x只, 母雞y只, 小雞z只
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\t%d\t%d\n", x, y, z);
}
}
}
}
}