2017-10-10 8 views
0

É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

+0

'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

+0

il donne une sortie comme "I Iikelrogrammingp I" – arfa

+0

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

Répondre

0

Pour reconvertir de porc-latin, écrire l'algorithme:

  1. Obtenez le 3ème de-dernière lettre. C'est la première lettre originale
  2. Vérifiez si la première lettre est majuscule. Convertir le résultat de l'étape 1 en haut.
  3. Supprimer les 3 dernières lettres. Le 1er + "ay" original
  4. Créer un nouveau mot.

Comme ceci:

while (pig.hasNext()) // is there another word? 
{ 
    word = pig.next(); 

    // Step 1. 
    // Original first letter of word 
    last = word.charAt(word.length() - 3); 
    // word.length should always be >= 3, but I guess you should check 
    // that and print an error if it's not. 

    // Step 2. 
    // First char in latin word 
    first = word.charAt(0); 
    if (Character.isUpperCase(first)) // first is capital letter 
    { 
     last = Character.toUpperCase(last); 
    } 

    // Steps 3. and 4. 
    String original = last + word.substring(0, word.length() - 3).toLowerCase(); 

    System.out.print(original + " "); 
} 

Note: Je ne vois pas comment vous pouvez reconvertir les mots commençant par une voyelle. Par exemple: "end" est "enday" dans fake-latin. Lorsque vous convertissez en arrière, comment savez-vous que c'est "fin" ou "den"? (Parce que "den" dans fake-latin est aussi "enday"). Cependant, l'assignation ne dit rien sur les mots qui commencent par les voyelles, donc vous devriez enlever cela de votre convertisseur. Problème résolu.

+0

Vous devriez être cohérent, utilisez 'Character.isUpperCase (d'abord) 'si vous utilisez' Character.toUpperCase (last) ' – phflack

+0

@phflack Ok. Actualisé. –