为什么我的代码跳过了这个循环

Why is my code skipping this loop?

本文关键字:循环 过了 我的 代码 为什么      更新时间:2023-10-16

我为我的noob问题提前道歉。我第一次接触编程是在我目前的课程中。关于这个问题,

为什么我的代码跳过第二个while循环。当我选择p或p作为输入时,我仍然会被要求输入与第一个while循环有关的信息,而不是第二个。例如分钟数,而不是白天的分钟数/晚上的分钟数。

 #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {
        char service;
        int number;
        int minutes;
        int dayMinutes;
        int nightMinutes;
        double bill;
        double const REG_FEE = 10.00;
        double const PREM_FEE = 25.00;
        double const REG_MIN = 0.20;
        double const PREM_DAY = 0.10;
        double const PREM_NIGHT = 0.05;
        cout << "Please enter your account number: ";
        cin >> number;
        cout << "Please enter your service type (regular or premium): ";
        cin >> service;
        while (service == 'r' || 'R')
    {
            cout << "How many minutes have been used for this service?: ";
            cin >> minutes;
            if (minutes < 50)
            {
                bill = REG_FEE;
                cout << fixed << showpoint << setprecision(2);
                cout << "The account number entered was: " << number << "." << endl;
                cout << "The service type entered was: " << service << "." << endl;
                cout << "You used: " << minutes << " minutes." << endl;
                cout << "Your bill is $" << bill << "." << endl;
            }
            else
            {
                bill = ((minutes - 50) * REG_MIN) + REG_FEE;
                cout << fixed << showpoint << setprecision(2);
                cout << "The account number entered was: " << number << "." << endl;
                cout << "The service type entered was: " << service << "." << endl;
                cout << "You used: " << minutes << " minutes." << endl;
                cout << "Your bill is $" << bill << "." << endl;
            }
        return 0;
    }
        while (service == 'p' || 'P')
    {
            cout << "How many minutes were used during the day?: ";
            cin >> dayMinutes;
            cout << "How many minutes were used during the night?: ";
            cin >> nightMinutes;
            if (dayMinutes > 75)
            {
                bill = ((dayMinutes - 75) * PREM_DAY) + PREM_FEE;
            }
            if (nightMinutes > 100)
            {
                bill = ((nightMinutes - 100) * PREM_NIGHT) + PREM_FEE;
                bill = bill + PREM_FEE;
                cout << fixed << showpoint << setprecision(2);
                cout << "The account number entered was: " << number << "." << endl;
                cout << "The service type entered was: " << service << "." << endl;
                cout << "You used: " << dayMinutes + nightMinutes << " minutes." << endl;
                cout << "Your bill is $" << bill << "." << endl;
            }
            else
            {
                cout << "You have entered an invalid service code.";
            }
        return 0;
    }
        return 0;
    }

正如@Vaughn-Cato所说,您的代码中有一个return 0;,它不是条件语句的一部分。我假设您可能需要将return语句插入其中一个if/else语句中。

我的最佳选择是使用break;语句而不是return 0;来只退出第一个循环。

您的条件在逻辑上不正确。您需要将其更改为:

while (service == 'r' || service == 'R')

另一个环路也是如此:

while (service == 'p' || service == 'P')

运算符==的优先级高于运算符||
你写的是:

while (service == 'r' || 'R')

因此,在您的情况下,首先计算表达式service == 'r'
假设service的值为'p',则表达式的求值结果为false
接下来,表达式false || 'R'被求值,它总是true,因为字符R求值为true。所以条件总是true,并且你总是进入第一个循环
第二个循环也有同样的问题。

因为无论输入是什么,第一个while的条件总是true

while (service == 'r' || 'R')

等同于:

while ( (service == 'r') || 'R') )

例如,输入'p'

while ( ('p' == 'r') || 'R') )
while ( (false || 'R') )     
while ( (false || true) )    //  'R' 's equivalent decimal is greater
                             //     than 0, so is always 'true'
while (true)

可能的解决方案:

while ( service == 'r' || service == 'R' )

无需使用while循环,因为代码只运行一次,并且service的值在循环内不会更改
如前所述,所有人都已解释使用service == 'r' || service == 'R'
我建议您使用if elseif而不是while

if(service == 'r' || service == 'R')
{
   // your code
}
else if(service == 'p' || service == 'P')
{
    //you code
}

在这两个条件下去除CCD_ 25
如果要多次运行一个条件,请使用循环。

如果你想多次运行它,那么继续使用while循环,只需删除循环中的return 0;

由于每隔一段时间只执行一次,因此最好使用

if (service == 'r' || service == 'R')

代替第一时间和

if (service == 'p' || service == 'P')

而不是第二个while。

while循环的思想是将一组句子执行零次或多次。