我用一个循环打印了这个模式:
*
**
***
****
*****
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)
和其他类似的模式只使用一个循环,我已经完成了所有使用多个循环.
以下是"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)