-5

嘿,所以我正在编写一个程序,用户输入正好 10 个单词,计算机输出每个包含Aora的单词。我在一些帮助下编写了程序,现在我需要知道如何使用户可以通过 while 循环连续输入 10 个单词。这是我到目前为止所拥有的。

import java.util.Scanner;

public class ReadASH{ 
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        String name;
        String S="";
        System.out.println("Please enter any word.");
        S = in.nextLine();
        if(S.contains("a")||S.contains("A"))
          System.out.println(S);
        else System.out.println("The word you entered contains no a's or A's");
    }
}
4

1 回答 1

3

您实际上需要有一个循环,例如:

import java.util.Scanner;

 public class ReadASH{ 
   public static void main(String[] args){
     Scanner in = new Scanner(System.in);

    String name;
    String S;
    for (int i=0; i<10; i++) {
      System.out.println("Please enter any word.");
      S = in.nextLine();
      if (S.contains("a")||S.contains("A"))
        System.out.println(S);
      else System.out.println("The word you entered contains no a's or A's");
    }
  }
}

根据评论更新代码:

import java.util.Scanner;

 public class ReadASH{ 
   public static void main(String[] args){
     Scanner in = new Scanner(System.in);

    String name;
    int wordsToRead = 10;
    String words[] = new String[wordsToRead];

    for (int i=0; i< wordsToRead; i++) {
      System.out.println("Please enter any word.");
      words[i] = in.nextLine();
    }
    for (int i=0; i< wordsToRead; i++) {
      if (words[i].contains("a")||words[i].contains("A"))
        System.out.println(words[i]);
    }
  }
}
于 2013-10-13T20:22:43.483 回答