就像斐波那契,我不知道这个代码

时间:2018-07-13 10:31:42

标签: java

import java.util.*;
public class MyClass {
    public static void main(String[] args) {  
        Scanner wew = new Scanner(System.in);
        int b = 0;
        int i = 0;
        System.out.println("Enter a number of row:");
        b=wew.nextInt();
        System.out.println("Enter a number of range:");
        i=wew.nextInt();    

        for (int c = 1; c <= b; c++)
        {
            for (int g = 1; g <= i; g++){

                System.out.println(g+"");
            }
            for ( int g = c-1; g >= 1; g--)
            {
                System.out.println(g+" ");
            }
            System.out.println();
        }

    }
}

代码仍然是我所不知道的。就像斐波那契,我知道我的输出与预期结果相差太远

预期结果:

Please enter a number range: 4
Please enter a number rows: 4

1111 2111 3111 4111
1211 1311 1411 1511
1131 1141 1151 1161
1114 1115 1116 1117

1 个答案:

答案 0 :(得分:0)

这是Kotlin代码,它可以完成您想要的操作(对我来说,以这种方式进行开发更容易):

$0

这是Java等效项:

import java.util.*

fun main(args: Array<String>) {
    val input = Scanner(System.`in`)

    print("Please enter a number range: ")
    val numOfCols = input.nextInt()

    print("Please enter a number of rows: ")
    val numOfRows = input.nextInt()

    for (i in 1..numOfRows) {
        for (j in 1..numOfCols){
            for (k in 1..numOfCols){
                if (k == i) {
                    print(k+j-1)
                } else {
                    print(1)
                }
            }
            print(" ")
        }
        println("")
    }
}