当你写你的代码。将开口和右括号{ }
保持在同一行上。
阅读你的代码更容易。
此外,你将能够看到你的代码是在哪里。
这是你的代码格式化,以尽我所能。
public class triangle
{
public static void main(String args[])
{
/*declarations*/
Scanner input= new Scanner(System.in);
/*
* No need to create this variable here.
* Just create a variable when you need it.
*
int how_many;/*number of rows/
*/
/*
* The i and j variables can be declared right within the
* for loop.
*
int i;
int j;
*/
/*
* It's println, not printIn
*/
/*prompt for input*/
System.out.println("Choissiez un nombre entier postif");
/*
* You can just create the variable right here, int how_many
*/
int how_many=input.nextInt();
/*
* This for loop is doing nothing. It runs once, thats it.
*
* You want it to loop as many times as the user entered
*
for(int i = 1 ; i <= 1 ; i++)
*/
for(int i = 1 ; i <= how_many ; i++)
{
/*
* In this nested loop you need to print out the number of
* asterisks depending on the iteration of the parent loop
*
for(int j=1; j<=i; j++)
*/
for(int j = 1 ; j <= i ; j++)
{
System.out.print("*");
}
System.out.println();
}
}
/*
* This entire for loop is not within any method.
*
* You need to bring it a few lines
*
for(i=1;i<=1;i++)
{
for(j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.printIn();
}
*/
}
或者
public class triangle
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Choissiez un nombre entier postif");
int how_many = input.nextInt();
for(int i = 1; i <= how_many; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
或者
public static void main(String args[])
{
System.out.println("Choissiez un nombre entier postif");
Scanner input = new Scanner(System.in);
int how_many = input.nextInt();
for(int i = 1; i <= how_many; i++)
System.out.println(String.format("%0" + i + "d", 0).replace("0", "*"));
input.close();
}
尽我所能得到
public class triangle
{
public static void main(String args[])
{
int n = new Scanner(System.in).nextInt();
while(n != 0)
System.out.println(String.format("%0" + n-- + "d", 0).replace("0", "*"));
}
}
你从不在你的嵌套循环中使用'how_many'。 –
并检查你的大括号('{'和'}')。你在'for'循环之前有一个大括号,这是'for'循环之前的一个语法错误 –
如果我不使用how_many应该使用什么? – Cherry