需要简单的Java模式编码方面的帮助 [英] need help on simple Java pattern coding

查看:108
本文介绍了需要简单的Java模式编码方面的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  XXXXXXXXXX 
XXXXXXXXXY
XXXXXXXXYY
XXXXXXXYYY
...

..等等

  public class ex {
public static void main(String [] args){
for(int i = 0; i <= 10; i ++){
System.out.println();
for(int j = 0; j <= 10; j ++){
if(i == 0){
System.out.print(X);
}

if(i == 1){
System.out.print(X);
if(j == 9){
System.out.print(Y);




$ b code $


}



我在最后为额外的X输出了我不想要的数据。
我认为有一个更好的方法来做到这一点,但现在不能想办法

任何帮助家伙?

$ b $尝试嵌套一个循环内的两个循环。计数到 i ,然后在外循环的每次迭代中继续计数至 10

  // 10行
for(int i = 10; i> = 0; i - ){

int j = 0;
$ b $ //打印X(10-i)
for(; j System.out.print(X);
$ b $ //打印'Y'(其中i)
((j <10; j ++)
System.out.print(Y);

System.out.println();
}


I am to make this following pattern on a for loop:

XXXXXXXXXX
XXXXXXXXXY
XXXXXXXXYY
XXXXXXXYYY
...

..and so on

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

                            if(i==1){
                                    System.out.print("X");
                                    if(j==9){
                                            System.out.print("Y");
                                    }
                            }
                    }
            }
    }

} ~

I am getting extra "X" at the end for my output that I don't want. I think there is a better way to do this but can't think of a way right now

Any help guys?

解决方案

Try nesting two loops inside one loop. Count up to i and then continue counting up to 10 on each iteration of the outer loop:

// 10 lines
for(int i = 10; i >= 0; i--){

    int j = 0; 

    // Print 'X's (10 - i of them)
    for(; j < i; j++)
        System.out.print("X");

    // Print 'Y's (i of them)
    for(; j < 10; j++)
        System.out.print("Y");

    System.out.println();
}

这篇关于需要简单的Java模式编码方面的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆