小编cod*_*005的帖子

仅使用一个循环打印图案

我用一个循环打印了这个模式:

*
**
***
****
*****

String s = "";
for (i = 1; i <= n; ++i) {
    s += "*";
    System.out.println(s);
}
Run Code Online (Sandbox Code Playgroud)

现在我想要如何仅使用一个循环打印以下模式.

1)
    *
   * *
  * * *
 * * * *
* * * * *

2)
  * * * * * 
   * * * *
    * * *
     * *
      *

3)
   1 2 3 4 5
   1 2 3 4
   1 2 3 
   1 2
   1
Run Code Online (Sandbox Code Playgroud)

和其他类似的模式只使用一个循环,我已经完成了所有使用多个循环.

java loops design-patterns for-loop

4
推荐指数
1
解决办法
2万
查看次数

使用throw进行用户定义的异常

以下是"throw use;"显示错误的代码.为什么?如何将throw用于用户定义的异常?举个例子?

class use extends Exception{
public String toString() {
    return "too many exceptions";
}
}   
class user{
public static void main(String s[]) {
    int i=3;
    try {
        if(i>1)
            throw use;
    }
    catch(use e) {
        System.out.println(e.toString());
    }
    finally{
        System.out.println("program executed successfully!");
    }

}
}
Run Code Online (Sandbox Code Playgroud)

java exception throw

0
推荐指数
1
解决办法
8233
查看次数

列出所有通过编码实现接口

是否可以列出任何类的所有实现的接口.

java interface

-3
推荐指数
1
解决办法
88
查看次数

标签 统计

java ×3

design-patterns ×1

exception ×1

for-loop ×1

interface ×1

loops ×1

throw ×1