英文:
Printing out a list of words with the given input parameters
问题
public void drawL(int g, char h){
int i = 1;
while (i <= g){
i += 1;
if(i == g){
System.out.println(h);
System.out.println(h + "" + h);
System.out.println(h + "" + h + "" + h);
System.out.println(h + "" + h + "" + h + "" + h);
System.out.println(h + "" + h + "" + h + "" + h + "" + h);
}
}
}
英文:
The return type is void and two input parameters which are int, char. I am using a while loop because I want to solve this problem with a while loop and the method will basically print out like a ladder.
Example: drawL(6, '@') will print
@
@@
@@@
@@@@
@@@@@
@@@@@@
But without those spaces in between so it would be smooshed. I've come up with this code but the problem is that if I input 5 or 4 or 7 any number then 6 it won't print out that many char with that integer so if it was 4,'@' then it would still print out those many @ above.
public void drawL(int g, char h){
int i = 1;
while (i <= g){
i += 1;
if(i == g){
System.out.println(h);
System.out.println(h+""+h);
System.out.println(h+""+h+""+h);
System.out.println(h+""+h+""+h+""+h);
System.out.println(h+""+h+""+h+""+h+""+h);
}
}
}
答案1
得分: 1
你可能需要在这里使用嵌套的双重循环。外层循环将控制行数,内层循环将控制打印出多少个字符。
public static void drawL(int g, char h) {
for (int i=0; i < g; ++i) {
for (int j=0; j <= i; ++j) {
System.out.print(h);
}
System.out.println();
}
}
public static void main(String[] args) {
drawL(6, '@');
}
从上述对drawL()
的调用的输出如下:
@
@@
@@@
@@@@
@@@@@
@@@@@@
英文:
You should probably use a nested double loop here. The outer loop will control the row, and the inner loop will control how many characters are printed.
public static void drawL(int g, char h) {
for (int i=0; i < g; ++i) {
for (int j=0; j <= i; ++j) {
System.out.print(h);
}
System.out.println();
}
}
public static void main(String[] args) {
drawL(6, '@');
}
The output from the above call to drawL()
was this:
@
@@
@@@
@@@@
@@@@@
@@@@@@
答案2
得分: 1
如果您坚持使用while循环,可以像这样做:
class Main {
public static void drawL(int n) {
int i = 1;
while (i <= n) {
int j = 1;
while(j <= i) {
System.out.print('@');
j += 1;
}
i += 1;
System.out.println();
}
}
public static void main(String[] args) {
drawL(6);
}
}
使用Java 8的stream库替代实现`drawL()`方法。
import java.util.stream.IntStream;
public static void drawL(int n) {
IntStream.rangeClosed(1, n).forEach(i -> {
IntStream.rangeClosed(1, i).forEach(j ->
System.out.print('@'));
System.out.println();
});
}
输出:
@
@@
@@@
@@@@
@@@@@
@@@@@@
英文:
If you insist on using while loops, you could do it like this:
class Main {
public static void drawL(int n) {
int i = 1;
while (i <= n) {
int j = 1;
while(j <= i) {
System.out.print('@');
j += 1;
}
i += 1;
System.out.println();
}
}
public static void main(String[] args) {
drawL(6);
}
}
Alternative implementation of drawL()
using the Java 8
stream
library.
import java.util.stream.IntStream;
public static void drawL(int n) {
IntStream.rangeClosed(1, n).forEach(i -> {
IntStream.rangeClosed(1, i).forEach(j ->
System.out.print('@'));
System.out.println();
});
}
Output:
@
@@
@@@
@@@@
@@@@@
@@@@@@
答案3
得分: 0
可以使用“for循环”来解决这个问题:
public class Main {
public static void main(String[] args) {
drawL(4,'@');
System.out.println();
drawL(5,'@');
}
public static void drawL(int g, char h){
for(int i = 0; i < g; i++) {
for(int j = 0; j < i+1; j++) {
System.out.print(h);
}
System.out.println();
}
}
}
输出结果:
@
@@
@@@
@@@@
@
@@
@@@
@@@@
@@@@@
英文:
You can use for-loop
s to solve this:
public class Main {
public static void main(String[] args) {
drawL(4,'@');
System.out.println();
drawL(5,'@');
}
public static void drawL(int g, char h){
for(int i = 0; i < g; i++) {
for(int j = 0; j < i+1; j++) {
System.out.print(h);
}
System.out.println();
}
}
}
Output:
@
@@
@@@
@@@@
@
@@
@@@
@@@@
@@@@@
答案4
得分: 0
以下是翻译好的内容:
你也可以使用 String.repeat()
方法轻松实现这一点。
drawL(6, '@');
public static void drawL(int g, char h) {
for (int i = 1; i <= g; ++i) {
System.out.println(String.valueOf(h).repeat(i));
}
}
或者使用 while 循环
public static void drawL(int g, char h) {
int k = g;
while (k-- > 0) {
System.out.println(String.valueOf(h).repeat(g - k));
}
}
两者都会输出
@
@@
@@@
@@@@
@@@@@
@@@@@@
英文:
You can also do it very easily using the String.repeat()
method.
drawL(6,'@');
public static void drawL(int g, char h) {
for (int i=1; i <= g; ++i) {
System.out.println((h+"").repeat(i));
}
}
Or with a while loop
public static void drawL(int g, char h) {
int k = g;
while(k-- > 0) {
System.out.println((h+"").repeat(g-k));
}
}
Both print
@
@@
@@@
@@@@
@@@@@
@@@@@@
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论