2017-06-25 22 views
0

如何從用戶獲取多個輸入並根據輸入輸出結果。 例如,在下面的編解碼器中,我想從用戶處取多個11 digit number,並根據給定的if else條件爲用戶輸入每個輸入的結果Y或N.如何在java中讀取用戶對數組操作的多個輸入

public class Test { 

public static void main(String args[]){ 

    Scanner sc = new Scanner(System.in); 
    System.out.println("Enter 11 digit number"); 
    String s = sc.nextLine(); 
    int[] a=new int[12]; 
    for(int i=0;i<s.length();i++){ 
     a[i] = Character.getNumericValue(s.charAt(i)); 
    } 

    int result = a[0] + a[1] *3+ a[2] * 7 + a[3]*9 + a[4] + a[5] * 3 + a[6] * 7 + a[7] * 9 + a[8] + a[9] * 3 + a[10]; 
    if(result%10==0){ 
     System.out.println("Y"); 
     } 
    else{ 
     System.out.println("N"); 
     } 

    } 
} 

任何幫助,將不勝感激。

+0

閱讀while循環。或者如果重複次數是固定的:只需編寫另一個封閉for循環。提示:有人可以幫忙在這裏不被視爲一個真正的問題。 – GhostCat

回答

0

請這樣做如下:

Scanner scanner=new Scanner(System.in); 
    while (true) { 
     System.out.println("Enter 11 digit number and press Q to quit"); 
     String number= scanner.nextLine(); 
     if(question.equals("Q")){ 
      break; 
     } 
     } 
0

在條件就可以把條件時,如以下答提到的終止循環。

public static void main(String[] args) { 

     Scanner sc = new Scanner(System.in); 

     while(sc.hasNextLine()){ 

     System.out.println(sc.nextLine()); 

     } 
    } 
相關問題