如何根据计数生成模式?

时间:2013-02-21 08:15:52

标签: java design-patterns

如果生成如下的模式 输入数量: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);
    }
}

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

6 个答案:

答案 0 :(得分: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++;
    }
}

答案 1 :(得分: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();
        }
    }

答案 2 :(得分: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

答案 3 :(得分: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();           
        }
    }

答案 4 :(得分: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();
        }
    }

答案 5 :(得分: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();
    }