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


#include<stdio.h> 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<stdio.h>
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 <stdio.h>

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可以输入自己所需要数目的数值,但缺陷是,用户需要再输入之前数下数值的数目。

改进方案,除去v2中出现的scan,在loop中增加if,询问用户是否还有新的数字加入,并增加新的数值,计算用户所输入的数值的总数。

原文地址:https://www.cnblogs.com/RLeeH/p/7588194.html