choose the max from numbers, use scanf and if else (v1:21.9.2017,v2:23.9.2017)

4017 ワード


#include int main(){ int a,b,c,max; printf(""); scanf("%d",&a); printf(""); scanf("%d",&b); printf(""); scanf("%d",&c); if(a>b){ max = a; } else{ max = b; } if(max<c){ max = c; } printf("MAX:%d
",max); }

I will use another scanf to let user input the number they want to input to choose the max value.
 
#include
int main(){ 
    int a,b,c,max;
    printf("       : ");
    scanf("%d",&a);
    printf("       : ");
    scanf("%d",&b); printf("       : "); scanf("%d",&c); if(a>b){ max = a; } else{ max = b; } if(max<c){ max = c; } printf("MAX:%d
",max); }

I will use another scanf to let user input the number they want to input to choose the maxvalue.
#include 

int main() {
    printf("       (   2):");
    int n,a,b;                                    //n      
    scanf("%d",&n);                                //a b                
    int e = n - 2;                                //e   for loop     
    if(n<=2){
        printf("       !!!");                //         2  ,    
    }else{
        printf("    1   :");
        scanf("%d",&a); printf("    2   :"); scanf("%d",&b); for(int i = 1; i <= e; i++){ int x = i + 2; printf("    %d   :",x); scanf("%d",&b); if(a < b){ //  a b   ,  a b    a = b; //        a      } } printf("      : %d",a); } }

バージョンを更新すると、Userは自分の必要な数の数値を入力することができますが、欠点は、ユーザーが前の数の数値を入力する必要があることです.
改良案は、v 2に現れるscanを除去し、loopにifを追加し、ユーザーに新しい数字が追加されているかどうかを尋ね、新しい数値を追加し、ユーザーが入力した数値の総数を計算する.