2014-02-10 51 views
-2

我已经看到了这篇文章的一些文章,但我似乎没有明白问题所在。我对此很新,而我无法弄清楚这一点。我必须做一个由星号组成的三角形作业。行的数量取决于用户输入和显示的整数由星号组成的三角形

* 
** 
*** 
**** 

我不太确定如何做到这一点。但是,这是我迄今(很肯定我犯了愚蠢的错误)

import java.util.Scanner; 


public class triangle { 
    public static void main(String args[]) { 
     /*declarations*/ 
     Scanner input= new Scanner(System.in); 
     int how_many;/*number of rows*/ 
     int i; 
     int j; 
     /*prompt for input*/ 
     System.out.printIn("Choissiez un nombre entier postif"); 
     how_many=input.nextInt(); 
    } 
     for(i=1;i<=1;i++){ 
      for(j=1;j<=i;j++){ 
       System.out.print("*"); 
      } 
      System.out.printIn(); 
     } 

} 
+9

你从不在你的嵌套循环中使用'how_many'。 –

+1

并检查你的大括号('{'和'}')。你在'for'循环之前有一个大括号,这是'for'循环之前的一个语法错误 –

+0

如果我不使用how_many应该使用什么? – Cherry

回答

0

当你写你的代码。将开口和右括号{ }保持在同一行上。

阅读你的代码更容易。

此外,你将能够看到你的代码是在哪里。

这是你的代码格式化,以尽我所能。

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", "*")); 
    } 
} 
2

如何:

import java.util.Scanner; 

public class triangle { 
    public static void main(final String args[]) { 
/*declarations*/ 
     Scanner input = new Scanner(System.in); 
     int how_many;/*number of rows*/ 
     int i; 
     int j; 
/*promt for input*/ 
     System.out.println("Choissiez un nombre entier postif"); 
     how_many = input.nextInt(); 
     for(i = 1; i <= how_many; i++) { 
      for(j = 1; j <= i; j++) { 
       System.out.print("*"); 
      } 
      System.out.println(); 
     } 
    } 
} 

使用JDK 7

主要算法的问题是,你需要设置i <= how_manyj <= i

+0

我没有编译器错误,或者我不会发布它。 – javamonkey79

+0

-1谨慎解释?这是对哪些问题有所了解的解决方案。 – javamonkey79

+0

这是我通过ideone.com运行我的代码时看到的错误。 [链接](http://imgur.com/CRsjlyY) – Cherry

0

下面是一个类似的项目,我有一个几年前一些代码。你必须添加一个主体,但这不应该太难。此代码正在工作并且非常明了。让我知道如果您有任何疑问:)

public class TriangleTwo 
{ 
    private int size; //size 
    private String letter; //symbol or letter 

public TriangleTwo() 
{ 

} 

public TriangleTwo(int count, String let) 
{ 
    setTriangle(count, let); 
} 

public void setTriangle(int count, String let) 
{ 
    size = count; 
    letter = let; 
} 

public String getLetter() 
{ 
    return "*"; //you can change the symbol that is printed 
} 

public String toString() 
{ 
    String output=""; 
    for(int i = size; i >= 1; i--) // transverse through col 
    { 
     for(int x = i; x >= 1; x--) // transverse through row 
     {  
      output += letter; // will print the letter that you chose 
     } 
     output += "\n"; // skips a line 
    } 
    return output + "\n"; // prints triangle 
} 
}