简体   繁体   English

在 Java 上处理菱形内的数字

[英]Manipulate number inside diamond shape on Java

picture of result that i want:我想要的结果图片:

钻石

I want to print out diamond shape like in the picture in the link.我想打印出链接中图片中的菱形。 The user will first need to input either 0,1 and 2 only.用户首先只需要输入 0,1 和 2。 If user enter 0 the result will be on the left one in the picture.如果用户输入 0,结果将在图片的左侧。 If user enter 1 the result will be same as the middle one and if user enter 2, result like the right one.如果用户输入 1,结果将与中间的结果相同,如果用户输入 2,结果与正确的结果相同。

From what I understand I need to do the half part of the diamond first.据我了解,我需要先做钻石的一半。 There will be the outer loop to determine the row.将有外部循环来确定行。 First inner loop to create the whitespaces.创建空格的第一个内部循环。 Second, inner loop for the number input by user.第二,用户输入数字的内循环。 Lastly make another half of the diamond, just make it decrement instead.最后再制作一半的钻石,只是让它减量。

The problem is I don't understand how to manipulate the number like in the result in the picture.问题是我不明白如何操纵图片中结果中的数字。

Can someone give some hint on what should I do.有人可以提示我该怎么做。 I don't want the code.我不想要代码。 Maybe, just a bit explanation on what i should know first to do this也许,只是对我首先应该知道的做一点解释

Hint: it can be done with 2 for loops: one for row number (0 to 8 inclusive) and one for column number (also 0 to 8 inclusive).提示:可以使用 2 个 for 循环来完成:一个用于行号(包括 0 到 8),一个用于列号(也包括 0 到 8)。

Start creating a pattern like:开始创建一个模式,如:

432101234
432101234
...

extended to 9 lines (I just did't want to make it too big).扩展到 9 行(我只是不想让它太大)。 Basically some calculation using just the column number like in 4 - col (not complete, missing handling of negative results - intentionally left for the reader to do).基本上一些计算只使用列号,如4 - col (不完整,缺少对负面结果的处理 - 故意留给读者去做)。

Then think of a condition using row and column number to eliminate the first corner (something like col < 4-row ).然后考虑使用行号和列号来消除第一个角的条件(类似于col < 4-row )。 In that case print a space instead of the digit:在这种情况下,打印一个空格而不是数字:

    01234
   101234
  2101234
 32101234
432101234
432101234
432101234
432101234
432101234

do similar for the other three corners.对其他三个角做类似的事情。

And, at last, multiply each digit by the input value (0, 1 or 2).最后,将每个数字乘以输入值(0、1 或 2)。


"what i should know first" “我首先应该知道什么”

  • for loops for循环
  • if statements or conditional operator ( ? : ) if语句或条件运算符 ( ? : )
  • comparing numbers and some maths ( col < 4 - row )比较数字和一些数学( col < 4 - row
  • output System.out.println() and System.out.print() ) output System.out.println()System.out.print() )
  • user input using the main argument(s) or reading standard input (depending on how that should be done)使用main参数的用户输入或读取标准输入(取决于应该如何完成)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM