只是寻找一些关于如何优化我的代码的反馈。它可以工作,但只是想得到一些关于如何改进解决方案的反馈。
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();
}
}发布于 2017-03-10 23:16:44
您实际上只需要在输入!= 0的条件下循环一个while循环。然后,当您获得输入时,如果输入介于1-5之间,则将其添加到列表中,忽略所有其他值,当输入为0时,循环退出,如果可能的话,您可以确定max,min。尝尝这个
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);
}发布于 2017-03-10 23:17:15
如果在prompt方法之外创建Scanner对象,并在要获取输入时在提示方法中使用该对象,那么每次调用prompt方法时都不会创建Scanner的新实例。
发布于 2017-03-12 13:59:01
我想补充几件事。
实际上,这里不需要集合,因为检查新数字是循环主体中的新最大值还是最小值是很简单的。
SIMPLE_NUMBER_PROGRAM不是SimpleNumberProgram类名的约定,应该更改为SimpleNumberProgram。
提示方法是在每次调用时创建一个扫描器的新实例。可以对程序进行重组,以便使用相同的扫描器。
与只打印错误大小写不同,在这里抛出未经检查的异常更符合Java惯例,从而终止主线程并使程序停止。它还确保使用正确的退出代码终止进程。查看退出代码和异常。
//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);
}
}https://codereview.stackexchange.com/questions/157562
复制相似问题