2015-07-11 50 views
-6

我要讓用戶輸入他想要輸入的浮點數。一旦輸入浮動數字,我要輸入一條消息,指出類似「最大數字輸入是如此「。如何從用戶輸入打印最大數字

如何識別用戶輸入的最大號碼。

#include <stdio.h> 
#include <iostream> 
#include <iomanip> 

using namespace std; 

int main() 
{ 
    float count; 
    float input; 
    float large; 

    cout << "Enter the number of floating numbers you wish to input: "; 
    scanf("%f", &count); 

    do 
    { 
     cin >> input; 
     count--; 

    } 
    while(0 < count); 

    return 0; 

} 
+2

沒你教練覆蓋'if'語句和比較操作的基本知識? –

+0

他確實覆蓋了if語句。我試過了它的多種變體,但我不確定是否將if語句包含在do-while循環或do while循環之外。我試着讓變量最大,以便我可以把最大=輸入;但每次用戶放置其浮動號碼時都會覆蓋該值。這基本上不是我想要完成的 – Jane

+2

這不是一個編碼服務,除非你自己做,否則你不會學習。相反,你需要做些什麼,解釋你期望它應該做什麼,解釋它沒有達到預期的效果,並且提出具體的問題來解決這個特定的問題。 –

回答

2

這種方法是快速,乾淨,在值基本上讀取的指定的次數,每次的數量大於當前最大時,與讀出的值代替最大。

int main() 
    { 
     int num_entries; 
     float num; 
     float max = 0; 
     cin >> num_entries; 
     while (num_entries-- > 0){ 
      cin >> num; 
      if (num > max) { 
       max = num; 
      } 
     } 
    } 
+0

這非常有幫助。謝謝你傑克瑞恩!我最終保持了它的編寫方式,但我意識到如何通過您在代碼中編寫最大值的方式來解決我的問題。謝謝! – Jane

+0

沒問題,如果你的問題解決了,隨時接受答案! –

+1

我剛剛意識到,如果所有輸入數字都是負數,此解決方案可能不起作用,您可以嘗試將最大值初始化爲最小浮點值。 –

1

這裏是例如與「for」循環

int main() 
{ 
    int conut = 0; 
    float number = 0; 
    float max_number = 0; 

    for (int i = 0; i != count; ++i) 
    { 
     cin >> number; // the user input 
     if (number > max_number) max_number = number; // if input is highter than actuall the highest number then max_number = number 
    } 

    std::cout << "max_number = " << max_number; 
} 
相關問題