我正在尝试为我的编程入门课程编写一个英语到意大利语的数组翻译JApplet。当输入与数组中的任何字符串和索引都不匹配时,我在使用if-else语句嵌套的循环中执行JOptionPane的错误消息时遇到问题。输入转换为小写字符串以使输入不区分大小写。
我尝试过一个else语句,但由于循环,导致错误消息弹出十次。我已尝试过if(i!= EnglishWords.length),但即使equals()语句为true,这也会使applet跳到错误消息。如果你手动输入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 :(得分:0)
for循环:
for (int i=0; i<=EnglishWords.length; i++)
应该是&lt;,not&lt; =,因为当i == EnglishWords.length时,你将获得“if(finalInput.equals(EnglishWords [i])”的无效索引的异常,所以你将永远不会到“else if(i == EnglishWords.length)”。如果你想检查你是否在阵列的末尾,那么测试是:
else if (i == EnglishWords.length-1)
答案 1 :(得分: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);