如何确定用户输入的输入中的最高值和最低值? [英] How do I determine the highest and lowest value in user-entered input?

查看:93
本文介绍了如何确定用户输入的输入中的最高值和最低值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取用户输入的最高和最低数字。下面的代码似乎主要是工作,但我似乎无法获得最低值的正确数字。我做错了什么?

I'm trying to get the highest and lowest numbers entered by the user. The code below seems to be mostly working but I can't seem to get the right number for the lowest value. What am I doing wrong?

import java.io.*;

public class jem3
{
    public static void main(String []args)
    {
        BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in ));        
        int high=0;
        int lowest=1;
        int num=0;
        int A=0;

        System.out.println("Enter number");

        for(int a=0;a<10;a++)
        {
            try
            {
                num=Integer.parseInt(dataIn.readLine());
            }
            catch(IOException e)
            {
                System.out.println("error");
            }

            if(num>high)
            {
                high=num;
            }

            if(num>=A)
            {
                A=lowest;
            }                        
        }

        System.out.println("highest is:"+  high);
        System.out.println("lowest is: "+A);
    }
}


推荐答案

什么 A 的目的是什么?您也没有修改最低。你就近了,试试这个:

What is the purpose of A? You're not modifying lowest either. You're close, try this:

num = ...
if ( num > max ) max = num;
if ( num < min ) min = num;

System.out.println("Highest: " + max);
System.out.println("Lowest: " + min);

这篇关于如何确定用户输入的输入中的最高值和最低值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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