java打印形状,基于用户输入在Java中打印钻石形状

I have a problem with a program I am trying to write. A user inputs a positive odd integer, otherwise the program prompts the user until they do. When they do, the program prints a diamond shape corresponding to the user input.

I have this piece so far that prints the left hand diagonal of such a figure, but cannot figure out how to print the rest of it. Here is the code:

import java.util.Scanner;

public class DrawingProgram {

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;

userInput = keyboard.nextInt();

if (userInput%2 == 0){

System.out.println("That is not a Positive Odd Integer!");

}

else if (userInput < 0){

System.out.println("That is not a Positive Odd Integer");

}

else if (userInput%2 == 1){

for (int row = 1; row<= userInput; row++){

for (int col = 1; col<= userInput; col++ ){

if (row+col==userInput-1 )

System.out.print( "*");

else

System.out.print( " ");

}

System.out.print("\n");

}

}

}

}

解决方案

A user inputs a positive odd integer, otherwise the program prompts

the user until they do

You need to scan in a loop

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);

Now you can remove that validation else if (userInput%2 == 1){

Now the first thing I realize when checking at your loop is if (row+col==userInput-1 || ) which won't compile as you have a comparison operator with nothing following.

Note that you can replace System.out.print("\n"); with System.out.println("") but that's not really important...

Now replace your loop condition so that they start as 0

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

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

Now since you want a Diamond, you need to have 4 diagonal, so 2 loops (one for top and bottom)...

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");

}

So the final code would look like this

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");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值