如何在java中打印一个数字三角形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21568210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to print a number triangle in java
提问by user3273509
I need to produce a triangle as shown:
我需要生成一个三角形,如图所示:
1
22
333
4444
55555
and my code is:
我的代码是:
int i, j;
for(i = 1; i <= 5; i++)
{
for(j = 1; j <= i; j++)??
{??????????
System.out.print(i);?
}??????
System.out.print("\n");????????
}
Producing a triangle the opposite way
以相反的方式生成三角形
1
22
333
4444
55555
What do i need to do to my code to make it face the right way?
我需要对我的代码做什么才能让它以正确的方式面对?
回答by user3273509
I came across this problem in my AP CS class. I think you may be starting to learn how to program so heres what I'd do without giving you the answer.
我在 AP CS 课上遇到了这个问题。我想你可能开始学习如何编程,所以我会在不给你答案的情况下做些什么。
Use a loop which removes the number of spaces each iteration. The first time through you would want to print four spaces then print 1 one time(probably done in a separate loop). Next time through one less space, but print i one more time.
使用循环删除每次迭代的空格数。第一次你想打印四个空格然后打印 1 一次(可能在一个单独的循环中完成)。下次通过少一个空间,但再打印一次。
回答by Brian Roach
You need to print some spaces. There is a relation between the number of spaces you need and the number (i
) you're printing. You can print X number of spaces using :
您需要打印一些空格。您需要的空格数与i
您要打印的数字 ( )之间存在关系。您可以使用以下方法打印 X 个空格:
for (int k = 0; k < numSpaces; k++)
{
System.out.print(" ");
}
So in your code:
所以在你的代码中:
int i, j;
for(i = 1; i <= 5; i++)
{
// Determine number of spaces needed
// print spaces
for(j = 1; j <= i; j++)
{
System.out.print(i);
}
System.out.print("\n");
}
回答by Roshan
use this code ,
使用此代码,
int i, j,z;
boolean repeat = false;
for (i = 1; i <= 5; i++) {
repeat = true;
for (j = 1; j <= i; j++) {
if(repeat){
z = i;
repeat = false;
while(z<5){
System.out.print(" ");
z++;
}
}
System.out.print(i);
}
{
System.out.print("\n");
}
}
回答by Denis C de Azevedo
You can use this:
你可以使用这个:
int i, j;
int size = 5;
for (i = 1; i <= size; i++) {
if (i < size) System.out.printf("%"+(size-i)+"s", " ");
for (j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.print("\n");
}
This line:
if (i < size) System.out.printf("%"+(size-i)+"s", " ");
这一行:
if (i < size) System.out.printf("%"+(size-i)+"s", " ");
Is going to print the left spaces.
将打印左边的空格。
It uses the old printf
with a fixed sized string like 5 characters: %5s
它使用printf
具有固定大小的字符串(如 5 个字符)的旧字符串:%5s
Try it here: http://ideone.com/jAQk67
在这里试试:http: //ideone.com/jAQk67
回答by Martin Frank
i'm having trouble sometimes as well when it's about formatting on console... ...i usually extract that problem into a separate method...
当我在控制台上格式化时,有时也会遇到问题... ...我通常将该问题提取到一个单独的方法中...
all about how to create the numbers and spacing has been posted already, so this might be overkill ^^
关于如何创建数字和间距的所有信息已经发布,所以这可能有点矫枉过正^^
/**
* creates a String of the inputted number with leading spaces
* @param number the number to be formatted
* @param length the length of the returned string
* @return a String of the number with the size length
*/
static String formatNumber(int number, int length){
String numberFormatted = ""+number; //start with the number
do{
numberFormatted = " "+numberFormatted; //add spaces in front of
}while(numberFormatted.length()<length); //until it reaches desired length
return formattedNumber;
}
that example can be easily modified to be used even for Strings or whatever ^^
该示例可以轻松修改以用于字符串或其他任何东西^^
回答by suraj08
Use three loops and it will produce your required output:
使用三个循环,它将产生您所需的输出:
for (int i=1;i<6 ;i++ )
{
for(int j=5;j>i;j--)
{
System.out.print(" ");
}
for(int k=0;k<i;k++)
{
System.out.print(i);
}
System.out.print("\n");
}
回答by iskandarchacra
Your code does not produce the opposite, because the opposite would mean that you have spaces but on the right side. The right side of your output is simply empty, making you think you have the opposite. You need to include spaces in order to form the shape you want.
您的代码不会产生相反的结果,因为相反意味着您在右侧有空格。输出的右侧只是空的,让您认为您的情况正好相反。您需要包含空格以形成您想要的形状。
Try this:
尝试这个:
public class Test{
public static void main (String [] args){
for(int line = 1; line <= 5; line++){
//i decreases with every loop since number of spaces
//is decreasing
for(int i =-1*line +5; i>=1; i--){
System.out.print(" ");
}
//j increases with every loop since number of numbers
//is decreasing
for(int j = 1; j <= line; j++){
System.out.print(line);
}
//End of loop, start a new line
System.out.println();
}
}
}
You approached the problem correctly, by starting with the number of lines. Next you have to make a relation between the number of lines (the first for loop) and the for loops inside. When you want to do that remember this formula:
通过从行数开始,您正确地解决了问题。接下来,您必须在行数(第一个 for 循环)和内部的 for 循环之间建立关系。当您想这样做时,请记住以下公式:
Rate of change*line + X = number of elements on line
变化率*行 + X = 在线元素数
You calculate rate of change by seeing how the number of elements change after each line. For example on the first line you have 4 spaces, on the second line you have 3 spaces. You do 3 - 4 = -1, in other words with each line you move to, the number of spaces is decreasing by 1. Now pick a line, let's say second line. By using the formula you will have
您可以通过查看每行之后元素数量的变化来计算变化率。例如,第一行有 4 个空格,第二行有 3 个空格。你做 3 - 4 = -1,换句话说,随着你移动到每一行,空格数减少 1。现在选择一行,假设第二行。通过使用公式,您将拥有
-1(rate of change) * 2(line) + X = 3(how many spaces you have on the line you picked).
-1(变化率)* 2(线)+ X = 3(你选择的线上有多少个空格)。
You get X = 5, and there you go you have your formula which you can use in your code as you can see on line 4 in the for loop.
你得到 X = 5,然后你就有了你的公式,你可以在代码中使用它,就像你在 for 循环的第 4 行看到的那样。
for(int i = -1 * line +5; i >= 1; i--)
for(int i = -1 * line +5; i >= 1; i--)
You do the same for the amount of numbers on each line, but since rate of change is 1 i.e with every line the amount of numbers is increasing by 1, X will be 0 since the number of elements is equal to the line number.
你对每一行的数字数量做同样的事情,但由于变化率为 1,即每行数字数量增加 1,X 将为 0,因为元素数量等于行号。
for(int j = 1; j <= line; j++){
for(int j = 1; j <= line; j++){
回答by Gomy
You need 3 for loops:
您需要 3 个 for 循环:
- Upper-level loop for the actual number to be repeated and printed
- first inner level for printing the spaces
- second inner level for to print the number repeatedly
- at the end of the Upper-level loop print new line
- 要重复和打印的实际数字的上层循环
- 用于打印空间的第一个内层
- 第二内层用于重复打印数字
- 在上层循环的末尾打印新行
Code:
代码:
public void printReversedTriangle(int num)
{
for(int i=0; i<=num; i++)
{
for(int j=num-i; j>0; j--)
{
System.out.print(" ");
}
for(int z=0; z<i; z++)
{
System.out.print(i);
}
System.out.println();
}
}
Output:
输出:
1
22
333
4444
55555
666666