2016-07-24 2 views
-1

문자열 배열 목록에 사용자 입력 (요소)을 추가하는 방법은 무엇입니까? 자바. 그래서 "질문 입력 :"이라고 물으면 어레이 목록에 추가해야합니다. 내 규범에서 아주 지저분한 일 이라니. 나는 인터넷에서 답을 찾기 위해 며칠이나 하루를 보냈습니다. 도와주세요!문자열 배열 목록에 요소 추가

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