簡體   English   中英

如何確定用戶輸入中的最大值和最小值?

[英]How do I determine the highest and lowest value in user-entered input?

我正在嘗試獲取用戶輸入的最高和最低數字。 下面的代碼似乎在大多數情況下都可以正常工作,但是我似乎無法獲得最低值的正確數字。 我究竟做錯了什么?

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的目的是什么? 您也沒有修改lowest 您接近了,試試這個:

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

System.out.println("Highest: " + max);
System.out.println("Lowest: " + min);
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=0;
    int num=0;
    boolean test = true;

    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;
        }
        //add an initial value to 'lowest'
        if (test)
            lowest = num;
            test = false;

        if(num < lowest)
        {
            lowest = num;
        }                        
    }

    System.out.println("highest is:"+  high);
    System.out.println("lowest is: "+ lowest);
    }
}
import java.io.*;   
public class jem3
{
    public static void main(String []args)
    {
    BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in ));     
    int high;
    int lowest;
    int num;
    System.out.println("Enter number");

    num=Integer.parseInt(dataIn.readLine());
    lowest = highest = num;

    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<lowest)
            {
                lowest=num;
            }

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


    }
}

我在重新設置第一行以設置高和低之后添加了代碼,因此,如果用戶給您10個數字“ 9”,它將輸出的最小數字為“ 9”,而不是1(與高類似)。

您在把握highest的正確道路上。 您需要將類似的邏輯應用於lowest

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM