一.循環(huán)結(jié)構(gòu)與分支結(jié)構(gòu)的嵌套練習(xí)
1.輸入一個(gè)數(shù),判斷是不是素?cái)?shù)(只能被1和自身整除的數(shù))
import java.util.Scanner;
public class Text1 {
public static int sort(int count) {
count = (count + 1) * 2;
return count;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.println("請(qǐng)輸入一個(gè)數(shù)");
*int a =input.nextInt(); boolean isPrime=true; for(int i =2;i<a;i++){
if(a%i==0){ isPrime=false; break; } } System.out.println(a
+(isPrime?"是":"不是")+"素?cái)?shù)"); input.close();
2.打印100以內(nèi)的質(zhì)數(shù)株汉;
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);
}
}
3.九九乘法表
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
System.out.printf("%d*%d=%d\t",j,i,j*i);
}
System.out.println("");
}
4.猴子吃桃問(wèn)題
int count = 1;
for (int i = 1; i < 10; i++) {
sort(count);
}
System.out.println(count);
}
}
.窮舉法案列:100塊錢買一百只雞
public class HomeWork1 {
public static void main(String[] args) {
for (int gj = 0; gj <= 20; gj++) {
for (int mj = 0; mj <= 33; mj++) {
int xj = 100 - gj - mj;
if (5 * gj + 3 * mj + xj / 3 == 100 && (gj + mj + xj) == 100 && xj % 3 == 0) {
System.out.println("公雞有 " + gj + " 母雞有 " + mj + " 小雞有 " + xj);
}
}
}
}
}
二.兩個(gè)典型案列
1.Graps賭博游戲:
<1>我的代碼
package comeDay12_01_2;
import java.util.Scanner;
public class Homework2 {
public static void main(String[] args) {
System.out.println("******************游戲規(guī)則**************************");
System.out.println("**玩家總共1000分,每次贏加100分蝙云,每次輸扣100分,平局分?jǐn)?shù)不變**");
System.out.println("**************************************************");
System.out.println("準(zhǔn)備好開(kāi)始了嗎路召,按\"1\"退出,其他任意鍵繼續(xù):");
int fen = 200;
int num = 0;
int num1 = 0;
Scanner input = new Scanner(System.in);
String zb = input.nextLine();
if (zb.equals("1")) {
System.out.println("游戲結(jié)束优训,還剩" + fen + "分");
} else {
int a = (int) (Math.random() * 6 + 1);
int b = (int) (Math.random() * 6 + 1);
num = a + b;
if (num == 7 || num == 11) {
fen += 100;
System.out.println("玩家勝利朵你,搖出了" + num + "點(diǎn),當(dāng)前分?jǐn)?shù)" + fen);
} else if (num == 2 || num == 3 || num == 12) {
fen -= 100;
System.out.println("電腦勝利,搖出了" + num + "點(diǎn),當(dāng)前分?jǐn)?shù)" + fen);
} else {
System.out.println("平局揣非,搖出了" + num + "點(diǎn),當(dāng)前分?jǐn)?shù)" + fen);
}
System.out.println("按\"1\"退出抡医,其他任意鍵繼續(xù):");
String zb1 = input.nextLine();
if (zb1.equals("1")) {
System.out.println("游戲結(jié)束");
} else {
do {
int a1 = (int) (Math.random() * 6 + 1);
int b1 = (int) (Math.random() * 6 + 1);
num1 = a1 + b1;
if (num == num1) {
fen += 100;
System.out.println("玩家勝利,搖出了" + num1 + "點(diǎn),當(dāng)前分?jǐn)?shù)" + fen);
} else if (num1 == 7) {
fen -= 100;
System.out.println("電腦勝利,搖出了" + num1 + "點(diǎn),當(dāng)前分?jǐn)?shù)" + fen);
} else {
System.out.println("平局忌傻,搖出了" + num1 + "點(diǎn),當(dāng)前分?jǐn)?shù)" + fen);
}
System.out.println("按\"1\"退出大脉,其他任意鍵繼續(xù):");
String zb2 = input.nextLine();
if (zb2.equals("1")) {
System.out.println("游戲結(jié)束");
} else {
int a2 = (int) (Math.random() * 6 + 1);
int b2 = (int) (Math.random() * 6 + 1);
a1 = a2;
b1 = b2;
}
} while (fen > 0);
System.err.println("你已經(jīng)輸完了!Kⅰ镰矿!無(wú)法繼續(xù)了!7帧秤标!");
}
}
input.close();
}
}
<2>教師代碼
- switch case 結(jié)構(gòu)中,case后面若不跟break宙刘,程序會(huì)繼續(xù)執(zhí)行下去苍姜,直到出現(xiàn)break為止
public class HomeWork__ {
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.println("請(qǐng)下注");
bet=input.nextInt();
} while (bet<=0||bet>money);
int face1 = (int) (Math.random() * 6 + 1);
int face2 = (int) (Math.random() * +1);
int firsstPoint = face1 + face2;
System.out.println("玩家搖出" + firsstPoint + "點(diǎn)");
boolean needsGoOn = false;
switch (firsstPoint) {
case 7:
case 11:
System.out.println("玩家勝");
money+=bet;
break;
case 2:
case 3:
case 12:
System.out.println("莊家勝");
money-=bet;
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 + "點(diǎn)");
if (currentPoint == 7) {
System.out.println("莊家勝");
money-=bet;
needsGoOn = false;
} else if (currentPoint == firsstPoint) {
System.out.println("玩家勝");
money+=bet;
needsGoOn = false;
}
}
} while (money>0);
System.out.println("你已經(jīng)破產(chǎn)");
input.close();
}
2.輸入年月日悬包,輸出這是這一年的第幾天
- 如果程序中出現(xiàn)了重復(fù)的或者相對(duì)獨(dú)立的功能衙猪,那么應(yīng)該將這些功能單獨(dú)寫(xiě)到一個(gè)方法中
package comDay12_02_2;
import java.util.Scanner;
public class Text3 {
// year作為因變量時(shí),此方法需要返回一個(gè)判斷是否為閏年的結(jié)果(是or不是)布近,屬于布爾類型垫释;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("請(qǐng)輸入年 月 日");
int year = input.nextInt();
int month = input.nextInt();
int day = input.nextInt();
int sum = 0;
for (int i = 1; i < month; i++) {
sum += daysOfMonth(year, i);
}
System.out.printf("有%d天", (sum + day));
input.close();
}
//isLeapYear方法判斷輸入的年份是否屬于閏年
public static boolean isLeapYear(int year) {
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
//daysOfMonth用于判斷當(dāng)前月份的天數(shù)
public static int daysOfMonth(int year, int month) {
if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
} else if (month == 2) {
if (isLeapYear(year)) {
return 29;
} else {
return 28;
}
} else {
return 31;
}
}
}
}