第八章
1.打印直角三角形
package tz1;
import java.util.Scanner;
public class dome31 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num=0;
System.out.println(“输入打印行数:”);
num =input.nextInt();
for (int i = 1; i <=num; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
2.百鸡问题
package tz1;
public class dome32 {
public static void main(String[] args) {
int i, j, k;
for (i = 0; i < 20; i++) {
for (j = 0; j < 33; j++) {
k=100-i-j;
if (5 * i + 3 * j + k/3 == 100) {
System.out.println("公鸡" + i + "母鸡" + j + "小鸡" + k);
}
}
}
}
}
3学员的平均成绩
package tz1;
import java.util.Scanner;
public class dome33 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int [] score = new int [4];
int classnum = 3;
double sum = 0.0;
double avg = 0.0;
int count =0;
for (int i = 0; i < classnum; i++) {
System.out.println("请输入"+(i+1)+"个班的成绩");
for (int j = 0; j < score.length; j++){
System.out.println("第"+(j+1)+"个学员的成绩");
score[j]=input.nextInt();
sum = sum+score[j];
if (score[j]<85) {
continue;
}count++;
}avg = sum/score.length;
}System.out.println("所有学员成绩大于85分的平均成绩是"+avg);
}
}
银行取钱
package tz1;
import java.util.Scanner;
public class dome34 {
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
String pasword;
int money;
for (int i = 1; i <= 3; i++) {
System.out.println("请输入密码:");
pasword = input.next();
if ("111111".equals(pasword)) {
}else {System.out.println("请重新输入密码");
continue;
}
for ( int j = 1; j<=3; j++) {
System.out.println("请输入取款金额:");
money=input.nextInt();
if (money>0&&money<=1000) {
System.out.println("您取了"+money);
break;
}else {
System.out.println("您输入的都金额不合法,请重新输入");
continue;
}
}
break;
}System.out.println("交易完成");
}
}
打印菱形
package tz1;
import java.util.Scanner;
public class dome35_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(“请输入菱形数:”);
int rows = input.nextInt();
while (rows%2==0) {
System.out.print(“请输入奇数:”);
rows = input.nextInt();
}
for (int i = 0; i < rows; i++) {
int t = 0; // t是用来存放距离边界需要多少空格
if (i >= rows / 2) {// >是m为奇数的情况下的判断,=是m为偶数的情况下的判断,这里将过半的那些i值转换为与它对称的i值
t = rows - 1 - i;
} else {
t = i;
}
for (int j = 1; j <= rows; j++) {
if (j >= (rows + 1) / 2 - t && j <= (rows + 1) / 2 + t) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.print("\n");
}
}
}
作者:tb19930719
来源:CSDN
原文:https://blog.csdn.net/tb19930719/article/details/80039660
版权声明:本文为博主原创文章,转载请附上博文链接!
上一篇: 《JavaScript DOM编程艺术》(第二版)学习笔记(一)
下一篇: 第八章