2016-07-24 15 views
-1

文字列配列リストにユーザー入力(要素)を追加するにはどうすればよいですか? Java。ですから、私は "質問を入力:"と尋ねると、配列リストに追加する必要があります。私のコードでは非常に乱雑です。私は日々インターネットで回答を探していました。助けてください!文字列配列リストに要素を追加する

public class OIP2 { 

     public static void main(String[]args) throws Exception{ 
     // TODO Auto-generated method stub 
     Scanner sc= new Scanner(System.in); 
     String input; 
     String fileName = "storeOIP.txt"; 
     java.io.File file = new java.io.File("storeOIP.txt"); 
      String entry; 
      int n; 
      int question12 = 0; 
      String question1 ; 
      String question11; 

      try { 

      System.out.println(" Organization in project work"); 
      System.out.println(" ============================"); 
      Scanner in = new Scanner(System.in); 
      ArrayList<String> questions= new ArrayList<String>(); 


     questions.add(" 1. Qn : How to organize your time well when you're juggling"); 
     questions.add(" with so many project work and assignments on the same day?"); 
     questions.add(""); 
     questions.add(" Best answer : The best solution to this is to early planning or schedule"); 
     questions.add(" your time wisely. Write in a calendar beforehand the work you are going"); 
     questions.add(" to do for an assignment"); 
     questions.add(""); 
     questions.add(" Andy23: Finish your work faster everyday"); 
     String questionsList[]=questions.toArray(new String[questions.size()]); 

     /*Displaying Array elements*/ 
     for(String k: questionsList){ 
      System.out.println(k); 
     } 
     BufferedReader br = new BufferedReader (new InputStreamReader(System.in)); 
     PrintWriter outputStream = new PrintWriter(fileName); 
     System.out.println(""); 

     System.out.println(" Do you want ask a question related to this category? (Y/N)"); 
     char answer1=sc.next().charAt(0); 

     if(answer1== 'Y'|| answer1 == 'y'){ 
      System.out.println(" Enter a question:");//when this prompt appears, it should add to the array list of qns// 
      question1 = br.readLine(); 
      questions.add (question1); 
      System.out.println(" Anonymous:" + question1); 
      outputStream.println("Anonymous:" + question1); 
      System.out.println(" Your question has been posted."); 
     } 

     System.out.println(" Do youwant to reply to any of the posted questions?(Y/N):"); 
     char answer = sc.next().charAt(0); 
     if(answer== 'Y' || answer== 'y'){ 

      System.out.println(" Which question do you want to add answer?"); 
      question12= sc.nextInt(); 
     } 

     while(question12 >2){ 
      System.out.println(" Please enter a valid question number:"); 
      question12 = sc.nextInt(); 

      outputStream.println(question12); 
     } 
     System.out.println(" Add an answer:"); 
     question11 = br.readLine(); 
     outputStream.println("Anonymous:" + question11); 
     System.out.println("Anonymous:" + question11); 
     outputStream.flush(); 
     outputStream.close(); 
     System.out.println(" Thank you for your answer!"); 
    }catch (FileNotFoundException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    System.out.println(" Do you want to stay at this page?(Y/N):"); 
    char ans = sc.next().charAt(0); 
    if (ans== 'N' || ans == 'n'){ 
    }else{ 
     if (ans=='N'|| ans=='n'){ 
      System.out.println(" Do you want to still stay at this page?(Y/N):"); 
      char ans1 = sc.next().charAt(0); 
      if (ans1== 'N' || ans1 == 'n'){ 
       return; 
      } 
     } 
    } 
} 
} 
+0

あなたがネストされすぎて、この***場合(ANS == 'N' || ANS == 'N'){***、nuddlenコードを作成し、ハードになっていますアルゴリズムのロジックを理解する.... –

答えて

0
public static void main(String[]args) { 

    Scanner scanner = new Scanner(System.in); 

    ArrayList<String> questions= new ArrayList<String>(); 

    String input = ""; 
    while(!input.equals("exit")) { 
     System.out.print("Insert a question: "); 
     input = scanner.nextLine(); 
     questions.add(input);  
    } 
} 
関連する問題