0

だから、私は私のConvertSentence方法に問題があります。後で出力を行う追加のメソッドがありますが、残念ながら、ユーザー入力の無限ループにはまってしまいます。何が欠けているのかわかりません。この gif をチェックして、実際の動作を確認してください: http://f.cl.ly/items/3A3h1w3Z271J3d2G393J/Screen%20Recording%202016-06-11%20at%2009.32%20PM.gif

import  java.util.Scanner;
public class austinnichols_Rot13Arrays {
        private static  Scanner input   = new Scanner (System.in);
        public static void  main    (   String args[] ) {
                            String[]    sentences   = new String[5];
                            getSentences(sentences);
                            displayOriginal(sentences);
                            String  convertMe   = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                            String  ROT13Text   = convertSentence(  convertMe   );
                           System.out.printf( "Original:    %s\nConverted:  %s\n",  convertMe,  ROT13Text   );
                        }
    /**
        *   getSentences
        *
        *   This    method  allows  the user    to  enter   text    into    each    of  the
        *   elements    of  the String  array   that    it  receives.
        *
        *   @param                      sentences                           An  array   of  String[]    data
        *   @return                 None
        */
        public static void getSentences( String[] sentences) {
            System.out.println("Enter your five sentences below");
            for (int i = 0; i < sentences.length; i++) {
            System.out.printf("Sentence" + " " + (i+1) + " >");
            sentences[i] = input.nextLine();
            }
        }
        /**
        *   displayOriginal
        *
        *   This    method  displays    all of  the elements    of  the array   of
        *   String  data    that    it  receives,   line    by  line    (element    by  element).
        *
        *   @param                      sentences                           An  array   of  String[]    data
        *   @return                 None
        */
        public static void  displayOriginal (String[]   sentences) {
      System.out.println ("The original text:");
            for (int i = 0; i < 5; i++) {
    System.out.println(sentences[i]);
            }
        }
        /**
            *   charConvert
            *
            *   This    method  will    take    one char    value   as  a   parameter   and convert
            *   it  to  its appropriate ROT13   equivalent. The return value    will    be  the
            *   new ROT13   char    equivalent.
            *
            *   This    method  will    not do  any output.
            *
            *   @param                      toConvert                           A   character   to  convert as  a   char
            *   @return                 The new ROT13   equivalent  value   as  a   char
            */
            public static char charConvert(char toConvert) {
                char c = toConvert;
        if  (c >= 'a' && c <= 'm') c += 13;
        else if  (c >= 'A' && c <= 'M') c += 13;
        else if  (c >= 'n' && c <= 'z') c -= 13;
        else if  (c >= 'N' && c <= 'Z') c -= 13;
                return c;
            }
        /**
            *   convertSentence
            *
            *   This    method  will    do  the actual  conversion  of  a   String  of  data    to  its
            *   ROT13   equivalent  in  5-character chunks  of  data.       It  should  call    on
            *   the charConvert()   method  to  do  the actual  character   conversion  for each
            *   individual  character.      In  other   words,  individual  character   conversion
            *   should  not happen  within  this    method.
            *
            *   This    method  will    not do  any output.
            *
            *   @param                      sentence                A   String  variable    to  convert
            *   @return                 The 5-characters    in  a   group   ROT13   result  as  a   String
            */
            public static String convertSentence(String x) {
                for (int i = 0; i < x.length(); i++) {
                char y = (charConvert(x.charAt(i)));
                x = x + y;
                }
                return x;
            }
    }
4

1 に答える 1

0

元の文字列に変換された文字を追加しないでください。新しい文字列を使用して、これらの変換された文字を保持する必要があります。

public static String convertSentence(String x) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < x.length(); i++) {
       char y = (charConvert(x.charAt(i)));
       sb.append(y);
    }
    return sb.toString();
}
于 2016-06-12T04:49:12.213 に答える