第一题:编写程序,打印如下图案,要求该图案的行数由用户输入。
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入行数:");
int lineNumber = input.nextInt();
for (int i = 1; i <= lineNumber; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j+" ");
}
System.out.println("");
}
}
}
第二题:买鸡问题
public class Test1 {
public static void main(String[] args) {for (int i = 0; i <20; i++) { //循环公鸡的数量
for (int j = 0; j < 33; j++) { //循环母鸡的数量
for (int j2 = 0; j2 < 300; j2++) { //循环小鸡的数量
if (((5*i)+(3*j)+(j2/3))==100 && (i+j+j2)==100) {
System.out.println("公鸡有:"+(i+1)+"\t"+"母鸡有:"+(j+1)+"\t"+"小鸡有:"+(j2+1));
}
}
}
}
}
}
第三题:有三个班级各四名学员参赛,从控制台输入每个班级参赛学员的成绩,要求统计出三个班级所有参赛学员中成绩大于85分的学员的平均分,如何编程实现?
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
int [][] arr = new int [3][4]; //定义二维数组,分别接受3个班的每个班上的4位同学的成绩
int count = 0; //平均分大于85的人数
double avg = 0.0;//平均分大于85的同学的平均分
int totalScore = 0; //平均分大于85分的同学的总成绩
for (int i = 0; i < arr.length; i++) {
int sum = 0;//初始化成绩和
double avg1 = 0.0;//初始化平均分
System.out.println("第"+(i+1)+"个班的"+"成绩");
for (int j = 0; j < arr[i].length; j++) {
Scanner input = new Scanner(System.in);
System.out.print("请输入第"+(j+1)+"位同学的成绩为:");
if (input.hasNextInt() == true) {
int score = input.nextInt();
arr[i][j] = score;
sum += arr[i][j];
if (score>=85) {
count++;
totalScore += arr[i][j];
}
}
avg = totalScore/(count*1.0);
}
System.out.println("成绩大于85分的学员的平均分的人数为:"+count);
System.out.println("成绩大于85分的学员的平均分为:"+avg);
}
}
}
第四题:银行取钱问题
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.print("请输入密码:");
int password =input.nextInt();
if (password == 123456) {
System.out.print("请输入金额:");
int money = input.nextInt();
while ((money % 100 == 0 && money > 0 && money <= 1000) == false) { // 循环判断用户输入的金额是否符合银行的出钱条件
System.out.print("您输入的金额不合法,请重新输入:");
money =input.nextInt();
}
System.out.print("您取出了" + money + "元\n");
break;
} else {
if (i == 2) {
System.out.println("密码错误,请取卡!");
}
continue;
}
}
System.out.println("交易完成,请取卡!");
}
}