①:模拟ATM机取款业务。错误输入三次密码则取卡。错误输入3次数额则取卡。
package Z1;
import java.util.Scanner;
public class Test6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String a = "";
int i, j, b = 0;
for (i = 0; i < 3; i++) {
System.out.println("请输入您的密码:");
a = input.next();
if (a.equals("123")) {
System.out.println("请输入取款金额:(金额数需是100的倍数,且≤1000元。)");
for (j = 0; j < 3; j++) {
b = input.nextInt();
if (b % 100 == 0 && b <= 1000 && b > 0) {
System.out.println("您取了"+b+"百元。\\n交易完成,请取出您的卡。");
return;
} else {
if (j < 2)
System.out.println("您输入的金额格式有误,请重新输入:(金额数需是100的倍数,且≤1000元。)");
else
System.out.println("错误次数过多,请取卡。");
continue;
}
}
break;
} else {
if (i < 2)
System.out.println("密码错误,请重新输入密码:");
else {
System.out.println("密码输入错误次数过多,请取卡。");
}
continue;
}
}
}
}
②:古代百钱买百鸡
public class Test9 {
public static void main(String[] args) {
int a, b, c;
for (a = 0; a <= 20; a++)
for (b = 0; b <= 50; b++)
for (c = 0; c <= 100; c++)
if (a + b + c == 100 && 5 * a + 2 * b + c / 3 == 100
&& c % 3 == 0 && a > 0 && b > 0 && c > 0)
System.out.println("公鸡" + a + "\\t" + "母鸡" + b + "\\t"
+ "小鸡" + c + "\\t");
}
}
运行结果: