Java 比较两个数组时出现问题
这个程序应该把一个单词从美国翻译成英国版本。它只对第一个单词起作用,但对其他单词不起作用,因为它给出了else语句 我的代码:Java 比较两个数组时出现问题,java,arrays,debugging,equals,compareto,Java,Arrays,Debugging,Equals,Compareto,这个程序应该把一个单词从美国翻译成英国版本。它只对第一个单词起作用,但对其他单词不起作用,因为它给出了else语句 我的代码: import java.util.Scanner; public class BritishTranslator { public static void main(String[]args) { Scanner input = new Scanner(System.in); String word; Str
import java.util.Scanner;
public class BritishTranslator {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
String word;
String [] america = new String[8];
String [] british = new String[8];
america[0] = "attic";
america[1] = "business suit";
america[2] = "elevator";
america[3] = "frenc fries";
america[4] = "ice cream";
america[5] = "sneakers";
america[6] = "truck";
america[7] = "zero";
british[0] = "loft";
british[1] = "lounge suit";
british[2] = "lift";
british[3] = "chips";
british[4] = "ice";
british[5] = "plimsolls";
british[6] = "lorry";
british[7] = "nough";
System.out.println("Please enter an American word: ");
word = input.nextLine();
for (int i = 0; i < america.length; i++)
{
for(int j = 0; j < british.length; j++)
{
if (word.equals(america[i]))
{
System.out.println(america[i] + " in british is: " + british[j]);
System.exit(0);
}
else
{
System.out.println("Word not found in the dictionary.");
System.exit(0);
}
}
}
}
}
import java.util.Scanner;
公共类英国翻译{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
字符串字;
字符串[]美国=新字符串[8];
字符串[]英制=新字符串[8];
美国[0]=“阁楼”;
美国[1]=“西装”;
美国[2]=“电梯”;
美国[3]=“法国薯条”;
美国[4]=“冰淇淋”;
美国[5]=“运动鞋”;
美国[6]=“卡车”;
美国[7]=“零”;
英国[0]=“阁楼”;
英国[1]=“休闲服”;
英国[2]=“电梯”;
英国[3]=“芯片”;
英国[4]=“冰”;
英国[5]=“plimsolls”;
英国[6]=“卡车”;
英国人[7]=“nough”;
System.out.println(“请输入一个美国单词:”);
word=input.nextLine();
for(int i=0;i
我需要学习如何调试这段代码的帮助。如果你发现单词look在british数组的同一索引中,你只需要遍历america数组。 另外一个是循环之外的
for (int i = 0; i < america.length; i++){
if (word.equals(america[i])) {
System.out.println(america[i] + " in british is: " + british[i]);
System.exit(0);
}
}
System.out.println("Word not found in the dictionary.");
System.exit(0); // you dont need this as well
}
for(int i=0;i
您只需遍历america数组,如果您发现单词,请查看british数组中的相同索引。
另外一个是循环之外的
for (int i = 0; i < america.length; i++){
if (word.equals(america[i])) {
System.out.println(america[i] + " in british is: " + british[i]);
System.exit(0);
}
}
System.out.println("Word not found in the dictionary.");
System.exit(0); // you dont need this as well
}
for(int i=0;i
由于要将美语单词与法语单词配对,只需循环数组一次,如果单词等于美语单词,则需要在法语数组和美语数组中的相同索引处打印字符串
for (int i = 0; i < america.length && i < british.length; i++)
{
if (word.equals(america[i]))
{
System.out.println(america[i] + " in british is: " + british[i]);
System.exit(0);
}
}
System.out.println("Word not found in the dictionary.");
for(int i=0;i
由于要将美语单词与法语单词配对,只需循环数组一次,如果单词等于美语单词,则需要在法语数组和美语数组中的相同索引处打印字符串
for (int i = 0; i < america.length && i < british.length; i++)
{
if (word.equals(america[i]))
{
System.out.println(america[i] + " in british is: " + british[i]);
System.exit(0);
}
}
System.out.println("Word not found in the dictionary.");
for(int i=0;i
由于这两个数组在同一索引中包含单词翻译,因此不必遍历第二个表。只需在第一个表中找到单词的索引,并使用该索引获得第二个表中的翻译。如果未找到单词,也可使用布尔
标志找到
在循环后检查:
System.out.println("Please enter an American word: ");
word = input.nextLine();
boolean found = false;
for (int i = 0; i < america.length; i++) {
found = word.equals(america[i]);
if (found) {
System.out.println(america[i] + " in british is: " + british[i]);
break;
}
}
if (!found)
System.out.println("Word not found in the dictionary.");
System.out.println(“请输入一个美国单词:”);
word=input.nextLine();
布尔值=false;
for(int i=0;i
使用
break
时,一旦找到单词,循环就会停止。由于两个数组在同一索引中包含单词翻译,因此不必遍历第二个表。只需在第一个表中找到单词的索引,并使用该索引获得第二个表中的翻译。如果未找到单词,也可使用布尔
标志找到
在循环后检查:
System.out.println("Please enter an American word: ");
word = input.nextLine();
boolean found = false;
for (int i = 0; i < america.length; i++) {
found = word.equals(america[i]);
if (found) {
System.out.println(america[i] + " in british is: " + british[i]);
break;
}
}
if (!found)
System.out.println("Word not found in the dictionary.");
System.out.println(“请输入一个美国单词:”);
word=input.nextLine();
布尔值=false;
for(int i=0;i
使用
break
一找到单词,循环就停止了。我相信这里发生了一些事情
首先,我认为您不想在完成循环之后再执行else语句。正如当前编写的一样,由于if/else和System.exit(0)调用的性质,它将在第一个“if”语句失败时立即终止
System.out.println(“请输入一个美国单词:”);
word=input.nextLine();
for(int i=0;i