栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

根据用户输入在Java中打印菱形

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

根据用户输入在Java中打印菱形

用户输入正整数,否则程序会提示用户,直到他们输入

您需要循环扫描

do{    userInput = keyboard.nextInt();     if (userInput % 2 == 0)        System.out.println("That is not an Odd Integer!");     if(userInput < 0)        System.out.println("That is not a Positive Odd Integer");} while(userInput < 0 || userInput %2 == 0);

现在您可以删除该验证

else if (userInput%2 == 1){

现在,我在检查循环时意识到的第一件事是

if (row+col==userInput-1 || )
不会编译,因为您有一个比较运算符,后面没有任何内容。

请注意,您可以替换为

System.out.print("n");
System.out.println("")
但这并不是很重要…

现在替换循环条件,使它们从0开始

for (int row = 0; row <= userInput; row++){        for (int col = 0; col <= userInput; col++ ){

现在,由于您想要钻石,因此需要有4个对角线,因此需要2个循环(一个用于顶部和底部)。

for (int i = 1; i < userInput; i += 2)//Draw the top of the diamond{    for (int j = 0; j < userInput - 1 - i / 2; j++)//Output correct number of spaces before    {        System.out.print(" ");    }    for (int j = 0; j < i; j++)//Output correct number of asterix    {        System.out.print("*");    }    System.out.print("n");//Skip to next line}for (int i = userInput; i > 0; i -= 2)//Draw the bottom of the diamond{    for (int j = 0; j < userInput -1 - i / 2; j++)    {        System.out.print(" ");    }    for (int j = 0; j < i; j++)    {        System.out.print("*");    }    System.out.print("n");}

所以最终的代码看起来像这样

public static void main(String[] args){    Scanner keyboard = new Scanner(System.in);    System.out.println("Welcome to the drawing program:");    System.out.println("Please Input a Positive Odd Integer:");    char userAnswer;    int userInput;    do    {        userInput = keyboard.nextInt();        if (userInput % 2 == 0)        { System.out.println("That is not an Odd Integer!");        }        if (userInput < 0)        { System.out.println("That is not a Positive Odd Integer");        }    } while (userInput < 0 || userInput % 2 == 0);    for (int i = 1; i < userInput; i += 2) //This is the number of iterations needed to print the top of diamond (from 1 to userInput by step of two for example with 5 = {1, 3, 5} so 3 rows.    {        for (int j = 0; j < userInput - 1 - i / 2; j++)//write correct number of spaces before, example with 5 = j < 5 - 1 -i / 2, so it would first print 4 spaces before, with 1 less untill it reach 0        {System.out.print(" ");//write a space        }        for (int j = 0; j < i; j++)        { System.out.print("*");//write an asterix        }        System.out.println("");    }    // Same logic apply here but backward as it is bottom of diamond    for (int i = userInput; i > 0; i -= 2)    {        for (int j = 0; j < userInput -1 - i / 2; j++)        { System.out.print(" ");        }        for (int j = 0; j < i; j++)        { System.out.print("*");        }        System.out.print("n");    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/431348.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号