2015-11-10 111 views
-1

我正在嘗試爲我的Intro編程課程編寫一個英文到意大利語數組翻譯器JApplet。當輸入與數組中的任何字符串和索引不匹配時,我在獲取JOptionPane的錯誤消息在嵌套if-else語句的循環內執行時遇到問題。輸入轉換爲小寫字符串以使輸入大小寫不敏感。在for循環中使用帶有JOptionPane的if-else語句

我試過只是一個else語句,但由於循環導致錯誤消息彈出十次。我試過其他的if(i!= EnglishWords.length),但是即使equals()語句爲true,小程序也會跳過錯誤消息。如果您手動輸入if-else if-else,它會起作用,正如我在代碼中所做的那樣,以防我無法使循環正常工作,但我們的任務中需要使用數組循環。

我真的可以使用一些幫助。謝謝你的閱讀。

下面是for循環:

for (int i=0; i<=EnglishWords.length; i++) 
     { 
      if (finalInput.equals(EnglishWords[i])){ 
       JOptionPane.showMessageDialog(null, ItalianWords[i], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[i]); 
       translatedWord = ItalianWords[i]; 
       break; 
       } 
      else if (i == EnglishWords.length){ 
       JOptionPane.showMessageDialog(null, "Sorry, word is not found", "ERROR", JOptionPane.ERROR_MESSAGE); 
       break; 
       } 

這裏是我的全部源代碼:

import java.awt.*; 
import javax.swing.*; 
import java.text.*; 



/** 
* Class ArrayAssignment - write a description of the class here 
* 
* @author 
* @version JDK 8 
* @course 
* @date 10-30-15 
* @URL: 
*/ 
public class ArrayAssignment extends JApplet 
{ 
    String[ ] EnglishWords = //declares initializes the text for the English words 
     {"House", "Lamp", "Table", "Sofa", "Computer", "Bathroom", "Bedroom", 
     "Kitchen", "Door", "Chair"}; 


    String[ ] ItalianWords = //declares and initializes the text for the Italian words 
     {"Casa", "Lampada", "Tabella", "Divano", "Computer", "Bagno", 
      "Camera da letto", "Cucina", "Porta", "Sedia"}; 

    String[ ] imageFileNames = //declares and initializes the images' file na 
     {"house.png", "lamp.jpg", "table.jpg", "sofa.jpg", 
     "computer.png", "bathroom.jpg", "bedroom.jpg", "kitchen.jpg", "door.jpg", 
     "chair.jpg"}; 

    /* 
    * Declares adn initializes the gallery size, which is the number of images in the file to be 
    * added to the applet 
    * 
    * The gallerySize will be used as the termination number in the 
    * getImage and icon loop. 
    */ 
    int gallerySize = 10; 

    Image img; //declaration of an alias for the Image variable 
    ImageIcon[ ] icons; //declaration for the actual future images 

    Image img2; //declaratin for another image 
    ImageIcon icon; //declaration for a second icon 

    Image[ ] imgSlide; //declaration to make all the images into an array for the loops 

    String EnglishInput; //declares the string to be used to define the JOptionPane input dialog box 

    String switchImgOutput; //delcaration for the image switch statement 
    Image imgOutput; //declaration to later execute the if statement inside of the imgSlide loop 

    String translatedWord; //variable to define the translated English word 
    /** 
    * Called by the browser or applet viewer to inform this JApplet that it 
    * has been loaded into the system. It is always called before the first 
    * time that the start method is called. 
    */ 

    public void init() 
    { 
     setLayout(new BorderLayout()); //changes the layout to border 

     icons = new ImageIcon[gallerySize]; // load the images into memory 

     //loop that retrieves all of the household images and converts them to icons 
     for (int i=0; i<gallerySize; i++) 
     { 
      img = getImage(getDocumentBase(), imageFileNames[i]); 
      icons[i] = new ImageIcon(img); 
     }   


     img2 = getImage(getDocumentBase(),"flags.png"); //retrieves the flag image 
     icon = new ImageIcon(img2); //converts the flag image to an icon 

     //shows a list of the available words to translate 
     JOptionPane.showMessageDialog(null, EnglishWords, "Available Words to Translate Into Italian", JOptionPane.INFORMATION_MESSAGE); 


     //creates a dialog box for where you will type in the English word you wish transalted to Italian 
     EnglishInput = (String)JOptionPane.showInputDialog(this, "English to Italian Translator", 
                "English to Italian", JOptionPane.PLAIN_MESSAGE, 
                 icon, null, "enter your selected word from list here"); 

     //converts the input to lowercase, thereby making it case insensitive            
     String lowerCase = EnglishInput.toLowerCase(); 

     String finalInput = lowerCase; //declares that the finalInput variable is the lowercase-converted input string 



     /* 
     * determines the entered English word by analyzing the string 
     * and connecting it to the correct 
     * array index 
     */ 
     switch (finalInput){ 
      case "house": 
       finalInput = EnglishWords[0]; break; 
      case "lamp": 
       finalInput = EnglishWords[1]; break; 
      case "table": 
       finalInput = EnglishWords[2]; break; 
      case "sofa": 
       finalInput = EnglishWords[3]; break; 
      case "computer": 
       finalInput = EnglishWords[4]; break; 
      case "bathroom": 
       finalInput = EnglishWords[5]; break; 
      case "bedroom": 
       finalInput = EnglishWords[6]; break; 
      case "kitchen": 
       finalInput = EnglishWords[7]; break; 
      case "door": 
       finalInput = EnglishWords[8]; break; 
      case "chair": 
       finalInput = EnglishWords[9]; break; 
      } 

    /* 
    * Displays an output message box that 
    * has the correct translation and icon. 
    * 
    * If the word doesn't exist in the translator, 
    * it will display an error message. 
    */ 
    /* 
     if (finalInput == EnglishWords[0]){ 
      JOptionPane.showMessageDialog(null, ItalianWords[0], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[0]); 
      translatedWord = ItalianWords[0]; 
     } 
     else if (finalInput == EnglishWords[1]){ 
      JOptionPane.showMessageDialog(null, ItalianWords[1], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[1]); 
      translatedWord = ItalianWords[1]; 
     } 
     else if (finalInput == EnglishWords[2]){ 
      JOptionPane.showMessageDialog(null, ItalianWords[2], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[2]); 
      translatedWord = ItalianWords[2]; 
     } 
     else if (finalInput == EnglishWords[3]){ 
      JOptionPane.showMessageDialog(null, ItalianWords[3], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[3]); 
      translatedWord = ItalianWords[3]; 
     } 
     else if (finalInput == EnglishWords[4]){ 
      JOptionPane.showMessageDialog(null, ItalianWords[4], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[4]); 
      translatedWord = ItalianWords[4]; 
     } 
     else if (finalInput == EnglishWords[5]){ 
      JOptionPane.showMessageDialog(null, ItalianWords[5], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[5]); 
      translatedWord = ItalianWords[5]; 
     } 
     else if (finalInput == EnglishWords[6]){ 
      JOptionPane.showMessageDialog(null, ItalianWords[6], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[6]); 
      translatedWord = ItalianWords[6]; 
     } 
     else if (finalInput == EnglishWords[7]){ 
      JOptionPane.showMessageDialog(null, ItalianWords[7], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[7]); 
      translatedWord = ItalianWords[7]; 
     } 
     else if (finalInput == EnglishWords[8]){ 
      JOptionPane.showMessageDialog(null, ItalianWords[8], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[8]); 
      translatedWord = ItalianWords[8]; 
     } 
     else if (finalInput == EnglishWords[9]){ 
      JOptionPane.showMessageDialog(null, ItalianWords[9], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[9]); 
      translatedWord = ItalianWords[9]; 
     } 
     else { 
      JOptionPane.showMessageDialog(null, "Sorry, word is not found", "ERROR", JOptionPane.ERROR_MESSAGE); 
     } 
     */ 



     for (int i=0; i<=EnglishWords.length; i++) 
     { 
      if (finalInput.equals(EnglishWords[i])){ 
       JOptionPane.showMessageDialog(null, ItalianWords[i], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[i]); 
       translatedWord = ItalianWords[i]; 
       break; 
       } 
      else if (i == EnglishWords.length){ 
       JOptionPane.showMessageDialog(null, "Sorry, word is not found", "ERROR", JOptionPane.ERROR_MESSAGE); 
       break; 
       } 
    } 
} 

public void paint(Graphics g){ 
    Image img3 = getImage(getDocumentBase(), "dictionary.jpg"); //retrieves the background image from file 
    g.drawImage(img3, 0,0,getWidth(), getHeight(), this); //sets the image as a background 

    g.setColor(Color.black); //sets the color to black 
    g.setFont(new Font ("Cooper Black", Font.BOLD, 18)); //changes the font to "Times new Roman, style to BOLD, and the size to 30 

    g.drawString("English Word:", 55, 175); //writes "English Word:" onto the applet window 
    g.drawString(EnglishInput, 105, 270); //draws the user input 

    g.drawString("Italian Translation:", 265, 175); //writes "Italian Translation:" onto the applet window 
    g.drawString(translatedWord, 320, 270); 

      for (int i=0; i<gallerySize; i++) 
     { 
      img = getImage(getDocumentBase(), imageFileNames[i]); 
      imgSlide[i] = img; 
     }   

      switch (switchImgOutput){ 
      case "house": 
       imgOutput = imgSlide[0]; break; 
      case "lamp": 
       imgOutput = imgSlide[1]; break; 
      case "table": 
       imgOutput = imgSlide[2]; break; 
      case "sofa": 
       imgOutput = imgSlide[3]; break; 
      case "computer": 
       imgOutput = imgSlide[4]; break; 
      case "bathroom": 
       imgOutput = imgSlide[5]; break; 
      case "bedroom": 
       imgOutput = imgSlide[6]; break; 
      case "kitchen": 
       imgOutput = imgSlide[7]; break; 
      case "door": 
       imgOutput = imgSlide[8]; break; 
      case "chair": 
       imgOutput = imgSlide[9]; break; 
      } 



    for (int i=0; i<=imageFileNames.length; i++){ 
     if (imgOutput == imgSlide[i]){ 
     g.drawImage(imgSlide[i], 200, 200, this); break; 
     } 
    } 








    } 
} 
+0

請正確格式化您的代碼。 – Turing85

+0

@ Turing85:我沒有看到*,*格式錯誤* – Makoto

回答

0

for循環:

for (int i=0; i<=EnglishWords.length; i++) 

應該是<,不< = ,因爲當我== EnglishWords.length,你會得到一個例外的無效索引在「如果(finalInput.equals(EnglishWords [i])「,所以你永遠不會去」其他if(i == EnglishWords.length)「。如果要檢查,如果你在數組的末尾,測試是:

else if (i == EnglishWords.length-1) 
+0

它工作!謝謝! – chase366

+0

不客氣! – DBug

+0

因此,讓我看看我是否理解,因爲數組的長度是10,通過使用<=運算符,它跳過9並繼續到10,正如您所說,這是一個無效索引,因爲第一個索引是總是0.因此,通過將for更改爲<,並在else-if語句中將長度減1,可以使兩個語句都停在索引9處。對嗎? – chase366

0

如果你只是想顯示消息您已在數組中用盡一切元素之後,那麼只顯示它在你完成所有事情之後。確保有某種return語句,以便其餘代碼不會執行;也許這應該被提取到它自己的功能?

for (int i = 0; i < EnglishWords.length; i++) { 
    if (finalInput.equals(EnglishWords[i])) { 
     JOptionPane.showMessageDialog(null, ItalianWords[i], "Italian Word", JOptionPane.PLAIN_MESSAGE, icons[i]); 
     translatedWord = ItalianWords[i]; 
     return; 
    } 
} 
JOptionPane.showMessageDialog(null, "Sorry, word is not found", "ERROR", JOptionPane.ERROR_MESSAGE); 
+0

這是另一種方式。感謝您的幫助! – chase366