- 循环分支的例子
两颗骰子,第一次摇出7,11,玩家胜,摇出2,3,12,庄家胜。
第二次以后摇出的点数与第一轮相同,玩家胜,摇出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("你的总资产还有 " + 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各个数求平方:
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);
}
}
}
任意输入一个数,判断是不是素数:
import java.util.Scanner;
public class Test03 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入一个数: ");
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 ? "是": "不是") +"一个素数");
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);
}
}
}
}
}
}
完美数:
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;
// 如果你的程序里面出现了重复或相对独立的功能 那么应该将这些功能单独写成一个方法
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;
}
}
网友评论