我正在嘗試爲我的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;
}
}
}
}
請正確格式化您的代碼。 – Turing85
@ Turing85:我沒有看到*,*格式錯誤* – Makoto