Écrire un programme qui traduit un texte en Faux Latin et vice versa. L'anglais est traduit en faux latin en prenant la première lettre de chaque mot, en le déplaçant à la fin du mot et en ajoutant «ay» à chaque mot. A titre d'exemple, si votre programme lit depuis le clavier la chaîne "Le renard brun rapide" alors il devrait imprimer sur l'écran le texte suivant: "Hetay uickqay rownbay oxfay". Vous devriez également mettre en œuvre l'inverse de ceci, en lisant "Iay ikelay rogrammingpay" devrait imprimer sur l'écran "J'aime programmer".Écrire un programme qui traduit un texte en Faux Latin et retour
public class fakelatin {
public static void main(String[] args)
{
Scanner pig = new Scanner(System.in);
String word; original word
String latin = "";
char first;
boolean cap = false;
String line;
System.out.print("enter line to translate: "); //enter the line to translate
line = pig.nextLine();
pig = new Scanner(line);
// loop through all the words in the line
while (pig.hasNext()) // is there another word?
{
word = pig.next();
first = word.charAt(0);
if ('A' <= first && first <= 'Z') // first is capital letter
{
first = Character.toLowerCase(first);
cap = true;
}
else
cap = false;
// test if first letter is a vowel
if (first=='a' || first=='e' || first=='i' || first=='o' || first=='u')
latin = word + "ay";
else
{
if (cap)
{
latin = "" + Character.toUpperCase(word.charAt(1)); // char to String conversion
latin = latin + word.substring(2) + first + "ay";
}
else
latin = word.substring(1) + first + "ay";
}
System.out.print(latin + " ");
}
}
}
i ont obtenu la sortie comme IAY ikelay rogrammingpay que je veux
import java.util.Scanner;
public class fakelatin2 {
public static void main(String[] args)
{
Scanner pig = new Scanner(System.in);
String word; // original word
String latin = ""; // pig latin translation, init to empty string
char first,last;
boolean cap = false;
String line;
System.out.print("enter line to translate: ");
line = pig.nextLine();
pig = new Scanner(line);
// loop through all the words in the line
while (pig.hasNext()) // is there another word?
{
word = pig.next();
first = word.charAt(0);
last=word.charAt(word.length()-1);
if ('A' <= first && first <= 'Z') // first is capital letter
{
first = Character.toLowerCase(first);
cap = true;
}
else
cap = false;
latin = word.replaceAll("ay", "");
if(cap)
{
latin = "" + Character.toUpperCase(word.charAt(1));
latin=latin+word.substring(word.length()-1);
}
System.out.print(latin + " ");
}
}
}
Je reçois une sortie comme "i Ikel rogrammingp" mais pas en mesure d'obtenir "i like programmation" back
'word.replaceAll (" ay "," ")' semble dangereux, que se passe-t-il si votre mot est joué (laypay)? Il sera tronqué, vous pouvez utiliser 'word.replaceAll (" ay $ "," ")' (en utilisant un [regex] (https://docs.oracle.com/javase/8/docs/api/java /util/regex/Pattern.html#sum)) ou 'word.substring (0, word.length() - 2)' – phflack
il donne une sortie comme "I Iikelrogrammingp I" – arfa
Vous supprimez toutes les instances de "ay", mais ne jamais déplacer le dernier caractère vers l'avant, vous pouvez vouloir 'latin = latin.substring (latin.length() - 1) + latin.substring (0, latin.length() - 1);' – phflack