gpt4 book ai didi

java - do-while 循环的困惑

转载 作者:太空宇宙 更新时间:2023-11-04 11:34:35 25 4
gpt4 key购买 nike

这是我的代码

import java.util.*;
public class dowhile
{
public static void main (String [] args)
{
Scanner kb = new Scanner (System.in);
System.out.println("Enter number of # to be displayed: ");
int tim = kb.nextInt();

String hash = "#";

do
{
System.out.println(hash);
hash = hash + 1;
} while (hash.equals(5));
}
}

我对如何在用户询问后显示#的数量感到困惑。

我知道hash.equals(5)没有意义。我该如何修复这个代码?

任何人请给我建议。

最佳答案

您可以使用 tim 作为计数器,并在测试它是否大于 0 之前递减它。比如,

int tim = kb.nextInt();
do {
System.out.print("#");
} while (--tim > 0);
System.out.println();

关于java - do-while 循环的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43422511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com