我是一個開始的程序員,我必須編寫一個豬拉丁語翻譯器。我有這個代碼可以翻譯和單個單詞。我只需要知道如何通過空格將輸入分隔爲單獨的字符串。一旦我能理解這一點(我相信我正在嘗試理解數組,我不確定嗎?)我將能夠根據需要編輯代碼。非常感謝!如何分隔一個字符串:豬拉丁語翻譯器
import java.io.*;
import java.util.*;
public class Main
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
String a = str.substring(0,1);
String b = str.substring(0,2);
String c = str.substring(0,3);
String d = str.substring(0,4);
String answer = "";
if (str.startsWith("a") || str.startsWith("e") || str.startsWith("i") || str.startsWith("o") || str.startsWith("u"))
{
System.out.print(str + "way");
}
else
{
answer = str.substring(2,str.length());
String answer2 = str.substring(1,str.length());
String answer3 = str.substring(3,str.length());
String answer4 = str.substring(4,str.length());
if (!(d.contains("a") || d.contains("e") || d.contains("i") || d.contains("o") || d.contains("u")))
{
System.out.print(answer4 + d + "ay");
}
else if (!(c.contains("a") || c.contains("e") || c.contains("i") || c.contains("o") || c.contains("u")))
{
System.out.print(answer3 + c + "ay");
}
else if (!(b.contains("a") || b.contains("e") || b.contains("i") || b.contains("o") || b.contains("u")))
{
System.out.print(answer + b + "ay");
}
else if (!(a.contains("a") || a.contains("e") || a.contains("i") || a.contains("o") || a.contains("u")))
{
System.out.print(answer2 + a + "ay");
}
}
}
}
我覺得這應該可行,但是當我創建字符串單詞時應該發生什麼?此外,我打印的單詞,它給了我:[Ljava.lang.String; @ 1f86e79 – Gihadi
@Gihadi'進口java.util.Arrays'和打印'Arrays.toString(單詞)'(數組不覆蓋'toString( )'方法,令人討厭,所以我們不得不訴諸使用這種實用方法)。 – arshajii