gpt4 book ai didi

java - 在循环迭代中更改循环条件

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

我试图通过逐字符解析字符串并找到整个浮点或整数来识别浮点与整数。如果我找到了“.”那么我不想在我的循环条件中使用它。

我想做这样的事情:

do
{
if((char) nextChar == '.')
reachedDot = true;

integer += (char) nextChar;
readChar();
} while(// if(reachedDot){
// Character.isDigit(nextChar) }
// else { (Character.isDigit(nextChar) || nextChar == '.')});

最佳答案

三元运算符应该可以解决问题:

do
{
if((char) nextChar == '.')
reachedDot = true;

integer += (char) nextChar;
readChar();
} while (reachedDot ? Character.isDigit(nextChar) : ((Character.isDigit(nextChar) || nextChar == '.'));

当然,这可以使用一些 boolean 代数来简化,因为 Character.isDigit(nextChar) 应该始终使循环继续:

do
{
if((char) nextChar == '.')
reachedDot = true;

integer += (char) nextChar;
readChar();
} while (Character.isDigit(nextChar) || (!reachedDot && nextChar == '.'));

关于java - 在循环迭代中更改循环条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22949799/

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