Also ich versuche, ein Programm mit JOptionPane zu programmieren, um einen Satz in Schwein Latin zu übersetzen Ich habe den Code meist herausgefunden, aber ich kann scheinen, um die Ausgabe zu erhalten, wenn es die Schweine Lateinische Version des zurückgibt Satz so jede mögliche Hilfe würde enorm seinNoch ein Schwein Latein Übersetzer
package piglatin;
import javax.swing.JOptionPane;
public class Piglatin {
public static void main(String[] args)
{
// String to hold input.
String input;
// Get a string to convert.
input = JOptionPane.showInputDialog("Enter a string and I will convert it to Pig latin.");
// Convert it to uppercase, for consistency.
input = input.toUpperCase();
// Display the Pig Latin translation.
JOptionPane.showMessageDialog(null,"Your Phrase is: "+ pigLatin);
}
public class PigLatinator
{
private String original; // Original string
private String pigLatin; // Pig Latin version
public PigLatinator(String input)
{
// Variable to hold each word
String word;
// Save the input string.
original = input;
// Initialize pigLatin to an empty string.
pigLatin = "";
// Trim all leading and trailing whitespaces.
StringBuilder sb = new StringBuilder(input.trim());
while (sb.length() > 0)
{
// Remove the first word from sb and assign it to word.
word = popWord(sb);
// Convert the word to Pig Latin and add it to the
// Pig Latin sentence.
pigLatin = pigLatin + toPigLatin(word) + " ";
}
}
private String popWord(StringBuilder sb)
{
// Locate the first space, or the end of the string.
int index = 0;
while (index < sb.length() && sb.charAt(index) != ' ')
{
index++;
}
// Get the word from the beginning of sb.
String word = sb.substring(0, index);
// Delete the word from sb.
sb.delete(0, index+1);
// Return the extracted word.
return word;
}
private String toPigLatin(String word)
{
// Create a StringBuilder.
StringBuilder sb = new StringBuilder(word);
// Get the first letter of the word.
char first = sb.charAt(0);
// Append the letter to the end of the word.
sb.append(first);
// Append "AY" to the word.
sb.append("AY");
// Delete the first letter.
sb.deleteCharAt(0);
// Return the word.
return sb.toString();
}
/**
getPigLatin method
@return The Pig Latin version of the string.
*/
public String getPigLatin()
{
return pigLatin;
}
/**
getOriginal method
@return The original string.
*/
public String getOriginal()
{
return original;
}
}
}
Hey, dank schlafen einzuwilligen für die Hilfe! Es hat mir wirklich geholfen zu sehen, was zum Teufel ich falsch gemacht habe. Dieser Code scheint viel eleganter zu sein und nicht überall. –