0

New to Java and I need to write a program that prints out the numbers from one to 100 in 10 rows and columns, incrementing first down the column using nested loops. Any help would be greatly appreciated.

This is what I have tried, but it's not printing the right numbers.

for ( int i = 1 ; i <= 10; i++ ) {
    for ( int j = 1 ; j <= 10; j++ ) { 
        System.out.printf("%4d", i*j ); 
    } 
    System.out.println(); 
}
4

3 回答 3

0
public class App {
    public static void main(String[] args) {
        int count = 1;
        for (int i = 1; i <= 10; i++) {
            for (int k = 1; k <= i; k++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= 10; j++) {
                System.out.print(" " + count);
                count++;
            }
            System.out.println();
        }
    }
}

将名称 App.java 命名为文件,在您的 cmd 行“javac App.java”然后运行“java App”

于 2013-10-06T00:19:59.493 回答
0

使用两个嵌套循环这将是

public static void main(String[] args) {
    for(int i=0; i<10; i++){
        for(int j=1; j<=10; j++){
            System.out.print(i*10+j + "\t");
        }
        System.out.print("\n");
    }
}
于 2013-10-06T00:21:36.033 回答
0
public static void main(String[] args) {
    // TODO code application logic here
    int count=1;
    for (int a=1;a<=100;a++)
    {
        if (count<10)
        {
        System.out.print(String.valueOf(a+"\t"));
        count++;
        }
        else
        {
            System.out.println(a);
            count=1;
        }
    }
}
于 2013-10-06T00:11:04.667 回答