gpt4 book ai didi

java - JAVA翻译程序

转载 作者:太空宇宙 更新时间:2023-11-04 06:35:42 25 4
gpt4 key购买 nike

所以,我和我的 friend 用 JAVA 编写了一个英语到 Al Bhed(FFX 的方言)代码。我想实现“括号(或 [])内的字符未翻译”的功能,但我不知道如何实现。有人可以帮我实现它吗?这是代码。它不是完整的数组格式,因为我不太熟悉数组。

import java.io.*;

class albhed
{
public static void main (String[] args) throws IOException
{
BufferedReader dataIn = new BufferedReader (new InputStreamReader (System.in));

int a;
String b;

System.out.println("English to Al Bhed, input 1.");
System.out.println("Al Bhed to English, input 2.");
b = dataIn.readLine();
a = Integer.parseInt(b);
if (a == 1)
{
System.out.println("Enter English characters to translate.");
String x = dataIn.readLine();
int y = x.length();
String z[] = x.split("");
System.out.println("The translated characters from English to Al Bhed:");
for(int i = 0; i < y; i++)
{
switch (z[i])
{
case "A": System.out.print("Y"); break;
case "B": System.out.print("P"); break;
case "C": System.out.print("L"); break;
case "D": System.out.print("T"); break;
case "E": System.out.print("A"); break;
case "F": System.out.print("V"); break;
case "G": System.out.print("K"); break;
case "H": System.out.print("R"); break;
case "I": System.out.print("E"); break;
case "J": System.out.print("Z"); break;
case "K": System.out.print("G"); break;
case "L": System.out.print("M"); break;
case "M": System.out.print("S"); break;
case "N": System.out.print("H"); break;
case "O": System.out.print("U"); break;
case "P": System.out.print("B"); break;
case "Q": System.out.print("X"); break;
case "R": System.out.print("N"); break;
case "S": System.out.print("C"); break;
case "T": System.out.print("D"); break;
case "U": System.out.print("I"); break;
case "V": System.out.print("J"); break;
case "W": System.out.print("F"); break;
case "X": System.out.print("Q"); break;
case "Y": System.out.print("O"); break;
case "Z": System.out.print("W"); break;
case "a": System.out.print("y"); break;
case "b": System.out.print("p"); break;
case "c": System.out.print("l"); break;
case "d": System.out.print("t"); break;
case "e": System.out.print("a"); break;
case "f": System.out.print("v"); break;
case "g": System.out.print("k"); break;
case "h": System.out.print("r"); break;
case "i": System.out.print("e"); break;
case "j": System.out.print("z"); break;
case "k": System.out.print("g"); break;
case "l": System.out.print("m"); break;
case "m": System.out.print("s"); break;
case "n": System.out.print("h"); break;
case "o": System.out.print("u"); break;
case "p": System.out.print("b"); break;
case "q": System.out.print("x"); break;
case "r": System.out.print("n"); break;
case "s": System.out.print("c"); break;
case "t": System.out.print("d"); break;
case "u": System.out.print("i"); break;
case "v": System.out.print("j"); break;
case "w": System.out.print("f"); break;
case "x": System.out.print("q"); break;
case "y": System.out.print("o"); break;
case "z": System.out.print("w"); break;
default: System.out.print(z[i]); break;
}
}
}
else if (a == 2)
{
System.out.println("Enter Al Bhed characters to translate.");
String x = dataIn.readLine();
int y = x.length();
String z[] = x.split("");
System.out.println("The translated characters from Al Bhed to English:");
for(int i = 0; i < y; i++)
{
switch (z[i])
{
case "Y": System.out.print("A"); break;
case "P": System.out.print("B"); break;
case "L": System.out.print("C"); break;
case "T": System.out.print("D"); break;
case "A": System.out.print("E"); break;
case "V": System.out.print("F"); break;
case "K": System.out.print("G"); break;
case "R": System.out.print("H"); break;
case "E": System.out.print("I"); break;
case "Z": System.out.print("J"); break;
case "G": System.out.print("K"); break;
case "M": System.out.print("L"); break;
case "S": System.out.print("M"); break;
case "H": System.out.print("N"); break;
case "U": System.out.print("O"); break;
case "B": System.out.print("P"); break;
case "X": System.out.print("Q"); break;
case "N": System.out.print("R"); break;
case "C": System.out.print("S"); break;
case "D": System.out.print("T"); break;
case "I": System.out.print("U"); break;
case "J": System.out.print("V"); break;
case "F": System.out.print("W"); break;
case "Q": System.out.print("X"); break;
case "O": System.out.print("Y"); break;
case "W": System.out.print("Z"); break;
case "y": System.out.print("a"); break;
case "p": System.out.print("b"); break;
case "l": System.out.print("c"); break;
case "t": System.out.print("d"); break;
case "a": System.out.print("e"); break;
case "v": System.out.print("f"); break;
case "k": System.out.print("g"); break;
case "r": System.out.print("h"); break;
case "e": System.out.print("i"); break;
case "z": System.out.print("j"); break;
case "g": System.out.print("k"); break;
case "m": System.out.print("l"); break;
case "s": System.out.print("m"); break;
case "h": System.out.print("n"); break;
case "u": System.out.print("o"); break;
case "b": System.out.print("p"); break;
case "x": System.out.print("q"); break;
case "n": System.out.print("r"); break;
case "c": System.out.print("s"); break;
case "d": System.out.print("t"); break;
case "i": System.out.print("u"); break;
case "j": System.out.print("v"); break;
case "f": System.out.print("w"); break;
case "q": System.out.print("x"); break;
case "o": System.out.print("y"); break;
case "w": System.out.print("z"); break;
default: System.out.print(z[i]); break;
}
}
}
else
{
throw new IllegalArgumentException("\nINPUT 1 OR 2 ONLY.");
}
}
}

最佳答案

好吧,您可以使用 boolean 值来存储这样的事实:您不想每次遇到 [ 时都翻译任何字符,直到找到任何 ]

                boolean must_translate = true;
for(int i = 0; i < y; i++)
{
if (z[i].equals("[")) { must_translate = false; }
else if (z[i].equals("]")) { must_translate = true; }
else {
if (must_translate) {
switch (z[i])
// (....)
} else {
System.out.print(z[i]);
}
}

当然,您的代码远非最佳,但这不是重点。

(仅供引用:您不必将字符串拆分为较小的字符串。最好直接访问字符串的 soem 索引处的字符。此外,最好构建一个 StringBuffer 对象,并在末尾打印它,而不是为每个字符调用 System.out.print。而且您的 switch 部分很长。最好将翻译规则存储在数组或映射中。)

关于java - JAVA翻译程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25423269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com