2017-08-09 54 views
-3

這是我迄今爲止對我的實驗室,但我不斷收到許多錯誤。林不知道我做錯了什麼,可有人請幫助我。問題在代碼註釋中,我們必須爲一個課程解決,但是我的老師沒有幫助。這讓非常令人沮喪,請幫助我的人,我已經花了幾個小時試圖弄清楚這一點Java課程撤銷重做流行的大學實驗室

import java.util.Scanner; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.util.Stack; 

/** 
* Application to demonstrate the use of a stack by simulating an editor's un/redo operations. 
* 
* @author lynda 
*/ 
public class UndoRedo { 

    public static Scanner openFile(String fileName) throws IOException{ 
     Scanner fileStream; 
     FileInputStream fileByteStream = new FileInputStream(fileName); 
     return(new Scanner(fileByteStream)); 
    } 

    public static void performOperation(String operation) { 
      System.out.println("Operation: " + operation + " completed."); 
    } 

    public static void main(String[] args) throws Exception { 
     String fileName; 
     Scanner inputFileStream; 

     Stack <String> undo= new Stack<String>(); 
     Stack <String> redo= new Stack <String>(); 
     /* Declare two stacks here: one for the undo and one for the redo 
     */ 

     String operation; 

     Scanner keyboard = new Scanner(System.in); 
     System.out.print("Enter file name: "); 
     inputFileStream = openFile(keyboard.nextLine()); 

     while (inputFileStream.hasNextLine()) { 
      operation = inputFileStream.nextLine(); 

      if(!operation.equals("UNDO")|| operation.equals("REDO")){ 
       performOperation(); 
       done.push(operation); 
      } 
      else if(operation.equals("UNDO")){ 
       performOperation("undo"); 

      } 
      else if(operation.equals("REDO")){ 
       performOperation("redo"); 

      } 
      else{ 
       System.out.println("the Stack is empty"); 
      } 

      /* if we have an editor operation (i.e. anything other than UNDO or REDO) 
       then 'perform' the operation by calling the method performOperation and 
       push the operation onto the 'done' stack. 
       . 
       if we have an UNDO operation, then if there is something to undo, 
       pop it off the stack, undo it (by calling method performOperation with "undo" in the string) 
       otherwise tell the user there is nothing to undo (i.e. the stack is empty) 

       if we have a REDO operation, then if there is something to redo, 
       pop it off the stack, redo it (by calling method performOperation with "redo" in the string) 
       otherwise tell the user there is nothing to undo (i.e. the stack is empty) 
      */ 


     } 

    inputFileStream.close(); 
    } 
} 
+2

「我不斷收到很多錯誤」。 _有什麼錯誤?_請複習[問]。 – jmoerdyk

+1

你沒有'performOperation()'方法沒有參數,否則if(operation.equals(「REDO」))'永遠不會被這個邏輯輸入 –

+0

發佈你在這裏遇到的錯誤,社區將幫助你出了如何解決每個問題。 –

回答

-1

您必須通過參數performOperation() ,我沒有看到任何堆棧中調用的代碼完成 和performOperation方法應該有內置的push和pop,並根據你的字符串參數執行其操作。

if(!operation.equals("UNDO")|| operation.equals("REDO")){ 
       *performOperation();* 
       done.push(operation); 
      } 
+0

什麼是arg我應該通過執行操作嗎? – Karanvir1

+0

您沒有沒有參數的執行方法,所以您應該再次要求用戶輸入正確的選項 –