在Java中调用多维数组的更好方法是什么?

在Java中调用多维数组的更好方法是什么?,java,arrays,bluej,Java,Arrays,Bluej,我是全新的Java,从我的导师那里得到了一个编码项目。我正在创建一个Java程序,如果某人去另一个国家旅行,它将帮助他们。现在我一直在努力寻找一种更好的方法,将一个短语从英语翻译成我创建的多维数组中列出的一种语言。很抱歉,这篇文章太长了。这是我几个月来第二次使用堆栈溢出 String[][] Phrases = {{"Hola", "Adios", "Gracias", "Si", "No", "Lo siento or perdón"},//Spanish {"Bonjour", "Good

我是全新的Java,从我的导师那里得到了一个编码项目。我正在创建一个Java程序,如果某人去另一个国家旅行,它将帮助他们。现在我一直在努力寻找一种更好的方法,将一个短语从英语翻译成我创建的多维数组中列出的一种语言。很抱歉,这篇文章太长了。这是我几个月来第二次使用堆栈溢出

String[][] Phrases = 
{{"Hola", "Adios", "Gracias", "Si", "No", "Lo siento or perdón"},//Spanish
{"Bonjour", "Goodbye", "Au revoir", "Oui", "Non", "Je suis désolé"},//French
{"Merhaba", "Güle güle", "teşekkür ederim", "Evet", "yok hayır", "üzgünüm"},//Turkish
{"Здравствуйте (Zdravstvuyte)", "Прощай (Proshchay)", "Спасибо (Spasibo)", "да (da)", "нет (net)", "прости (prosti)"}};//Russian
我有一个switch语句,在案例4中,我将使用if语句调用数组,根据用户的输入翻译英语短语。把它翻译成西班牙语很好,但是如果我要为其他三种语言做的话,那就有很多事情要做

switch (option){




    case 4: System.out.println("Select which phrase 1 through 6 you would like to translate to.");
        System.out.println("1 - Hello, \n2 - Goodbye, \n3 - Thank you, \n4 - Yes, \n5 - No, \n6 - I'm Sorry");
        int speak = 0;
        speak = info.nextInt();

         if (speak ==1){
        System.out.println("Option " + speak + " translates to " + Phrases[0][0]);
    } else if (speak == 2){
        System.out.println("Option " + speak + " translates to " + Phrases[0][1]);
    }else if (speak == 3){
        System.out.println("Option " + speak + " translates to " + Phrases[0][2]);
    }else if (speak == 4){
        System.out.println("Option " + speak + " translates to " + Phrases[0][3]);
    }else if (speak == 5){
        System.out.println("Option " + speak + " translates to " + Phrases[0][4]);
    }


           break;

    //Give user menu of phrases, store phrase input, call array for phrase,
}
肯定有比写一堆if语句更好的方法。调用数组来翻译用户想要翻译的短语的更好方法是什么?我很抱歉,如果这没有任何意义,但在这一点上,我完全迷失了方向。下面是我到目前为止得到的一切。我用的是蓝色J,我有两门课。我想做的就是找到一种更简单的方法,当用户选择要翻译的短语时,将英语短语翻译成所提供的四种语言之一

旅行班

import java.util.Scanner;

public class Travel
{


public static void main (String args[]) {
System.out.println("Welcome to One Culture! Before selecting a vacation spot, we'll need to ask a few things.");
//User input
System.out.println("What's your full name?");
Scanner info = new Scanner(System.in);
String name = info.nextLine();

System.out.println("What's your nationality?");
String origin = info.nextLine();

System.out.println("Are you looking for a place where the weather is hot or cold?");
String temp = info.nextLine();

System.out.println("How much money do you plan on traveling with? What's your budget?");
int money = info.nextInt();

System.out.println("Traveler's Information: \n" + "Name: " + name + "\nOrigin: " + origin +
"\nBudget: " + money +" USD" + "\nPreferred Weather: " + temp + "\n");


System.out.println("Based on traveler's information, here are locations of interest: \n");

Assistant.vacationSpot();
int spot = 0;
if (temp.equals ("hot")){
    System.out.println("\nBased on weather choice, here are recommended locations for hot: ");
    Assistant.warm();
    System.out.println("\nUse the following codes to input your currency choices for your destination: \n 1 - Mexican peso \n 2 - Senegal West African CFA franc");
        spot = info.nextInt();
}
        else if(temp.equals ("cold")){
    System.out.println("\nBased on weather choice, here are recommended locations for cold: ");
    Assistant.cold();
    System.out.println("\nUse the following codes to input your currency choices for your destination: \n 3 - Turkish Lira \n 4 - Russian ruble");
         spot = info.nextInt();
    }



int option = 0;  

//Greeting user
String greet = "";
if (spot == 1) {
         greet = "Mexico";
     }
     else if (spot == 2){
         greet = "Senegal";
        }
     else if (spot == 3){
         greet = "Turkey";
        }   

     else if (spot == 4){
         greet = "Russia";
        }



String[] moneySwap = {"peso", "CFA franc", "Lira", "ruble"};




/*String[] spanishPhrases = {"Hola", "Adios", "Gracias", "Si", "No", "Lo siento or perdón"}; 
String[] frenchPhrases = {"Bonjour", "Goodbye", "Au revoir", "Oui", "Non", "Je suis désolé"};
String[] turkishPhrases = {"Merhaba", "Güle güle", "teşekkür ederim", "Evet", "yok hayır", "üzgünüm"};
String[] russianPhrases = {"Здравствуйте (Zdravstvuyte)", "Прощай (Proshchay)", "Спасибо (Spasibo)", "да (da)", "нет (net)", "прости (prosti)"};
 */

String[][] Phrases = 
{{"Hola", "Adios", "Gracias", "Si", "No", "Lo siento or perdón"},//Spanish
{"Bonjour", "Goodbye", "Au revoir", "Oui", "Non", "Je suis désolé"},//French
{"Merhaba", "Güle güle", "teşekkür ederim", "Evet", "yok hayır", "üzgünüm"},//Turkish
{"Здравствуйте (Zdravstvuyte)", "Прощай (Proshchay)", "Спасибо (Spasibo)", "да (da)", "нет (net)", "прости (prosti)"}};//Russian


System.out.println("Welcome to " + greet + " What would you like to do now?");

//Menu of task to perform
System.out.println("1 - convert currency \n 2 - Haggle prices \n 3 - Make a purchase \n 4 - Translate a phrase.");
option = info.nextInt();


switch (option){
    case 1: System.out.println("Your budget from USD to " + moneySwap[spot - 1] + " is " +  Assistant.currency(money, spot));
            break;

    case 2:
            break;

    case 3: //ask user for purchase cost, store number, subtract from total budget, check if budget is less than 0
            break;

    case 4: System.out.println("Select which phrase 1 through 6 you would like to translate to.");
        System.out.println("1 - Hello, \n2 - Goodbye, \n3 - Thank you, \n4 - Yes, \n5 - No, \n6 - I'm Sorry");
        int speak = 0;
        speak = info.nextInt();

         if (speak ==1){
        System.out.println("Option " + speak + " translates to " + Phrases[0][0]);
    } else if (speak == 2){
        System.out.println("Option " + speak + " translates to " + Phrases[0][1]);
    }else if (speak == 3){
        System.out.println("Option " + speak + " translates to " + Phrases[0][2]);
    }else if (speak == 4){
        System.out.println("Option " + speak + " translates to " + Phrases[0][3]);
    }else if (speak == 5){
        System.out.println("Option " + speak + " translates to " + Phrases[0][4]);
    }


           break;

    //Give user menu of phrases, store phrase input, call array for phrase,
}
 }
}
下面是助手

   public class Assistant
{

 //Displaying warm locations
 public static void warm() {
 String[][] warm = {{"Mexico","Senegal"},
                   {"Spanish", "French"}};
            hot(warm);       
            }                      
 public static void hot(String x[][]){
     for(int row = 0; row < x.length; row++){
         for (int col = 0; col < x[row].length;col++){
            System.out.print(x[row][col]+ "\t");
         }
         System.out.println();
        }              
   }  

 //Displaying colder locations
 public static void cold() {
 String[][] freeze = {{"Turkey ","Russia"},
                     {"Turkish","Russian",}};  
            display(freeze);       
            }                      
 public static void freeze(String x[][]){
     for(int row = 0; row < x.length; row++){
         for (int col = 0; col < x[row].length;col++){
            System.out.print(x[row][col]+ "\t");
         }
         System.out.println();
        }                 
   }  

   //money conversion
 public static double currency(int budget, int  dest) {
     double temp = budget;
     if (dest == 1) {
         temp = budget * 18.84;
     }
     else if (dest == 2){
         temp = budget * 543.75;
        }
     else if (dest == 3){
         temp = budget * 4.10;
        }   

      else if (dest == 4){
         temp = budget * 63.42;
        }
     return temp;
    }












}
公共课堂助理
{
//显示温暖的位置
公共静态空温(){
字符串[][]warm={{“墨西哥”,“塞内加尔”},
{“西班牙语”、“法语”};
热(暖);
}                      
公共静态void hot(字符串x[]]{
对于(int row=0;row
您的speak整数比该单词的索引多出一个,因此请使用speak-1访问该单词:

case 4: System.out.println("Select which phrase 1 through 6 you would like to translate to.");
        System.out.println("1 - Hello, \n2 - Goodbye, \n3 - Thank you, \n4 - Yes, \n5 - No, \n6 - I'm Sorry");
        int speak = 0;
        speak = info.nextInt();
        System.out.println("Option " + speak + " translates to " + Phrases[0][speak-1]

如果使用此方法,最好使用额外的If语句检查speak是否也在1到5之间。

您已经有了要返回的翻译索引。您根本不需要
if
语句

替换:

         if (speak ==1){
    System.out.println("Option " + speak + " translates to " + Phrases[0][0]);
} else if (speak == 2){
    System.out.println("Option " + speak + " translates to " + Phrases[0][1]);
}else if (speak == 3){
    System.out.println("Option " + speak + " translates to " + Phrases[0][2]);
}else if (speak == 4){
    System.out.println("Option " + speak + " translates to " + Phrases[0][3]);
}else if (speak == 5){
    System.out.println("Option " + speak + " translates to " + Phrases[0][4]);
}
与:

if(说话>0&&speak<5){
System.out.println(“选项”+speak+”翻译为“+Phrases[0][speak-1]);
}否则{
//按照您的意愿处理无效输入
}

感谢所有帮助并发布解决方案的人。我从两位发布答案的人那里看到了答案,解决方案奏效了,但只有在翻译成西班牙语时才起作用。我需要它能够根据用户所在的四个位置中的哪一个来翻译短语。所以我添加了一些if语句,得到了这个

 if(spot == 1 && speak > 0 && speak < 7){
      System.out.println("Option " + speak + " translates to " + Phrases[0][speak-1]);
     }else if ((spot == 2 && speak > 0 && speak < 7)) {         
         System.out.println("Option " + speak + " translates to " + Phrases[1][speak-1]);
         //handle the invalid input however you want
      } else if ((spot == 3 && speak > 0 && speak < 7)) {         
         System.out.println("Option " + speak + " translates to " + Phrases[2][speak-1]);

        }else if ((spot == 4 && speak > 0 && speak < 7)) {         
         System.out.println("Option " + speak + " translates to " + Phrases[3][speak-1]);
        }
if(spot==1&&speak>0&&speak<7){
System.out.println(“选项”+speak+”翻译为“+Phrases[0][speak-1]);
}如果((spot==2&&speak>0&&speak<7)){
System.out.println(“选项”+speak+”翻译为“+Phrases[1][speak-1]);
//按照您的意愿处理无效输入
}如果((spot==3&&speak>0&&speak<7)){
System.out.println(“选项”+speak+”翻译为“+Phrases[2][speak-1]);
}如果((spot==4&&speak>0&&speak<7)){
System.out.println(“选项”+speak+”翻译为“+Phrases[3][speak-1]);
}

我刚刚在if语句中添加了
spot
,这样它就可以知道用户选择了哪个国家,并将其翻译成特定的语言。然后我让
说<7
,因为它没有翻译选项5和6中的短语。这还是个新概念,但当我告诉你我发现它时有多兴奋时,我非常高兴。

地图是用于此目的的,但是因为你还没有学会它们,你的教授很可能让你这么做。我尝试了下面给我的两种解决方案,它们都起了作用,但只有在将短语翻译成西班牙语时才起作用。如果用户选择了其他三个国家中的一个,如何将其翻译成其他语言?
 if(spot == 1 && speak > 0 && speak < 7){
      System.out.println("Option " + speak + " translates to " + Phrases[0][speak-1]);
     }else if ((spot == 2 && speak > 0 && speak < 7)) {         
         System.out.println("Option " + speak + " translates to " + Phrases[1][speak-1]);
         //handle the invalid input however you want
      } else if ((spot == 3 && speak > 0 && speak < 7)) {         
         System.out.println("Option " + speak + " translates to " + Phrases[2][speak-1]);

        }else if ((spot == 4 && speak > 0 && speak < 7)) {         
         System.out.println("Option " + speak + " translates to " + Phrases[3][speak-1]);
        }