gpt4 book ai didi

java - 为什么我的程序无法识别输入 "q"并中断循环?

转载 作者:行者123 更新时间:2023-12-02 13:13:05 24 4
gpt4 key购买 nike

我正在编写一个主要方法,要求用户以圆锥体的半径长度和高度的形式输入,然后调用其他 3 个数学方法来确定圆锥体底部的面积,如下所示以及它的表面积和体积。

这个想法是,您应该能够输入多组输入,并通过输入“q”表示您已完成。示例输入可以是“10 5 6 8 q”。然后,程序应该使用两组半径和高度对所有内容进行两次计算,然后中断循环。相反,它会进行 3 次计算,其中最后两次是相同的。而且它不会在无限循环后跳转到下一段代码。

outerLoop: while(true)       //Infinite loop
{
if (scan.hasNextInt()) //If next input is an integer, read it
{
radie = scan.nextInt();
height = scan.nextInt();
}
else if (scan.next().equals('q'))
{ //If it instead if "q", break the loop
break outerLoop;
}
System.out.print("r = "+radius);
System.out.println("h = "+height);
System.out.println("Bottom area: "+ area(radius));
System.out.println("Surface area: "+area(radie, height));
System.out.println("Volume: "+ volume(radius, height));

}

}

最佳答案

您正在将字符串 (scan.next()) 与字符“q”进行比较,它们永远不会相等。

关于java - 为什么我的程序无法识别输入 "q"并中断循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43870653/

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