2017-02-28 55 views
-4

因此,由於某種原因,編譯器總是說「輸入」從不關閉。但是,該程序運行並執行。我只是想知道爲什麼它一直說。這是語法。提前致謝。關於錯誤的誤解

import java.util.Scanner; 
public class Problem1 { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Enter three numbers: "); 
     int N1 = input.nextInt(); 
     int N2 = input.nextInt(); 
     int N3 = input.nextInt();  
     DoSort(N1, N2, N3); 


    } 
    public static void DoSort(int N1, int N2, int N3){ 

     if ((N1 < N2) && (N2 < N3)){ 
      System.out.println("The acsending order is " + N1 + " " + N2 + " " + N3); 
      } 
     if ((N1 > N2) && (N2 > N3)){ 
      System.out.println("The acsending order is " + N3 + " " + N2 + " " + N1); 
      } 
     if ((N1 < N2) && (N2 > N3) && (N1 < N3)){ 
      System.out.println("The acsending order is " + N1 + " " + N3 + " " + N2); 
      } 
     if ((N1 < N2) && (N2 > N3) && (N1 > N3)){ 
      System.out.println("The acsending order is " + N3 + " " + N1 + " " + N2); 
      } 

     } 
    } 

enter image description here

+2

因爲你沒有關閉它?在完成輸入後,可以使用input.close()關閉它。 – Janar

+0

如果您使用的是JDK 7或更高版本,請嘗試類似'try(Scanner input = ....){//代碼塊}。 – SMA

+0

注意:你的'DoSort'邏輯不考慮一些數字相同的情況。 –

回答

1

這只是遺憾的是沒有在您的案件,不適用,因爲你真的不想關閉System.in警告。

在該行之前添加一個@SuppressWarnings("resource")(它應該是一個快速修復)以擺脫警告。