首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >读取1到5之间的数字,然后打印最小值和最大值

读取1到5之间的数字,然后打印最小值和最大值
EN

Code Review用户
提问于 2017-03-10 22:51:03
回答 4查看 3.7K关注 0票数 2

只是寻找一些关于如何优化我的代码的反馈。它可以工作,但只是想得到一些关于如何改进解决方案的反馈。

需求:

  1. 提示并读取介于1到5之间的数字。重复此步骤,直到输入为1..5。
  2. 根据步骤1. 中读取的数字多次重复下列内容
    • a.读取以0结尾的整数列表。0标记输入的结束,不被视为列表的一部分。
    • b.打印列表中最大和最小的整数。
    • c.如果列表中只出现零,则打印错误消息。

代码:

代码语言:javascript
复制
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class SIMPLE_NUMBER_PROGRAM {

    public static void main(String[] args) {

        int input = prompt();
        ArrayList<Integer> inputList;
        inputList = new ArrayList();
        Integer min;
        Integer max;

        if (input == 0 && inputList.isEmpty()) {
            System.out.println("error: empty list!");
        } else {
            while (input < 1 && input != 0 || input > 5 && input != 0) {
                input = prompt();
            }
            while (input >= 1 && input <= 5) {
                inputList.add(input);
                input = prompt();
            }
            min = Collections.min(inputList);
            max = Collections.max(inputList);
            System.out.println("Min: " + min + "\n" + "Max: " + max);
        }

    }

    private static int prompt() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number: ");
        return sc.nextInt();
    }

}
EN

回答 4

Code Review用户

回答已采纳

发布于 2017-03-10 23:16:44

您实际上只需要在输入!= 0的条件下循环一个while循环。然后,当您获得输入时,如果输入介于1-5之间,则将其添加到列表中,忽略所有其他值,当输入为0时,循环退出,如果可能的话,您可以确定max,min。尝尝这个

代码语言:javascript
复制
    ArrayList<Integer> inputList = new ArrayList();
    Integer min;
    Integer max;
    int input = -1;

    while(input != 0)
    {
        input = prompt();
        if(input > 0 && input < 6)
        {
            inputList.add(input);
        }
    }

    if(inputList.size() == 0)
        System.out.print("Error, No Valid Numbers Entered");
    else
    {
        min = Collections.min(inputList);
        max = Collections.max(inputList);
        System.out.println("Min: " + min + "\n" + "Max: " + max);
    }
票数 0
EN

Code Review用户

发布于 2017-03-10 23:17:15

如果在prompt方法之外创建Scanner对象,并在要获取输入时在提示方法中使用该对象,那么每次调用prompt方法时都不会创建Scanner的新实例。

票数 1
EN

Code Review用户

发布于 2017-03-12 13:59:01

我想补充几件事。

实际上,这里不需要集合,因为检查新数字是循环主体中的新最大值还是最小值是很简单的。

SIMPLE_NUMBER_PROGRAM不是SimpleNumberProgram类名的约定,应该更改为SimpleNumberProgram。

提示方法是在每次调用时创建一个扫描器的新实例。可以对程序进行重组,以便使用相同的扫描器。

与只打印错误大小写不同,在这里抛出未经检查的异常更符合Java惯例,从而终止主线程并使程序停止。它还确保使用正确的退出代码终止进程。查看退出代码和异常。

代码语言:javascript
复制
//Name uses proper java convention
public class SimpleNumberProgram {

    //Proper convention to have constants all upper case, not class names
    //Use the same instance for every iteration of our do loop (ie don't call new each time)
    private final static Scanner SCANNER = new Scanner(System.in);

    public static void main(String[] args) {
        //Integer.MIN_VALUE and INTEGER.MAX_VALUE are static constants (Note convention)
        //on the Integer class, that denote the highest possible and lowest possible integer values
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        int input;
        do {
            System.out.println("Enter a number: ");
            input = SCANNER.nextInt();
            if (input >= 1 && input <= 5) {
                //Math max takes the max of the two inputs
                //remember we start with the Minimum possible integer value, so we will always take the input the first time.
                //Subsequent calls compare the old input to new input for the max
                max = Math.max(max, input);
                //Same for min but the inverse
                min = Math.min(min, input);
            }
        } while (input != 0);

        //Actually throw an exception here
        if (max == Integer.MIN_VALUE || min == Integer.MAX_VALUE) {
            throw new IllegalStateException("No Valid Numbers Entered");
        }
        System.out.println("Min: " + min);
        System.out.println("Max: " + max);
    }
}
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/157562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档