for循环练习 打印图形
import java.util.Random; import java.util.Scanner; public class day0812housework { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { // TODO Auto-generated method stub /** * 5、使用循环嵌套在控制台使用*完成长方形的打印 * * 6、使用循环嵌套在控制台使用*完成直角三角形的打印(直角四个方向) * * 7、使用循环嵌套在控制台使用*完成平行四边形的打印 * * 8、使用循环嵌套在控制台使用*完成等腰三角形的打印 * * 9、使用random工具类完成猜数字游戏的书写 * */ // 使用循环嵌套在控制台使用*完成长方形的打印 // question1(); // 使用循环嵌套在控制台使用*完成直角三角形的打印(直角四个方向) question2(); // 使用循环嵌套在控制台使用*完成平行四边形的打印 // question3(); // 使用循环嵌套在控制台使用*完成等腰三角形的打印 // question4(); // 使用random工具类完成猜数字游戏的书写 // question5(); } // 使用random工具类完成猜数字游戏的书写 public static void question5() { System.out.println("开始游戏,注意不要输入0,0代表退出程序。请输入你猜的数字:"); // 生成随机数 Random random = new Random(); int num = random.nextInt(100) + 1; while (true) { int guessNum = sc.nextInt(); if (guessNum == 0) { System.out.println("你已经退出游戏"); break; } else { if (guessNum > num) { System.out.println("你猜的" + guessNum + "大了。请再次输入"); } else if (guessNum < num) { System.out.println("你猜的" + guessNum + "小了。请再次输入"); } else { System.out.println("恭喜你猜对了,正确值为:" + guessNum+"新一轮游戏开始,输入0可退出游戏。"); // 生成新一轮随机数 num = random.nextInt(100) + 1; } } } sc.close(); } // 使用循环嵌套在控制台使用*完成等腰三角形的打印 // * // *** // ***** // ******* public static void question4() { System.out.println("输入行:"); int row = sc.nextInt(); System.out.println("===================="); //外层循环控制行数 for (int i = row; i >= 1; i--) { //打印出空的上边 for (int j = 1; j <= i; j++) { System.out.print(" "); } //打印出实体下边 for (int k = 1; k <= row - i + 1; k++) { System.out.print("*"); } //从第二行进行循环打印 // j>=i+1 相当与int j = row -1; for (int j = row; j >= i + 1; j--) { System.out.print("*"); } System.out.println(); } sc.close(); } // 使用循环嵌套在控制台使用*完成平行四边形的打印 // **** // **** // **** // **** public static void question3() { System.out.println("输入行:"); int row = sc.nextInt(); System.out.println("===================="); for(int i = row;i>=1;i--){ for(int j = 1;j<=i;j++){ System.out.print(" "); } for(int k = row;k>=1;k--){ System.out.print("*"); } System.out.println(); } sc.close(); } // 使用循环嵌套在控制台使用*完成直角三角形的打印(直角四个方向) public static void question2() { System.out.println("输入行:"); int row = sc.nextInt(); // * // ** // *** // **** System.out.println("===================="); for(int i = 1;i<=row;i++){ for(int j = 1; j<=i;j++){ System.out.print("*"); } System.out.println(); } System.out.println("===================="); // **** // *** // ** // * for(int i = 1;i<= row;i++){ for(int j = row;j>=i;j--){ System.out.print("*"); } System.out.println(); } System.out.println("===================="); // * // ** // *** // **** for(int i = row;i>=1;i--){ for(int j = 1;j<=i;j++){ System.out.print(" "); } for(int k = 1;k<=row-i+1;k++){ System.out.print("*"); } System.out.println(); } System.out.println("===================="); // **** // *** // ** // * for(int i = row;i>=1;i--){ for(int j = row;j >= i; j--){ System.out.print(" "); } for(int k = row;k>=row-i+1;k--){ System.out.print("*"); } System.out.println(); } sc.close(); } // 使用循环嵌套在控制台使用*完成长方形的打印 public static void question1() { System.out.println("输入行:"); int row = sc.nextInt(); for(int i =1;i<=row;i++){ for(int j = 1;j<=row;j++){ System.out.print("*"); } System.out.println(); } sc.close(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!