簡體   English   中英

為什么“ else”語句出現在輸出中?

[英]Why is the 'else' statement appearing in the output?

我是C ++的新手,當我遇到問題時正在嘗試使用該程序。 那不是技術性的。 該程序運行正常。

#include<iostream>    
int main()
{
    using namespace std;
    int num1, num2;
    char ch;
    cout << "Enter the two integers: ";
    cin >> num1 >> num2;
    cout << "Choose one of the following arithmetic operators: " << endl;
    cout << "+" << endl;
    cout << "-" << endl;
    cout << "*" << endl;
    cout << "/" << endl;
    cin >> ch;
    if (ch == '+')
        cout << "The sum of the two numbers is: " << num1 + num2 << endl;
    if (ch == '-')
        cout << num2 << " subtracted from " << num1 << " is " << num1 - num2 << endl;
    if (ch == '*')
        cout << "When multiplied: " << num1*num2 << endl;
    if (ch == '/')
        cout << num1 << " divided by " << num2 << " will be " << num1 / num2 << endl;
    else
        cout << "Wrong input. Try again.";
    return 0;
}

現在是問題所在。 當滿足if語句以及提供的計算時,輸出還將顯示else語句中的語句。 我嘗試將else編輯為:-

else if(ch=!'+','-','*','/')

if(ch=!'+','-','*','/')

但它們似乎都不起作用。 在所有情況下,“輸入錯誤。請重試。” 不知何故找到了輸出的方式。 我要去哪里錯了?

您的整個結構必須為if - else if - else ,如下所示:

if(ch=='+')
    cout<<"The sum of the two numbers is: "<<num1+num2<<endl;
else if(ch=='-')
    cout<<num2<<" subtracted from "<<num1<<" is "<<num1-num2<<endl;
else if(ch=='*')
    cout<<"When multiplied: "<<num1*num2<<endl;
else if(ch=='/')
    cout<<num1<<" divided by "<<num2<<" will be "<<num1/num2<<endl;
else 
    cout<<"Wrong input. Try again.";

但這是做事的最好方法嗎? 可能不是。 由於您只是在測試以查看ch等於什么,因此實際上可以使用另一個內置的C ++構造,即switch語句:

switch (ch) {
  case '+':
    cout << "The sum of the two numbers is: " << num1+num2 << endl;
    break;
  case '-':
    cout << num2 << " subtracted from " << num1 << " is " << num1-num2 << endl;
    break;
  case '*':
    cout << "When multiplied: " << num1*num2 << endl;
    break;
  case '/':
    cout << num1 << " divided by " << num2 << " will be " << num1/num2 << endl;
    break;
  default:
    cout << "Wrong input. Try again." << endl;
    break;
}

在您的原始代碼中, else僅適用於最后的if語句。 您需要鏈接您的條件:

if(ch=='+')
    cout<<"The sum of the two numbers is: "<<num1+num2<<endl;
else if(ch=='-')
    cout<<num2<<" subtracted from "<<num1<<" is "<<num1-num2<<endl;
else if(ch=='*')
    cout<<"When multiplied: "<<num1*num2<<endl;
else if(ch=='/')
    cout<<num1<<" divided by "<<num2<<" will be "<<num1/num2<<endl;
else 
    cout<<"Wrong input. Try again.";

但是,最好使用switch語句:

switch (ch) {
case '+':
    cout<<"The sum of the two numbers is: "<<num1+num2<<endl;
    break;
case '-';
    cout<<num2<<" subtracted from "<<num1<<" is "<<num1-num2<<endl;
    break;
case '*':
    cout<<"When multiplied: "<<num1*num2<<endl;
    break;
case '/':
    cout<<num1<<" divided by "<<num2<<" will be "<<num1/num2<<endl;
    break;
default:
    cout<<"Wrong input. Try again."; 
}

ch不等於/時, else部分將始終執行,因為else部分僅與if(ch=='/')匹配。可能要使用if else梯形圖或切換大小寫。

“其他語句”與最后的“如果語句”鏈接。 因此,如果非最后一個if語句變為true,則最后一個if語句變為false,這意味着對else語句的真實評價。 您可以將if語句更改為“ else if”(不要修改第一個if語句)。

暫無
暫無

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

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