2014-02-21 42 views
1

我可以在不使用數組的情況下獲得最大數量,但無法獲得最小數量。Java程序可以在不使用陣列的情況下查找n個數字中的最大和最小數字

public static void main(String[] args) 
    { 
     int smallest=0; 
     int large=0; 
     int num; 

     System.out.println("enter the number"); 

     Scanner input=new Scanner(System.in); 

     int n=input.nextInt(); 

     for(int i=0;i<n;i++) 
     { 
      num=input.nextInt(); 
      if(num>large) 
      { 
      large=num; 
      } 
      System.out.println("the largest is:"+large); 
      //gives the largest number in n numbers 

爲最小的代碼..

 if(i==0&&num>0) 
     small=num; 
    if(num<small) 
     small=num; 
    System.out.println(small); 

     } 
+0

將'small'改爲''''後,你的代碼適用於我。 – Pshemo

+0

是的!即使我嘗試過,這確實有用。謝謝你。我沒有注意到我使用的是最小的,但並不小。 – user3168844

+0

當你寫出如果阻止最大數字時,你無法確定最小數字的條件? –

回答

2
public static void main(String[] args) { 
    int smallest = 0; 
    int large = 0; 
    int num; 
    System.out.println("enter the number");//how many number you want to enter 
    Scanner input = new Scanner(System.in); 
    int n = input.nextInt(); 
    num = input.nextInt(); 
    smallest = num; //assume first entered number as small one 
    // i starts from 2 because we already took one num value 
    for (int i = 2; i < n; i++) { 
     num = input.nextInt(); 
     //comparing each time entered number with large one 
     if (num > large) { 
      large = num; 
     } 
     //comparing each time entered number with smallest one 
     if (num < smallest) { 
      smallest = num; 
     } 
    } 
    System.out.println("the largest is:" + large); 
    System.out.println("Smallest no is : " + smallest); 
} 
3

試試這個:

int smallest = Integer.MAX_VALUE; 
for(int i=0;i<n;i++) 
{ 
    num=input.nextInt(); 
    if(num>large) 
    { 
     large=num; 
    } 
    if(num<smallest){ 
     smallest=num; 
    } 
+0

這個工程。謝謝。 – user3168844

-2

嘗試以下

public static void main(String[] args) { 
    int smallest=0; int large=0; int num; 
    System.out.println("enter the number"); 
    Scanner input=new Scanner(System.in); 
    int n=input.nextInt(); 
    num=input.nextInt(); 
    smallest = num; 
    for(int i=0;i<n-1;i++) 
     { 
      num=input.nextInt(); 
      if(num<smallest) 
      { 
       smallest=num; 
      } 
     } 
     System.out.println("the smallest is:"+smallest); 
} 
-2

提到@ user3168844代碼:試試下面的代碼:

import java.util.Scanner; 

public class LargestSmallestNum { 

    public void findLargestSmallestNo() { 

     int smallest = Integer.MAX_VALUE; 
     int large = 0; 
     int num; 

     System.out.println("enter the number"); 

     Scanner input = new Scanner(System.in); 

     int n = input.nextInt(); 

     for (int i = 0; i < n; i++) { 

      num = input.nextInt(); 

      if (num > large) 
       large = num; 

      if (num < smallest) 
       smallest = num; 

      System.out.println("the largest is:" + large); 
      System.out.println("Smallest no is : " + smallest); 
     } 
    } 

    public static void main(String...strings){ 
     LargestSmallestNum largestSmallestNum = new LargestSmallestNum(); 
     largestSmallestNum.findLargestSmalestNo(); 
    } 
} 
-2
import java.util.Scanner; 

public class LargestSmallestNum { 

    public void findLargestSmallestNo() { 

     int smallest = Integer.MAX_VALUE; 
     int large = 0; 
     int num; 

     System.out.println("enter the number"); 

     Scanner input = new Scanner(System.in); 

     int n = input.nextInt(); 

     for (int i = 0; i < n; i++) { 

      num = input.nextInt(); 

      if (num > large) 
       large = num; 

      if (num < smallest) 
       smallest = num; 

      System.out.println("the largest is:" + large); 
      System.out.println("Smallest no is : " + smallest); 
     } 
    } 

    public static void main(String...strings){ 
     LargestSmallestNum largestSmallestNum = new LargestSmallestNum(); 
     largestSmallestNum.findLargestSmalestNo(); 
    } 
} 
-1

試試這個...這個簡單的

import java.util.Scanner; 

class numbers 
{ 
    public static void main(String args[]) 
    { 
     int x, y, z; 
     System.out.println("Enter three integers "); 
     Scanner in = new Scanner(System.in); 

     x = in.nextInt(); 
     y = in.nextInt(); 
     z = in.nextInt(); 

     if (x > y && x > z) 
     System.out.println("First number is largest."); 
     else if (y > x && y > z) 
     System.out.println("Second number is largest."); 
     else if (z > x && z > y) 
     System.out.println("Third number is largest."); 
     else 
     System.out.println("Entered numbers are not distinct"); 
    } 
} 
-2
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); 
    } 
} 
-1
public class Main { 
    public static void main(String[] args) { 
     int i = 10; 
     int j = 20; 
     int k = 5; 
     int x = (i > j && i > k) ? i : (j > k) ? j : k; 
     int y = (i < j && i < k) ? i : (j < k) ? j : k; 
     System.out.println("Largetst Number : "+x); 
     System.out.println("Smallest Number : "+y); 
    } 
} 

輸出:
Largetst號碼:20
最小的數字:5

相關問題