不能将最大值运行出来

img

img

img


最后结果运行不出来

利用char数组来输入int
用for循环查询最大数。


#include<stdio.h>
int main() {
    char arrs[10];
    int max = 0;
    int x = 0;
    scanf("%s", &arrs);
    for (int i = 0; i < 10; i++) {
        x = arrs[i] - 48;
        if(x>max){
            max = x;
        }
    }
    printf("%d", max);
    
    return 0;
}

(1)for循环中,b>10不是for的第一个表达式,应该是for的第二个表达式,而且应该是b>0
(2)最后的printf函数中,b的值已经改变了,应该用a
(3)main函数主要有返回值

img

代码修改如下:


#include <stdio.h>
int main()
{
    int a,b,max=0;
    do 
    {
        printf("输入一个正整数:");
        scanf("%d",&a);
    } while (a<=0);
    b = a;
    for (;b>0;b=b/10)
    {
        if(b%10 >max)
            max = b%10;
    }
    printf("整数%d中最大的数字是%d",a,max);
    return 0;
}
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
int n,ma;
int main() {
    cin>>n;
    while(n>0){
        if(n%10>ma)ma=n%10;
        n/=10;
    } 
    cout<<ma;
    return 0;
}




#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
int n,ma;
int main() {
    cout<<"输入一个正整数:"; 
    cin>>n;
    while(n>0){
        if(n%10>ma)ma=n%10;
        n/=10;
    } 
    cout<<"整数"<<n<<"中最大的数字是"<<ma;
    return 0;
}

这个才有.