ForLoops最大和最小的数字

时间:2015-11-26 16:27:37

标签: java eclipse

那你怎么在这里找到最大和最小的数字呢?

import java.util.Scanner;

public class Loops2 {
    public static void main (String args[]) {

        Scanner input = new Scanner(System.in);
        System.out.println("Type 10 numbers");

        for (int i = 0; i < 10; i++) {
            int number = input.nextInt();
            System.out.println(number);
        }
    }
}

4 个答案:

答案 0 :(得分:1)

假设你要做的是找到最大和最小的整数是整数数组:

public static void main (String args[]) {

   Scanner input = new Scanner(System.in);
   System.out.println("Type 10 numbers");

   //I will create the array here...

   int[] nums = new int[10];

   //assigning numbers/ints

   for(int i = 0; i < 10; i++) {
     nums[i] = input.nextInt();
   }

   //now to find the largest and smallest (in this order)

   int largest = 0;

   for(int j = 0; j < nums.length; j++)//usage of the 1-line rule :)
      if(nums[j] > largest)
         largest = nums[j];

   int smallest = largest; 

   //I'm doing this, so that it keeps checking for something lower than the largest number...

   for(int k = 0; k < nums.length; k++)//usage of the 1-line rule again :)
       if(nums[k] < smallest)
          smallest = nums[k];

   System.out.println("Largest: " + largest);
   System.out.println("Smallest: " + smallest);


}

希望这有帮助!

答案 1 :(得分:0)

    List<Integer> list = new ArrayList<Integer>();
    Scanner input = new Scanner(System.in);
    System.out.println("Type 10 numbers");

    for (int i = 0; i < 10; i++) {
        int number = input.nextInt();
        System.out.println(number);
        list.add(number);
    }
    Collections.sort(list);
    System.out.println("the small: "+ list.get(0));
    System.out.println("the big: "+list.get(list.size() - 1));

答案 2 :(得分:0)

这可能是实施它的最佳方式

import java.util.Scanner;

public class FindLargestSmallestNumber {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Type 10 numbers");
        //array of 10 numbers
        int numbers[] = new int[10];

        for (int i = 0; i < 10; i++) {
            int number = input.nextInt();
            System.out.println(number);
            numbers[i] = number ;
        }

        //assign first element of an array to largest and smallest
        int smallest = numbers[0];
        int largetst = numbers[0];

        for(int i=1; i< numbers.length; i++)
        {
            if(numbers[i] > largetst)
                largetst = numbers[i];
            else if (numbers[i] < smallest)
                smallest = numbers[i];

        }

        System.out.println("Largest Number is : " + largetst);
        System.out.println("Smallest Number is : " + smallest);
    }
}

答案 3 :(得分:-1)

import java.util.Scanner;

public class LargestSmallestNumbers {

    private static Scanner input;

    public static void main(String[] args) {
       int count,items;
       int newnum =0 ;
       int highest=0;
       int lowest =0;

       input = new Scanner(System.in);
       System.out.println("How many numbers you want to enter?");
       items = input.nextInt();

       System.out.println("Enter "+items+" numbers: ");


       for (count=0; count<items; count++){
           newnum = input.nextInt();               
           if (highest<newnum)
               highest=newnum;

           if (lowest==0)
               lowest=newnum;

           else if (newnum<=lowest)
               lowest=newnum;
           }

       System.out.println("The highest number is "+highest);
       System.out.println("The lowest number is "+lowest);
    }
}