仅使用的if / else最小和最大整数 [英] Smallest and Largest integers using if/else only

查看:204
本文介绍了仅使用的if / else最小和最大整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code,采取从用户的5个输入,显示最小和最大整数...它的最小值,但不是最大的,我不能找出问题......请帮助

 进口java.util.Scanner中;

公共类LargestAndSmallestIntegers {

    公共静态无效的主要(字串[] args){


        扫描仪输入=新的扫描仪(System.in);
        诠释A,B,C,D,E;
        INT小的,大的;

        的System.out.println(输入五位整数值...);



        一个= input.nextInt();

        小=一个;
        大=一个;

        B = input.nextInt();

        如果(小< B)
        {
            大= B;
        }

        其他
        {
            小= B;
        }
        C = input.nextInt();

        如果(小c为C)
        {
            大= C;
        }

        其他
        {
            小= C;
        }

        D = input.nextInt();
        如果(小< D​​)
        {
            大= D;
        }

        其他
        {
            小= D;
        }
        E = input.nextInt();
        如果(小< E)
        {
            大= E;
        }

        其他
        {
            小= E;
        }


        input.close();
         System.out.printf(%d为最小,%d为大,小,大);

    }
}
 

解决方案

 私人INT A = input.nextInt()
    B = input.nextInt(),
    C = input.nextInt()
    D = input.nextInt()
    E = input.nextInt();

私人诠释小的,大的;

小= MIN(A,B);
小=分钟(小C);
小=分钟(小四);
小=分钟(小,E);

大= MAX(A,B);
大= MAX(大,C);
大= MAX(大,D);
大= MAX(大,E);

私人诠释分钟(INT A,INT B){
    如果(A< B)返回一个失败返回B:
}

私人诠释马克斯(INT A,INT B){
    如果(A> B)返回一个失败返回B:
}
 

我想这样的作品;)

Here is my code to display smallest and largest integer by taking 5 inputs from user...it works for smallest values but not for largest and I cant figure out the problem...please help

import java.util.Scanner;

public class LargestAndSmallestIntegers {

    public static void main(String[] args) {


        Scanner input=new Scanner(System.in);
        int a,b,c,d,e;
        int small,large;

        System.out.println("Enter five integer values...");



        a=input.nextInt();

        small=a;
        large=a;

        b=input.nextInt();

        if (small<b)
        {
            large=b;
        }

        else 
        {
            small=b;
        }
        c=input.nextInt();

        if  (small<c)
        {
            large=c;
        }

        else
        {
            small=c;
        }

        d=input.nextInt();
        if (small<d)
        {
            large=d;
        }

        else
        {
            small=d;
        }
        e=input.nextInt();
        if (small<e)
        {
            large=e;
        }

        else
        {
            small=e;
        }


        input.close();  
         System.out.printf("%d is smallest and %d is largest", small,large);        

    }   
}

解决方案

private int a = input.nextInt(),
    b = input.nextInt(),
    c = input.nextInt(),
    d = input.nextInt(),
    e = input.nextInt();

private int small, large;

small = min(a,b);
small = min(small,c);
small = min(small,d);
small = min(small,e);

large = max(a,b);
large = max(large,c);
large = max(large,d);
large = max(large,e);

private int min(int a, int b) {
    if (a < b) return a else return b;
}

private int max(int a, int b) {
    if (a > b) return a else return b;
}

I think this works ;)

这篇关于仅使用的if / else最小和最大整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆