以下是我到目前为止:
我不知道如何排除0作为最小数字。赋值要求0为退出号,因此我需要在min字符串中出现除0以外的最小数字。有什么想法吗?
int min, max;
Scanner s = new Scanner(System.in);
System.out.print("Enter a Value: ");
int val = s.nextInt();
min = max = val;
while (val != 0) {
System.out.print("Enter a Value: ");
val = s.nextInt();
if (val < min) {
min = val;
}
if (val > max) {
max = val;
}
};
System.out.println("Min: " + min);
System.out.println("Max: " + max);
答案 0 :(得分:3)
您只需要跟踪这样的最大值:
int maxValue = 0;
然后,当您遍历数字时,如果maxValue大于maxValue,请继续将maxValue设置为下一个值:
if (value > maxValue) {
maxValue = value;
}
对minValue重复相反的方向。
答案 1 :(得分:2)
这是一个可能的解决方案:
public class NumInput {
public static void main(String [] args) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
Scanner s = new Scanner(System.in);
while (true) {
System.out.print("Enter a Value: ");
int val = s.nextInt();
if (val == 0) {
break;
}
if (val < min) {
min = val;
}
if (val > max) {
max = val;
}
}
System.out.println("min: " + min);
System.out.println("max: " + max);
}
}
(不确定使用int还是双重思考)
答案 2 :(得分:2)
更好
public class Main {
public static void main(String[] args) {
System.out.print("Enter numbers: ");
Scanner input = new Scanner(System.in);
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;
while (true) {
if ( !input.hasNextDouble())
break;
Double num = input.nextDouble();
min = Math.min(min, num);
max = Math.max(max, num);
}
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
}
}
答案 3 :(得分:1)
//for excluding zero
public class SmallestInt {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
System.out.println("enter number");
int val=input.nextInt();
int min=val;
//String notNull;
while(input.hasNextInt()==true)
{
val=input.nextInt();
if(val<min)
min=val;
}
System.out.println("min is: "+min);
}
}
答案 4 :(得分:1)
这就是我所做的,它可以尝试和它一起玩。它计算总数,平均值,最小值和最大值。
public static void main(String[] args) {
int[] score= {56,90,89,99,59,67};
double avg;
int sum=0;
int maxValue=0;
int minValue=100;
for(int i=0;i<6;i++){
sum=sum+score[i];
if(score[i]<minValue){
minValue=score[i];
}
if(score[i]>maxValue){
maxValue=score[i];
}
}
avg=sum/6.0;
System.out.print("Max: "+maxValue+"," +" Min: "+minValue+","+" Avarage: "+avg+","+" Sum: "+sum);}
}
答案 5 :(得分:1)
这里你需要跳过int 0 如下:
val = s.nextInt();
if ((val < min) && (val!=0)) {
min = val;
}
答案 6 :(得分:0)
System.out.print("Enter a Value: ");
val = s.nextInt();
此行放在最后。整个代码如下: -
public static void main(String[] args){
int min, max;
Scanner s = new Scanner(System.in);
System.out.print("Enter a Value: ");
int val = s.nextInt();
min = max = val;
while (val != 0) {
if (val < min) {
min = val;
}
if (val > max) {
max = val;
}
System.out.print("Enter a Value: ");
val = s.nextInt();
}
System.out.println("Min: " + min);
System.out.println("Max: " + max);
}
答案 7 :(得分:0)
我试图通过处理用户输入异常来优化解决方案。
public class Solution {
private static Integer TERMINATION_VALUE = 0;
public static void main(String[] args) {
Integer value = null;
Integer minimum = Integer.MAX_VALUE;
Integer maximum = Integer.MIN_VALUE;
Scanner scanner = new Scanner(System.in);
while (value != TERMINATION_VALUE) {
Boolean inputValid = Boolean.TRUE;
try {
System.out.print("Enter a value: ");
value = scanner.nextInt();
} catch (InputMismatchException e) {
System.out.println("Value must be greater or equal to " + Integer.MIN_VALUE + " and less or equals to " + Integer.MAX_VALUE );
inputValid = Boolean.FALSE;
scanner.next();
}
if(Boolean.TRUE.equals(inputValid)){
minimum = Math.min(minimum, value);
maximum = Math.max(maximum, value);
}
}
if(TERMINATION_VALUE.equals(minimum) || TERMINATION_VALUE.equals(maximum)){
System.out.println("There is not any valid input.");
} else{
System.out.println("Minimum: " + minimum);
System.out.println("Maximum: " + maximum);
}
scanner.close();
}
}