2016-02-12 44 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看我的编辑 –