簡體   English   中英

我在讀取變量、比較它然后在使用循環時打印輸出時遇到問題

[英]I am having a problem with reading a variable, comparing it and then printing the output while using loops

我需要接受來自用戶的兩個數字,我需要將這些數字與他們之后將輸入的數字列表進行比較。 這是我的代碼:

#include <iostream>
using namespace std;
int main ()
{
int i = 1, count, num1, num2, num3, n;
bool check= false;

cout << "Please, enter the first number: ";
cin >> num1;
cout << "Please, enter your second number: ";
cin >> num2;
cout <<"-----------------------------"<<endl;
cout << "How many numbers you will enter? "<<endl;
cin >> count;

for (i; i<= count; i++)
{
    cout<<"Enter your #"<<i<<" number: ";
    do
    {
    cin >> num3;
    if (num1 == num3 && num2 == num3)
        {   
            n = 1;
            check = true;
        }
        else if (num1 == num3 && num2 != num3)
        {   
            n = 2;
            check = true;
        }
        else if (num1 != num3 && num2 == num3)
        {   
            n = 3;
            check = true;
        }
        else
        {
            n = 4;
            check = true;
        }
    }
    while (!check);
}
if (n == 1)
cout<<"Both "<<num1<<" and "<<num2<<" are found."<<endl;
else if (n == 2)
cout<<"Only the first number "<<num1<<" was found."<<endl;
else if (n == 3)
cout<<"Only the second number "<<num2<<" was found."<<endl;
else if (n == 4)
cout<<"Neither numbers "<<num1<<" and "<<num2<<" were found."<<endl;
return 0;
}

這里的問題是它永遠不會告訴我用戶輸入的兩個數字是否出現在他們輸入的數字列表中。 我可以讓它告訴我是否輸入了一個或另一個,但永遠不會同時輸入。 我該如何解決這個問題,以便如果找到兩個數字,它會在最后打印出來?

這里的問題是當您發現一個與num1num2不匹配的數字時,您會不斷重置n 如果您輸入510作為要查找的數字,然后輸入51作為要與之比較的數字,您會發現5 ,將n設置為2 ,然后當您檢查1 ,它不匹配,因此您將n設置為4 . 您需要的是兩個bool變量,一個用於如果您找到num1 ,另一個用於如果您找到num2

您也不需要do..while循環。 它不會做任何事情,因為您只循環一次。 進行以上兩個更改,代碼可以濃縮為

#include <iostream>
using namespace std;
int main ()
{
    int i = 1, count, num1, num2, num3;
    bool found1 = false, found2 = false;

    cout << "Please, enter the first number: ";
    cin >> num1;
    cout << "Please, enter your second number: ";
    cin >> num2;
    cout <<"-----------------------------"<<endl;
    cout << "How many numbers you will enter? "<<endl;
    cin >> count;

    for (; i<= count; i++)
    {
        cout<<"Enter your #"<<i<<" number: ";
        cin >> num3;
        if (num3 == num1)
            found1 = true;
        if (num3 == num2)
            found2 = true;
    }
    if (found1 && found2)
        cout<<"Both "<<num1<<" and "<<num2<<" are found."<<endl;
    else if (found1)
        cout<<"Only the first number "<<num1<<" was found."<<endl;
    else if (found2)
        cout<<"Only the second number "<<num2<<" was found."<<endl;
    else 
        cout<<"Neither numbers "<<num1<<" and "<<num2<<" were found."<<endl;
    return 0;
}

您還應該養成編寫 for 循環的習慣,例如

for (int i = 0; i < count; i++)

代替

for (; i<= count; i++)

第一種情況是“標准”循環,因為它是訪問數組的方式,因為數組的索引為 0。 它還使i范圍限定於 for 循環,並且因為在它應該位於的循環之外不需要它。 您希望盡可能將變量保持在本地狀態,這樣您的作用域就不會被不需要的變量弄得亂七八糟。

程序邏輯似乎有缺陷,即使沒有,也沒有必要使這項任務復雜化。 這是一個更簡單的方法:

#include <iostream>
using namespace std;
int main ()
{
    int count, num1, num2, num3;

    cout << "Please, enter the first number: ";
    cin >> num1;
    cout << "Please, enter your second number: ";
    cin >> num2;
    cout << "-----------------------------" <<endl;
    cout << "How many numbers you will enter? " <<endl;
    cin >> count;

    bool if_1_in = false; // if the first seen
    bool if_2_in = false; // if the second seen

    for (int i = 0; i != count; ++i)
    {
        cout << "Enter your #" << i+1 << " number: ";
        cin >> num3;
        if (!if_1_in || !if_2_in) { // if both are seen, we don't check anymore
            if (num1 == num3) // we set the flag if the first one is seen
            {   
                if_1_in = true;
            }
            if (num2 == num3) // we set the flag if the second one is seen
            {   
                if_2_in = true;
            }
        }
    }
    if (if_1_in && if_2_in) // both are seen
    cout<<"Both "<<num1<<" and "<<num2<<" are found."<<endl;
    else if (if_1_in)       // just the first one is seen
    cout<<"Only the first number "<<num1<<" was found."<<endl;
    else if (if_2_in)       // just the second one is seen
    cout<<"Only the second number "<<num2<<" was found."<<endl;
    else                    // neither one is seen
    cout<<"Neither numbers "<<num1<<" and "<<num2<<" were found."<<endl;

    return 0;
} 

結果:

Please, enter the first number: 1
Please, enter your second number: 4
-----------------------------
How many numbers you will enter?
4
Enter your #1 number: 1
Enter your #2 number: 2
Enter your #3 number: 3
Enter your #4 number: 4
Both 1 and 4 are found.


Please, enter the first number: 1
Please, enter your second number: 4
-----------------------------
How many numbers you will enter?
4
Enter your #1 number: 1
Enter your #2 number: 2
Enter your #3 number: 3
Enter your #4 number: 5
Only the first number 1 was found.


Please, enter the first number: 1
Please, enter your second number: 4
-----------------------------
How many numbers you will enter?
4
Enter your #1 number: 2
Enter your #2 number: 3
Enter your #3 number: 4
Enter your #4 number: 5
Only the second number 4 was found.


Please, enter the first number: 1
Please, enter your second number: 4
-----------------------------
How many numbers you will enter?
4
Enter your #1 number: 2
Enter your #2 number: 3
Enter your #3 number: 5
Enter your #4 number: 6
Neither numbers 1 and 4 were found.

您甚至可以使用std::unordered_set使該程序適用於更一般的情況:

#include <iostream>
#include <unordered_set>
using namespace std;
int main ()
{
    int count, num1, num2, num3;

    cout << "Please, enter the first number: ";
    cin >> num1;
    cout << "Please, enter your second number: ";
    cin >> num2;
    cout << "-----------------------------" <<endl;
    cout << "How many numbers you will enter? " <<endl;
    cin >> count;

    std::unordered_set<int> seen;

    for (int i = 0; i != count; ++i)
    {
        cout << "Enter your #" << i+1 << " number: ";
        cin >> num3;
        seen.insert(num3);
    }
    if (seen.find(num1) != seen.end() && seen.find(num2) != seen.end()) // both are seen
    cout << "Both " << num1 << " and " << num2 << " are found." <<endl;
    else if (seen.find(num1) != seen.end())                             // just the first one is seen
    cout << "Only the first number " << num1 << " was found." << endl;
    else if (seen.find(num2) != seen.end())                             // just the second one is seen
    cout << "Only the second number " << num2 << " was found." << endl;
    else                                                                // neither one is seen
    cout << "Neither numbers " << num1 << " and " << num2 << " were found." << endl;

    return 0;
} 

在 for 循環中:

for(i=0; i<=count; i++)

此外,擺脫do{}while()循環,因為它總是執行一次

所有測試( if(n==x) )必須在 for 循環中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM