简体   繁体   English

在 do-while 循环中退出程序而没有其他消息 - Java

[英]Quit program without additional messages in do-while loop - Java

my code works for what its supposed to do,我的代码适用于它应该做的事情,

do {
    System.out.print("Type a number (or -1 to stop): ");
    
    num = console.nextInt();
    if(num == -1)  
        break;
    
    if (num < min){
        min = num;
    }
    
    if(num > max){
        max = num;
    }
    
} while (true);
System.out.println("Maximum was " + max);
System.out.println("Minimum was " + min);

with the exception of when -1 is the first thing input.除了当 -1 是第一件事输入时。 if -1 is input with nothing else before the result is this:如果在结果是这样之前输入 -1 没有其他内容:

Type a number (or -1 to stop): -1
Maximum was -2147483648
Minimum was 2147483647

when it should be:什么时候应该:

Type a number (or -1 to stop): -1

I don't know how to do it without max and min being printed in every loop, making the code incorrect for general use.如果没有在每个循环中打印 max 和 min ,我不知道该怎么做,这使得代码不适合一般使用。

The easiest way to do it (in terms of the change to your existing code) is just to wrap the System.out.println statements in a conditional:最简单的方法(就现有代码的更改而言)是将System.out.println语句包装在条件中:

if (max >= min) {
  System.out.println("Maximum was " + max);
  System.out.println("Minimum was " + min);
}

This works because the condition will only be true if the loop body has executed fully, at least once.这是有效的,因为只有在循环体至少执行一次时,条件才会为真。

Alternative conditions could include:替代条件可能包括:

  • if (max.= Integer.MIN_VALUE) (or whatever you initialize it to) if (max.= Integer.MIN_VALUE) (或者你初始化它的任何东西)
  • if (min.= Integer.MAX_VALUE) (ditto initialization) if (min.= Integer.MAX_VALUE) (同上初始化)
  • if (min.= Integer.MAX_VALUE && max != Integer.MIN_VALUE)
  • Something involving a boolean flag that you set inside the loop when you enter a value other than -1当您输入非 -1 的值时,您在循环内设置的涉及 boolean 标志的内容

and many others.和许多其他人。 But I think just checking that max >= min is the neatest.但我认为只检查max >= min是最整洁的。

You need to keep a counter to know, that at least one valid input has been given.您需要保留一个计数器来知道,至少已经给出了一个有效的输入。

int counter = 0;
do {
    System.out.print("Type a number (or -1 to stop): ");
    
    num = console.nextInt();
    if(num == -1)  
        break;
    
    if (num < min){
        min = num;
    }
    
    if(num > max){
        max = num;
    }
    counter++;
} while (true);
 if (counter != 0) {
   System.out.println("Maximum was " + max);
   System.out.println("Minimum was " + min);
 }

In case counter remains 0, after ending the loop you know that no valid input has been given, therefore you don't print the max, min如果计数器保持为 0,则在结束循环后您知道没有给出有效输入,因此您不打印最大值、最小值

just add an if for your print conditions:只需为您的打印条件添加一个 if :

if (min!=-2147483648 && max!=2147483647) {
    System.out.println("Maximum was " + max);
    System.out.println("Minimum was " + min);
}

its not the right way but its the fastest i think这不是正确的方式,但它是我认为最快的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM