Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我是否必须在代码的开头声明所有变量才能正确输出? 公共类分配1{ /*这段代码将打印出一个三角形 朝向左侧,并将增加1 asterik每次有新线时*/ 公共静态void main(字符串[]args){ //声明整数变量x和y int x,y; //如果1大于或等于11,则运行for循环并添加1。 对于(x=1;x_Java - Fatal编程技术网

Java 我是否必须在代码的开头声明所有变量才能正确输出? 公共类分配1{ /*这段代码将打印出一个三角形 朝向左侧,并将增加1 asterik每次有新线时*/ 公共静态void main(字符串[]args){ //声明整数变量x和y int x,y; //如果1大于或等于11,则运行for循环并添加1。 对于(x=1;x

Java 我是否必须在代码的开头声明所有变量才能正确输出? 公共类分配1{ /*这段代码将打印出一个三角形 朝向左侧,并将增加1 asterik每次有新线时*/ 公共静态void main(字符串[]args){ //声明整数变量x和y int x,y; //如果1大于或等于11,则运行for循环并添加1。 对于(x=1;x,java,Java,我注意到的第一件事是,在不需要花括号的地方放上花括号。你只需要在函数、循环和if语句中使用的代码周围放上花括号。使用eclipse之类的程序跟踪花括号是个好主意,另外,它还会警告你其他错误 其次,您必须在使用变量n之前声明它,以及printStars()方法。您必须在k=2*n-2;使用它之前声明n变量,我是否应该创建另一个公共类并从我的main调用它?您的代码不完整:您调用printStars(),但没有这样的函数。请提供其余函数。然后解释在调试程序中单步执行代码时看到的内容。 public

我注意到的第一件事是,在不需要花括号的地方放上花括号。你只需要在函数、循环和if语句中使用的代码周围放上花括号。使用eclipse之类的程序跟踪花括号是个好主意,另外,它还会警告你其他错误


其次,您必须在使用变量n之前声明它,以及printStars()方法。

您必须在
k=2*n-2;

使用它之前声明
n
变量,我是否应该创建另一个公共类并从我的main调用它?您的代码不完整:您调用
printStars()
,但没有这样的函数。请提供其余函数。然后解释在调试程序中单步执行代码时看到的内容。
public class Assignment1{
    /* This piece of the code will print out a triangle 
       orientated to the left and will increase by one
       asterik every time there is a new line */

    public static void main(String[] args){

        // declaring the integer variables x and y 
        int x,y;                   

        // if 1 is greater than or equal to 11, runt he for loop and add 1.
        for(x=1; x<=11; x++){

            // if x is greater than y print add 1 and print out the next lines
            for(y=1; y<x; y++){

                // prints out the actual * in the code.
                System.out.print("*");  
            }        
            System.out.println();   
        }

        // number of spaces
        int i, j, k=2*n-2;

        // outer loop to handle number of rows
        // n in this case
        for(i=0; i<n; i++){

            // inner loop to handle number spaces
            // values changing acc. to requirement
            for(j=0; j<k; j++){
                // printing spaces
                System.out.print(" ");
            }

            // decrementing k after each loop
            k = k - 2;

            // inner loop to handle number of columns
            // values changing acc. to outer loop
            for(j=0; j<=i; j++){

                // printing stars
                System.out.print("* ");
            }

            // ending line after each row
            System.out.println();
        }
        int n = 5;
        printStars(n);
    }
}