-1

如果输入数字:2 输入行数:5,如何生成如下模式

2
33
444
5555
66666
public static void main(String[]args)
{
    System.out.println("Input a number(0-9):");
    Scanner sc1 = new Scanner(System.in);
    int inputNo = sc1.nextInt();
    System.out.println("Input number of lines:");
    Scanner sc2 = new Scanner(System.in);
    int inputNoLine = sc2.nextInt();
    for (i=inputNo;i<=9;i++)
    {
        count++;
        if(count>inputNoLine)
        {
            break;
        }
        System.out.println(i);
    }
}

我只能像上面那样编码。有什么建议吗?谢谢你。

4

6 回答 6

2

试试这个,我想这就是你需要的

public static void main(String[] args) {

    System.out.println("Input a number(0-9):");
    Scanner sc1 = new Scanner(System.in);
    int inputNo = sc1.nextInt();

    System.out.println("Input number of lines:");
    Scanner sc2 = new Scanner(System.in);

    int inputNoLine = sc2.nextInt();

    for(int line=1 ; line <=inputNoLine; line++) {
        for(int count=0; count<line; count++) {
            System.out.print(inputNo);
        }
        System.out.println();
        inputNo++;
    }
}
于 2013-02-21T08:23:38.920 回答
1

这应该工作

public static void main(String[] args)
    {
        System.out.println("Input a number(0-9):");
        Scanner sc1 = new Scanner(System.in);
        int inputNo = sc1.nextInt();
        System.out.println("Input number of lines:");
        Scanner sc2 = new Scanner(System.in);
        int inputNoLine = sc2.nextInt();
        int count = 1;
        for (int i=0; i < inputNoLine; i++)
        {
            for(int j = 0; j < count; j++)
            {
                System.out.print(inputNo);
            }
            count++;
            inputNo++;
            System.out.println();
        }
    }
于 2013-02-21T08:18:54.823 回答
1
public static void main(String[] args) {
    for(int i =1; i<=5 ; i++){
        for(int j = 0; j < i; j++){
            System.out.print(i);
        }
        System.out.println();

    }
}

输出:

1
22
333
4444
55555
于 2013-02-21T08:22:11.787 回答
1

你去:

public static void main(String args[]) throws IOException 
    {
        System.out.println("Input a number(0-9):");
        Scanner sc1 = new Scanner(System.in);
        int inputNo = sc1.nextInt();
        System.out.println("Input number of lines:");
        Scanner sc2 = new Scanner(System.in);
        int inputNoLine = sc2.nextInt();        
        for (int i = 0; i < inputNoLine; ++i,++inputNo) {
            for (int j = 0; j < i + 1; ++j) {
                     System.out.print(inputNo);
            }
            System.out.println();           
        }
    }
于 2013-02-21T08:23:39.193 回答
0

你需要这个代码:

public static void main(String[] args) throws ParseException,
            UnsupportedEncodingException, ClassNotFoundException {
        System.out.println("Input a number(0-9):");
        Scanner sc1 = new Scanner(System.in);
        int inputNo = sc1.nextInt();
        System.out.println("Input number of lines:");
        Scanner sc2 = new Scanner(System.in);
        int inputNoLine = sc2.nextInt();

        for (int i = 0; i < inputNoLine; i++,inputNo++) {
            for (int j = 0; j < inputNo; j++) {
                System.out.print(inputNo);
            }
            System.out.println();
        }
    }
于 2013-02-21T08:24:15.860 回答
0

假设对于 inputNo 3 和 inputNoLine 5 - 输出将是:

3
44
555
6666
77777

试试这个:

int times = 1;
    for (int i=inputNo;i<=inputNoLine;i++){
        for(int j=1;j<=times;j++) {
          System.out.print(i);
      }
     times++;
     System.out.println();
    }
于 2013-02-21T08:28:41.333 回答