- 循環(huán)分支的例子
兩顆骰子刊愚,第一次搖出7,11,玩家勝踩验,搖出2,3,12鸥诽,莊家勝。
第二次以后搖出的點數(shù)與第一輪相同箕憾,玩家勝牡借,搖出7點莊家勝:
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
//[min,max] - (int) (Math.random() * (max - min + 1) + min);
//[min,max) - (int) (Math.random() * (max - min) + min)
Scanner input = new Scanner(System.in);
int money = 1000;
do {
System.out.println("你的總資產(chǎn)還有 " + money + "元.");
int bet;
do {
System.out.println("請下注: ");
bet = input.nextInt();
} while ( bet < 0 || bet > money);
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到10各個數(shù)求平方:
public class Test02 {
public static void main(String[] args) {
for (int j = 1; j <= 10; j += 1) {
System.out.printf("%3d %5d\n",j,j*j);
}
}
}
任意輸入一個數(shù),判斷是不是素數(shù):
import java.util.Scanner;
public class Test03 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("輸入一個數(shù): ");
int n = input.nextInt();
boolean isPrime = true;
for (int i = 2; i <= n -1; i++) {
if (n % i == 0){
isPrime = false;
break;
}
}
System.out.println(n + (isPrime ? "是": "不是") +"一個素數(shù)");
input.close();
}
}
九九乘法表:
public class Test05 {
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();
}
}
}
公雞5塊1只扁远,母雞3塊1只俊鱼,小雞1塊3只,
100塊錢買100只雞:
public class Test07 {
public static void main(String[] args) {
for (int i = 0; i <= 20; i++) {
for (int j = 0; j <= 33; j++) {
for (int y = 0; y <= 99; y += 3) {
if( i + j + y == 100 && 5 * i + 3 * j + y / 3 == 100){
System.out.printf("公雞%d, 母雞%d畅买, 小雞%d\n",i,j,y);
}
}
}
}
}
}
完美數(shù):
public class Test08 {
public static void main(String[] args) {
for (int i = 2; i <= 10000; i++) {
int y = 1;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
y += j;
if(i / j != j){
y += i / j;
}
}
}
if (y == i) {
System.out.println(i);
}
}
}
}
輸入年月日并闲,判斷那一天是那一年的多少天:
import java.util.Scanner;
// 如果你的程序里面出現(xiàn)了重復(fù)或相對獨立的功能 那么應(yīng)該將這些功能單獨寫成一個方法
public class Test10 {
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(isLeapYear(year)){
if(month == 2){
return 29;
}
else if(month == 4 || month == 6 || month == 9 || month == 11){
return 30;
}
else{
return 31;
}
}
else{
if(month == 2){
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.println("請輸入年,月谷羞,日");
int year = input.nextInt();
int month = input.nextInt();
int day = input.nextInt();
int x = 0;
for (int i = 1; i < month; i++) {
x += daysOfMonth(year, i);
input.close();
}
System.out.println(x + day);
}
}
5個人捕魚帝火,第一個人把所有的分成5份,拿走一份湃缎,第二個人把剩下的所有分成五份犀填,拿走一份,第三個人把第二個人剩下的所有分成五份拿走一份嗓违。九巡。。蹂季。他們至少捕了多少魚
public class Test14 {
public static void main(String[] args) {
for (int i = 2; i > 0; i++) {
int b = i;
for (int j = 1; j <= 5; j++) {
b = fishAll(b);
}
if(b != 0){
System.out.println(i);
break;
}
}
}
private static int fishAll(int b) {
if((b - 1) % 5 == 0){
b = (b - 1) / 5 * 4;
}
else{
b = 0;
return b;
}
return b;
}
}