我需要Java循环练习方面的帮助
嗨,我需要帮忙 使用将显示以下输出的循环创建java程序我需要Java循环练习方面的帮助,java,loops,for-loop,while-loop,Java,Loops,For Loop,While Loop,嗨,我需要帮忙 使用将显示以下输出的循环创建java程序 * ** *** **** ***** 新手 (i=1) **(i=2) ***(i=3) publicstaticvoidprintTriangle(int-length){ 对于(int i=1;iString s=“*”; 对于(int i=0;i
*
**
***
****
*****
新手
publicstaticvoidprintTriangle(int-length){
对于(int i=1;iString s=“*”;
对于(int i=0;i<4;i++){
字符串toPrint=s.repeat(i+1);
系统输出打印项次(toPrint);
}
试试这个:
public class Main
{
//main funtion
public static void main(String[] args) {
// variable to store number of asterisks (Total asterisks)
int asterisks = 10;
// loop until "i" less than or equal to asterisks
for (int i = 0; i <= asterisks; i++) {
//inner loop
for (int j = 0; j <= i; j++) {
//print "asterisks" until "j" less then or equal to "i".
System.out.printf("*");
}
//insert newline after coming out from inner loop
System.out.printf("\n");
}
}
}
*
**
***
****
*****
******
*******
********
*********
**********
***********
公共类星空模式
{
公共静态void main(字符串参数[])
{
//i表示行,j表示列
//行表示要打印的行数
int i,j;
对于(i=1;i要打印这样的内容,您需要两个循环,第一个循环对每行进行迭代,第二个循环或内部循环对每行添加数据,星形数与行的索引相关,因此我们检查列是否小于行:
for (int row = 1; row < 10; row++) {
for (int column = 0; column < row; column++) {
System.out.print("*");
}
System.out.println("");
}
for(int行=1;行<10;行++){
for(int column=0;column|
你需要一个循环来创建行和一个循环来创建每行的内容。试着从基础学习它,,,这里有一些链接:这能回答你的问题吗?如果不能,那么搜索java print triangle这个问题已经被问和回答了好几次,包括你还应该阅读我认为你应该提到在JDK 11(11)中,方法repeat()
被添加到类java.lang.String
。
*
**
***
****
*****
******
*******
********
*********
**********
***********
public class StarPattern
{
public static void main(String args[])
{
//i for rows and j for columns
//row denotes the number of rows you want to print
int i, j;
for(i=1; i<=5; i++) //outer loop
{
for(j=0; j<i; j++) //inner loop
{
//prints stars
System.out.print("* ");
}
//throws the cursor in a new line after printing each line
System.out.println();
}
}
}
for (int row = 1; row < 10; row++) {
for (int column = 0; column < row; column++) {
System.out.print("*");
}
System.out.println("");
}