[英]Printing pyramid in Java on the console
我怎样才能像这样在Java中打印金字塔
1
23
456
78910
我当前的代码如下所示:
public class T {
public static void main(String[] args) {
int i, j, num = 1;
int n = Integer.parseInt(args[0]);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.println(num);
num++;
}
System.out.println(" ");
}
}
}
如果我尝试删除声明的i
和j
,那么它会显示一个数组越界异常但是 'i' 和 'j' 正在造成问题。 我的代码应该是什么样子。
int val=1;
for(int i=0;i<6;i++){
for(int j=1;j<i;j++){
System.out.print(val);
val++;
}
System.out.print("\n");
}
最初 val 等于 1 。 在第一个for
循环中 i=0 和 j 从 1 开始增加,但是当 i=0 时,第二个 for 循环不运行。 然后你得到第一个值为 1。然后它将指向新行。
当 i=1,j 仍然为 1 时,第二个for
循环运行 1 次并打印 2,因为 val 具有增量(val++)。 当 j=2 在内部for
循环中它没有运行时,只打印 val 的新值(3)。
所以这会起作用
public static void main(String[] args) {
int num = 1;
//i is how many numbers per row
for(int i = 1; i < 5; i++){
//prints i numbers because j increases from 0 to i, incrementing num each time
for(int j = 0; j < i; j++){
System.out.print(num++);
}
System.out.println();
}
}
此代码将适用于您的目的。
现在,如果您想更好地理解 Java,请继续阅读,看看为什么编译器会在您的代码中抛出错误。 您不应该使用 stackoverflow 在不理解的情况下复制粘贴其他人的代码。 在您的代码中,您声明了i
和j
两次。 在 Java 中,您不能两次声明一个变量。 你首先在int i,j, num = 1;
然后在每个 for 循环中再次for (int i = 1; i <= lines; i++)
。 你可以通过说for(i = 1; i <= lines; i++)
来纠正这个问题。 注意int
在for
循环的第二个版本中是如何被省略的。 您可以简单地在 for 循环中为变量赋值,而不是像声明类型int i = 1
时那样创建新变量
for 循环的语法是:
for(initialization; Boolean_expression; update)
{
//Statements
}
首先执行初始化步骤,并且只执行一次。 此步骤允许您声明和初始化任何循环控制变量。 只要出现分号,您就不需要在此处声明。
至于您收到的数组越界错误,您正在尝试读取语句int n = Integer.parseInt(args[0]);
请注意 main 方法如何具有参数String[] args
。 这些称为命令行参数,如果您从命令行手动运行程序,则可以传入。 您试图读取超出 args[] 范围的 args[ args[0]
args[]
。
换句话说,如果你运行
java MyProgram one two
然后 args 包含:
[ “一二” ]
public static void main(String [] args) {
String one = args[0]; //=="one"
String two = args[1]; //=="two"
}
我想你把行数作为你唯一的参数,所以代码是
public static void main(String[] args)
{
int lines = Integer.parseInt(args[0]);
int num = 1;
for (int i = 1; i <= lines; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(num);
num++;
}
System.out.println("");
}
}
int l=1;
for (int i=0; i<5; i++)
{
for (int k=0; k<5-i; k++)
{
System.out.print(" ");
}
for (int j=0; j<(i*2)+1; j++)
{
if(j%2!=0){
System.out.print(l++);
}else {
System.out.print(" ");
}
}
System.out.println("");
}
public static void pyramid(int max) {
int num = 1;
max = 4;
for (int row = 0; row < max; row++) {
for (int column = 0; column < max; column++)
System.out.print(column <= row ? num++ : " ");
System.out.println();
}
}
import java.util.Scanner;
/**
*
* @author shelc
*/
public class PrintNumberPyramid {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the count : ");
int number = scanner.nextInt();
//enter the number of rows you want to print
pyramid(number);
}
public static void pyramid(int rows) {
int count = 1;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
System.out.print(j <= i ? count++ : " ");
}
System.out.println();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.