简体   繁体   English

如何从用户读取多个输入以在Java中进行数组操作

[英]How to read multiple inputs from user for array manipulation in java

How do i take multiple inputs from user and print the result as per the input. 我如何从用户处获取多个输入并根据输入打印结果。 For example in the below codec i want to take multiple 11 digit number from user and print the result Y or N for each input by user as per the given if else condition. 例如,在下面的编解码器中,我要从用户处获取多个11 digit number ,并根据给定的条件(如果有其他条件)为用户的每个输入打印结果Y或N。

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");
        }

   }
}

Any help would be appreciated. 任何帮助,将不胜感激。

Please do something like below : 请执行以下操作:

 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;
        }
      }

In condition you can put condition when to terminate the loop as mentioned in below ans. 在条件中,您可以设置条件何时终止循环,如以下ans中所述。

 public static void main(String[] args) {

       Scanner sc = new Scanner(System.in); 

       while(sc.hasNextLine()){

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

      }
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM