2017-02-22 12 views
0

ユーザーの入力が前のif状態を満たしていれば、次のelse if文にコードを移動させる方法がわかりません。C++でスキップする方法

#include <iostream> 

using namespace std; 

int main() 
{ 
    double input; 
    cout << "Time Calculator\n Enter the number of Seconds: " << endl; 
    cin >> input; 
    if (input < 60) 
    { 
     cout << "The time is " << input << " seconds." << endl; 
    } 
    else if (input >= 60) 
    { 
     cout << "The time is " << input << " minutes." << endl; 
    } 
    else if (input >= 3600) 
    { 
     cout << "The time is " << input << " hours." << endl; 
    } 
    else if (input >= 86400) 
    { 
     cout << "The time is " << input << " days." << endl; 
    } 
    return 0; 
} 
+0

'入力> = 3600 &&入力<86400 'または' input> = 60 && input <3600' – Smit

+0

あなたは 'if'と' else if'の目的を理解しましたか?これはどちらかのチェックのようなもので、1つのコードブロックしか実行されません。あなたは正確に何を達成しようとしていますか? –

+3

条件の順序を逆にすると、問題は、> = 86400が> 60、> = 3600などのすべての条件を満たしていることです。 –

答えて

0

これを行うための複数の方法がありますが、あなたは>= val1 and < val2のような条件を使用して2つの値の間のチェックを実装するかのチェックが起こる順序が異なっていることを確認することができます。

#include <iostream> 

using namespace std; 

int main() 
{ 
    double input; 
    cout << "Time Calculator\n Enter the number of Seconds: " << endl; 
    cin >> input; 
    if (input < 60) 
    { 
     cout << "The time is " << input << " seconds." << endl; 
    } 
    else if (input >= 60 && input < 3600) 
    { 
     cout << "The time is " << input << " minutes." << endl; 
    } 
    else if (input >= 3600 && input < 86400) 
    { 
     cout << "The time is " << input << " hours." << endl; 
    } 
    else if (input >= 86400) 
    { 
     cout << "The time is " << input << " days." << endl; 
    } 
    return 0; 
} 

または別の方法は、特定のif文が有効になると、それはif文の残りの部分とチェックしません、順序を変更することです。あなたのコード内

#include <iostream> 

using namespace std; 

int main() 
{ 
    double input; 
    cout << "Time Calculator\n Enter the number of Seconds: " << endl; 
    cin >> input; 
    if (input >= 86400) 
    { 
     cout << "The time is " << input << " seconds." << endl; 
    } 
    else if (input >= 3600) 
    { 
     cout << "The time is " << input << " minutes." << endl; 
    } 
    else if (input >= 60) 
    { 
     cout << "The time is " << input << " hours." << endl; 
    } 
    else if (input < 60) 
    { 
     cout << "The time is " << input << " days." << endl; 
    } 
    return 0; 
} 
+0

ありがとう、それは完璧に働いた! – Logan

+0

@Loganうまくいきました。それが多分助けてくれれば、それを受け入れられた答えとしてマークすることもできます。乾杯! –

3

それが入力> 36000ならば、入力> 60

をチェックしていないかどうかを確認後、条件を満たしますと入力天気を最初にチェックして、他の部分を実行しません> 86400そうでない場合は、入力> 60場合

条件が逆転している場合

#include <iostream> 

using namespace std; 

int main() 
{ 
double input; 
cout << "Time Calculator\n Enter the number of Seconds: " << endl; 
cin >> input; 
if (input < 60) 
{ 
    cout << "The time is " << input << " seconds." << endl; 
} 
else if (input >= 86400) 
{ 
    cout << "The time is " << input << " days." << endl; 
} 
else if (input >= 3600) 
{ 
    cout << "The time is " << input << " hours." << endl; 
} 
else if (input >= 60) 
{ 
    cout << "The time is " << input << " minutes." << endl; 
} 
return 0; 
} 
2

が他の方法でラウンドしてくださいここでのコードの下にしてみてください。

#include <iostream> 

using namespace std; 

int main() 
{ 
    double input; 
    cout << "Time Calculator\n Enter the number of Seconds: " << endl; 
    cin >> input; 
    if (input >= 86400) 
    { 
     cout << "The time is " << input << " days." << endl; 
    } 
    else if (input >= 3600) 
    { 
     cout << "The time is " << input << " hours." << endl; 
    } 
    else if (input >= 60) 
    { 
     cout << "The time is " << input << " minutes." << endl; 
    } 
    else 
    { 
     cout << "The time is " << input << " seconds." << endl; 
    } 
    return 0; 
} 

希望がこれです。

+0

最後の 'else if'はちょうど' else'でもかまいません。 – Bernhard

+0

ええ、そうです。 –

0

他の条件が完全に定義されていない場合はあなたの...あなたが範囲のものを設定する必要があります...それ以外の場合は仕事に行くのではありません...

if (input < 60) 
    { 
     cout << "The time is " << input << " seconds." << endl; 
    } 
    else if (input >= 60 && input < 3600)) 
    { 
     cout << "The time is " << input << " minutes." << endl; 
    } 
    else if (input >= 3600 && input < 86400)) 
    { 
     cout << "The time is " << input << " hours." << endl; 
    } 
    else if (input >= 86400) 
    { 
     cout << "The time is " << input << " days." << endl; 
    } 
関連する問題