2016-02-12 72 views
-2

應該從用戶讀取行,直到輸入哨兵「DONE」,此時它們將反向顯示。不打印「DONE」字使用java.util.Stack打印反向文本行

這裏是我的代碼,到目前爲止我不知道如何得到它的所有一起工作,我知道我已經得太多這一點,但任何幫助將不勝感激

import java.util.Scanner; 
import java.util.Stack; 

public class Turner_A05Q3{ 
    public static void main(String[] args) { 
     Stack<String> stack = new Stack<String>(); 
     ArrayList<String> list = new ArrayList<String>(); 
     System.out.println("Enter text, or done to stop"); 
     Scanner scanner = new Scanner(System.in);  
     List<String> al = new ArrayList<String>(); 
     String word;         
     while (scanner.hasNextLine()) {    
      word = scanner.nextLine();     
      if (word != null) {       
       word = word.trim();      
       if (word.equalsIgnoreCase("done")) {  
        break;         
       } 
       al.add(word);        
      } else { 
       break;         
      } 
     } 
     for (int i=0;i<word.length();i++){ 
      stack.push(word.substring(i,i+1)); 
     } 
     String wordrev = ""; 
     while(!stack.isEmpty()){ 
      wordrev += stack.pop(); 
     } 
     System.out.println("Reverse of the text \"" + wordrev);      
    } 
} 
+0

句子應該是b e以與輸入相反的順序顯示,或者每行應該被顛倒 - 例如, 「一些輸入」變成「tupni emoS」? –

+0

這段代碼的格式很難遵循。除此之外,你在問什麼?什麼/不工作? – Whymarrh

+0

https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html#reverse()可能會對您有所幫助 – 2016-02-12 18:23:55

回答

0

試着寫了這樣的循環:

for (int i = word.length(); i > 0; i--) { 
     stack.push(word.substring(i-1, i)); 
    } 

輸入:

DONE 

輸出:

DONE 
0

你總是硬道理(完成)的工作,因爲你不使用al你只使用變量word的最後內容。

你必須遍歷al

嘗試:

import java.util.Scanner; 
import java.util.Stack; 
import java.util.ArrayList; 
import java.util.List; 

public class Turner_A05Q3{ 
    public static void main(String[] args) { 
     Stack<String> stack = new Stack<String>(); 
     System.out.println("Enter text, or done to stop"); 

     Scanner scanner = new Scanner(System.in);  
     List<String> al = new ArrayList<String>(); 
     String word=null;   

     while (scanner.hasNextLine()) {    
      word = scanner.nextLine();     
      if (word != null) {       
       word = word.trim();      
        if (word.equalsIgnoreCase("done")) {  
         break;         
        } 
       al.add(word);        
      }else { 
       break;         
      } 
     } 

     for(int j=0;j<al.size();j++){ 
      word = al.get(j);  
      for (int i=0;i<word.length();i++){ 
       stack.push(word.substring(i,i+1)); 
      } 

      String wordrev = ""; 
      while(!stack.isEmpty()){ 
       wordrev += stack.pop(); 
      } 
      System.out.println("Reverse of the text \"" + wordrev);      
     } 
    } 
} 

如果你想顯示以相反的順序的話,而不是文字,請嘗試:

public static void main(String[] args) { 
    Stack<String> stack = new Stack<String>(); 
    System.out.println("Enter text, or done to stop"); 

    Scanner scanner = new Scanner(System.in);  
    String word=null;   

    while (scanner.hasNextLine()) {    
     word = scanner.nextLine();     
     if (word != null) {       
      word = word.trim();      
       if (word.equalsIgnoreCase("done")) {  
        break;         
       } 
      stack.push(word);        
     }else { 
      break;         
     } 
    } 

    while(!stack.isEmpty()){ 
     System.out.println("Reverse of the text \"" + stack.pop()); 
    } 
} 

記住, Stack是LIFO(後進先出)

+0

非常感謝您的好奇,您將如何更改以顯示逆轉的字詞不是我不知道如何告訴程序這樣做的人物 – zompire

+0

@zompire看我的編輯 –