输出n行*组成的等腰三角形。
public class IsoscelesTriangleDomo {
public static void main(string[] args){
//这里假设输入为5
printTriangle(5);
}
publie static void printTriangle(int n){
for(int i = 1; i <= n; i++){
// 打印前导空格
for(int j = 1; j <= n - 1; j++){
System.out.print(" ");
}
// 打印*
for (int k = 1:k <= 2 * i - 1; k++){
System.out.print("*");
}
// 换行
System.out.println();
}
}
}
九九乘法表
public class MultiplicationTableDemo{
public static void main(string[] args){
int m = 9;
int n = 9;
String str = m + "*" + n + "=" + m*n;
int maxLength = str.length();
for(int i=1; i<=m; i++){
for(int j=1; j<=i; j++){
str = String.format("%-"+ maxLength + "s\t",i + "*" + j + "=" + i*j);
System.out.print(str);
}
System.out.println();
}
}
}
百钱买百鸡
有一人去买鸡,公鸡每只5元,母鸡每只3元,小鸡每3只1元,用100元买100只鸡,要求公鸡、母鸡、小鸡都要有,各买多少只?
public class ChickenDemo {
public static void main(String[]args){
int totalMoney = 100;//总金额
int totalChickens=100://总鸡数
for(int roosters = 1; roosters <= totalMoney / 5; roosters++){
for(int hens = 1; hens <= totalMoney / 3; hens++){
int chicks = totalChickens - roosters - hens;
int cost = roosters * 5 + hens * 3 + chicks / 3;
// 检查是否满足条件
if(chicks >= 1 && chicks % 3 == 0 && cost == totalMoney){
System.out.println("公鸡:"+ roosters +"只,母鸡:"+ hens +"只,小鸡:"+ chicks +"只");
}
}
}
}
}
168万+

被折叠的 条评论
为什么被折叠?



