Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用(while)循环,以及如果符合所有代码的条件,将其放置在何处 在C++上,我试图制作一个程序,除非用户按下999,否则它将重复接受用户的输入。此外,如果输入不能被5或20整除,那么我要求它输出“必须输入可被5或20整除”。我想继续这样做,直到他们输入-999,但我不知道我的while循环放在哪里,或者如果没有输入,应该放什么。我也不知道该把“完成后输入-999离开”放在哪里,同时让它可以一直使用,而不仅仅是开始。谢谢你 #include <iostream> using namespace std; int main(void) { int amountEntered; cout << "Please enter the amount of money you would like to dispense (must be in 20's or 5's)" << endl; cout << "when finished, enter -999 to leave" << endl; if (amountEntered == -999) { cout << "Thank you for doing business." << endl; } cin >> amountEntered; if (amountEntered % 20 == 0) { cout << amountEntered / 20 << endl; } else { if (amountEntered % 5 == 0) { cout << amountEntered / 5 << endl; } else { cout << "You must enter multiples of twenty or five only!" << endl; } } { while (amountEntered != -999); while (amountEntered % 5 == 0); else { if (amountEntered % 5 != 0) { cout << "You must enter multiples of twenty or five only!" << endl; } } while (amountEntered % 20 == 0); } if (amountEntered % 20 != 0); { cout << "You must enter a number divisible by 20 or 5!" << endl; } if (amountEntered = -999) { cout << "Thank you for doing business." << endl; } } #包括 使用名称空间std; 内部主(空) { 国际货币基金组织; cout_C++_While Loop - Fatal编程技术网

如何使用(while)循环,以及如果符合所有代码的条件,将其放置在何处 在C++上,我试图制作一个程序,除非用户按下999,否则它将重复接受用户的输入。此外,如果输入不能被5或20整除,那么我要求它输出“必须输入可被5或20整除”。我想继续这样做,直到他们输入-999,但我不知道我的while循环放在哪里,或者如果没有输入,应该放什么。我也不知道该把“完成后输入-999离开”放在哪里,同时让它可以一直使用,而不仅仅是开始。谢谢你 #include <iostream> using namespace std; int main(void) { int amountEntered; cout << "Please enter the amount of money you would like to dispense (must be in 20's or 5's)" << endl; cout << "when finished, enter -999 to leave" << endl; if (amountEntered == -999) { cout << "Thank you for doing business." << endl; } cin >> amountEntered; if (amountEntered % 20 == 0) { cout << amountEntered / 20 << endl; } else { if (amountEntered % 5 == 0) { cout << amountEntered / 5 << endl; } else { cout << "You must enter multiples of twenty or five only!" << endl; } } { while (amountEntered != -999); while (amountEntered % 5 == 0); else { if (amountEntered % 5 != 0) { cout << "You must enter multiples of twenty or five only!" << endl; } } while (amountEntered % 20 == 0); } if (amountEntered % 20 != 0); { cout << "You must enter a number divisible by 20 or 5!" << endl; } if (amountEntered = -999) { cout << "Thank you for doing business." << endl; } } #包括 使用名称空间std; 内部主(空) { 国际货币基金组织; cout

如何使用(while)循环,以及如果符合所有代码的条件,将其放置在何处 在C++上,我试图制作一个程序,除非用户按下999,否则它将重复接受用户的输入。此外,如果输入不能被5或20整除,那么我要求它输出“必须输入可被5或20整除”。我想继续这样做,直到他们输入-999,但我不知道我的while循环放在哪里,或者如果没有输入,应该放什么。我也不知道该把“完成后输入-999离开”放在哪里,同时让它可以一直使用,而不仅仅是开始。谢谢你 #include <iostream> using namespace std; int main(void) { int amountEntered; cout << "Please enter the amount of money you would like to dispense (must be in 20's or 5's)" << endl; cout << "when finished, enter -999 to leave" << endl; if (amountEntered == -999) { cout << "Thank you for doing business." << endl; } cin >> amountEntered; if (amountEntered % 20 == 0) { cout << amountEntered / 20 << endl; } else { if (amountEntered % 5 == 0) { cout << amountEntered / 5 << endl; } else { cout << "You must enter multiples of twenty or five only!" << endl; } } { while (amountEntered != -999); while (amountEntered % 5 == 0); else { if (amountEntered % 5 != 0) { cout << "You must enter multiples of twenty or five only!" << endl; } } while (amountEntered % 20 == 0); } if (amountEntered % 20 != 0); { cout << "You must enter a number divisible by 20 or 5!" << endl; } if (amountEntered = -999) { cout << "Thank you for doing business." << endl; } } #包括 使用名称空间std; 内部主(空) { 国际货币基金组织; cout,c++,while-loop,C++,While Loop,下面是一些伪代码来说明: while true get input if input is -999 (or other conditions) break out of loop else // rest of code goes here 因此,基本上,将整个过程包装在while true循环中,然后在满足某些条件时使用条件逻辑中断循环。下面是一些伪代码来说明: while true get i

下面是一些伪代码来说明:

while true
    get input
        if input is -999 (or other conditions)
            break out of loop
        else
            // rest of code goes here

因此,基本上,将整个过程包装在
while true
循环中,然后在满足某些条件时使用条件逻辑中断循环。

下面是一些伪代码来说明:

while true
    get input
        if input is -999 (or other conditions)
            break out of loop
        else
            // rest of code goes here
因此,基本上,将整个过程包装在
while true
循环中,然后在满足某些条件时使用条件逻辑中断循环

在C++上,我试图制作一个程序,除非用户按下999,否则它将重复接受用户的输入。 此外,如果输入不能被5或20整除,那么我要求它输出“必须输入可被5或20整除”

#包括//标准::数值限制
#包括
int main()
{
为了(;){//永远
int值;
而(std::cout>value)| value!=-999&&value%5!=0&&value%20!=0)
//^^提取失败或值不符合要求
{
标准:cerr
在C++上,我试图制作一个程序,除非用户按下999,否则它将重复接受用户的输入。
此外,如果输入不能被5或20整除,那么我要求它输出“必须输入可被5或20整除”

#包括//标准::数值限制
#包括
int main()
{
为了(;){//永远
int值;
而(std::cout>value)| value!=-999&&value%5!=0&&value%20!=0)
//^^提取失败或值不符合要求
{
标准:cerr
我想继续这样做,直到他们输入-999,但我不知道在哪里放置我的while循环,或者如果未输入该循环,则放置什么。我也不知道在使其符合所有时间(而不仅仅是开始)的条件时,在哪里放置“完成后输入-999以离开”

也许你想解决这个问题。首先,你会知道这个循环的“结束条件”是用户键入了-999。因此你会希望你的循环看起来像这样

有了它,我们所需要的就是放置用户输入的捕获。一个在开始,一个在while循环结束之前。

有几种方法可以解决此问题,这取决于您希望如何设计代码。以下是我的方法:

#包括
作废申请(整数和金额)
{
标准::cout
我想继续这样做,直到他们输入-999,但我不知道在哪里放置我的while循环,或者如果未输入该循环,则放置什么。我也不知道在使其符合所有时间(而不仅仅是开始)的条件时,在哪里放置“完成后输入-999以离开”

也许你想解决这个问题。首先,你会知道这个循环的“结束条件”是用户键入了-999。因此你会希望你的循环看起来像这样

有了它,我们所需要的就是放置用户输入的捕获。一个在开始,一个在while循环结束之前。

有几种方法可以解决此问题,这取决于您希望如何设计代码。以下是我的方法:

#包括
作废申请(整数和金额)
{


std::cout太复杂了。@Swardfish这怎么“复杂”?我有点喜欢它不太复杂,只是高水平。从教学角度来说,更有益于解释一种可用于其他场景的模式,而不是重写他们的代码。“教一个人钓鱼,他吃一辈子”太复杂了。@Swardfish这是怎么“复杂”的?!我有点喜欢它,不太复杂,只是高层次。从教学角度来说,更有益于解释一种可用于其他场景的模式,而不是重写他们的代码。“教一个人钓鱼,他吃一辈子”你的代码中有一些非常有趣的
while
-循环。还有一个
else
没有匹配的
if
。你的代码中有一些非常有趣的
while
-循环。而一个
else
没有匹配的
if
则不做要求做的事情,做的部分在功能上等同于但不做We’我们比已经发布的另一个解决方案更复杂,您拒绝了该解决方案,因为它“太复杂”lolOh,你从
-999
中删除了
-
一段时间reason@LightnessRacesinOrbit再看一次。@LightnessRacesinOrbit执行现在要求的操作。不,它不执行。不执行要求的操作,执行的部分在功能上等同于已发布的另一个解决方案,但比您拒绝的解决方案更复杂“太复杂了”lolOh,你从
-999
中删除了
-
一段时间reason@LightnessRacesinOrbit再看一遍。@LightnessRacesinOrbit现在按要求做了。不,它没有。
while userinput != -999
    // do something here
end while loop
get userinput
while userinput != -999
    // do something here
    get userinput
end while loop
#include <iostream>

void Request(int& amount)
{
    std::cout << "Please enter the amount of money you would like to dispense (must be in 20's or 5's)" << std::endl;
    std::cout << "when finished, enter -999 to leave" << std::endl;
    std::cout << ">";
    std::cin >> amount;
}

int main(void)
{
    int amount = 0;

    for (Request(amount); amount != -999; Request(amount))
    {
        // Since 20 is a multiple of 5, just check if its divisble by 5 will do 
        if (amount % 5)
        {
            std::cout << "You must enter multiples of twenty or five only!" << std::endl;
            continue;
        }

        // Otherwise print out (or do stuff here)
        std::cout << amount % 5 << std::endl;
    }

    std::cout << "Thank you for doing business." << std::endl;
}