陷入无尽的输入循环 - 使用方法的ROT13加密。

时间:2016-06-12 03:03:12

标签: java arrays methods rot13

所以,我的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;
            }
    }

1 个答案:

答案 0 :(得分: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();
}