小编Dra*_*olf的帖子

do-while循环不能正常工作

好吧基本上我很难找到为什么这不起作用,因为我认为它应该,并需要帮助获得正确的输出.我试过用这种格式搞砸几种方法,但没有任何作用,我真的不明白为什么.以下是说明,其后是我的来源:

说明

编写一个循环,从标准输入读取字符串,其中字符串是"land","air"或"water".当读入"xx​​xxx"(五个x字符)时,循环终止.忽略其他字符串.在循环之后,你的代码应打印出3行:第一行包含字符串"land:",后跟读入的"land"字符串的数量,第二行包含字符串"air:",后跟数字" air"字符串读入,第三个字符串由"water:"组成,后跟读入的"water"字符串数.每个字符串应打印在单独的行中.

ASSUME变量stdin的可用性,该变量引用与标准输入关联的Scanner对象.

资源:

int land = 0;
int air = 0;
int water = 0;

do
{
     String stdInput = stdin.next();
        if (stdInput.equalsIgnoreCase("land"))
        {
            land++;
        }else if (stdInput.equalsIgnoreCase("air"))
        {
            air++;
        }else if (stdInput.equalsIgnoreCase("water"))
        {
            water++;
        }
}while (stdin.equalsIgnoreCase("xxxxx") == false); // I think the issue is here, I just dont't know why it doesn't work this way
System.out.println("land: " + land);
System.out.println("air: " + air);
System.out.println("water: " + water);
Run Code Online (Sandbox Code Playgroud)

java do-while

2
推荐指数
1
解决办法
1万
查看次数

解释为什么这对C代码不起作用

参加在线C课程,当我为其中一个家庭作业问题做这个时,它并不像我想要的那样有效.它应该提示用户输入半径,然后进行计算并打印出答案.它的功能是什么都不打印,但执行得很好,当我输入一个数字时,它会打印出提示和答案.

基本上,为什么不提示先输入,因为它是在代码中写的:

int main(void)
{
    float volume, radius;
    printf("Enter the radius of the sphere: \n");
    scanf("%f", &radius);
    volume = (4.0f / 3.0f) * 3.14f * radius * radius * radius;
    printf("%.2f", volume);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c

0
推荐指数
1
解决办法
190
查看次数

标签 统计

c ×1

do-while ×1

java ×1