0

我让用户定义他要比较并打印最小值和最大值的整数数量。但是如果输入的整数之一是 0,则程序需要终止,打印到那时输入的任何数字的最大值和最小值。如果用户输入 0,我无法弄清楚如何终止程序。感谢任何帮助。

package lu.ics.Sohail.labbar;

import java.util.Scanner;

public class ovningsuppgift323 {
    public static void main (String[] args){
        Scanner input = new Scanner(System.in);
        Scanner input2= new Scanner(System.in);
                 int number;
                 int max = 0;
                 int min = 0;
                 int antal;

                 System.out.println("Amount of input numbers");
                 antal = input2.nextInt();



                       for (int x = 0; x<antal; x++){ 
                             System.out.print("Give me an integer: "); 
                             number = input.nextInt(); 





                             if (x == 0 || number > max){ 
                                 max = number;  
                             }               
                             if (x == 0 || number < min){ 
                                 min = number;  
                             }               
                       }



                       System.out.println("Highest value: " + max);
                       System.out.println("Lowest value: " + min);        


    }
}
4

1 回答 1

4

打破循环:

for (int x = 0; x<antal; x++){ 
        System.out.print("Give me an integer: "); 
        number = input.nextInt(); 

        if(number == 0) break;

         /***/
}

更多关于分支语句

于 2013-10-20T09:46:52.920 回答