2017-02-11 32 views
-2

私のプログラムでは、作業ディレクトリ(テキストを含む)にあるファイル名を入力し、同じディレクトリに既に存在する出力ファイル名を入力する必要があります。その後、ユーザーはファイル内のすべてのテキストを大文字にするか小文字にするかを選択する必要があります。このコードはどのようにリループすることができますか?

一度選択すると、別のファイルを処理するオプションが与えられます。それが私が困っているところです。 「別のファイルを処理しますか?はい、いいえはNですか?」という印刷後どのように最初にループバックするのですか?

今、私のコードは "大文字小文字のすべての単語"に戻ります。私はそれをやめて、別のファイルを処理するかどうかをユーザーに尋ねる必要があります。ファイル名を再度出力します。

public static void main(String[] args) { 
    Scanner sc = new Scanner(System.in); 
    System.out.println("Please enter the input data file name:"); 
    String fileInput = sc.next(); 
    System.out.println("Please enter the output data file name:"); 
    String fileOutput = sc.next(); 
    while(true){ 
     System.out.println("A: Capitalize all words.\nB: Lowercase all words."); 

     System.out.println("enter choice:"); 
     char choice = sc.next().charAt(0); 
     if(choice == 'A'){ 
      capitalize(fileInput, fileOutput); 
     }else{ 
      lowercase(fileInput, fileOutput); 
     } 

    } 
    System.out.println("Process another file? Y for Yes or N for No"); 
} 

答えて

1

次のように、すべてのコードをwhileループでラップするだけで済みます。 whileループは、それだけでコードを繰り返し:

public static void main(String[] args) { 
    while (true) { 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Please enter the input data file name:"); 
     String fileInput = sc.next(); 
     System.out.println("Please enter the output data file name:"); 
     String fileOutput = sc.next(); 
     System.out.println("A: Capitalize all words.\nB: Lowercase all words."); 

     System.out.println("enter choice:"); 
     char choice = sc.next().charAt(0); 
     if (choice == 'A') { 
      capitalize(fileInput, fileOutput); 
     } else { 
      lowercase(fileInput, fileOutput); 
     } 

     System.out.println("Process another file? Y for Yes or N for No"); 
     String processAnother = sc.next(); 
     if (processAnother.equals("N") || processAnother.equals("n")) break; 
    } 
} 
+0

私は一種の私は、whileループの中にコードのすべてを入れなければならないだろう考え出しええ、私はちょうど私がのために新しい文字列を追加する必要があります知りませんでした別のものを処理する。ありがとう! – user3496266

関連する問題