Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String for循环中的返回字符串(来自chars)_String_Loops_For Loop_Return - Fatal编程技术网

String for循环中的返回字符串(来自chars)

String for循环中的返回字符串(来自chars),string,loops,for-loop,return,String,Loops,For Loop,Return,我正在尝试创建一个简单的程序来使用凯瑟密码。目前我的问题是,我不知道如何(或者是否可能)从for循环返回完整的字符串 我所知道的(我想……) 已加密的返回结束for循环,因为它是一个返回。。 如果我没有返回,而是输入System.out.print(加密),它将打印整个字符串(但它由字符组成,不是实际的字符串) 我想知道如何返回一个实现了所有“密码”的字符串。在这种情况下,它应该是“BCE” 一个解释会很有帮助!谢谢 public class crypto { public stati

我正在尝试创建一个简单的程序来使用凯瑟密码。目前我的问题是,我不知道如何(或者是否可能)从for循环返回完整的字符串

我所知道的(我想……) 已加密的返回结束for循环,因为它是一个返回。。 如果我没有返回,而是输入System.out.print(加密),它将打印整个字符串(但它由字符组成,不是实际的字符串)

我想知道如何返回一个实现了所有“密码”的字符串。在这种情况下,它应该是“BCE”

一个解释会很有帮助!谢谢

public class crypto {

    public static void main(String[] args) {

        String text = normalizeText("ABD");
        String encrypted = ceaserCipher(text, 1);

        System.out.println(encrypted);

    }//main

    public static String normalizeText(String text) {

        text = text.replaceAll("[^A-Za-z]+", "").toUpperCase(); 
        return text;

    }//normalizeText

    public static String ceaserCipher(String text, int key) {

        for (int index = 0; index < text.length(); index++) {
        char start = text.charAt(index);
        char cipherChar = (char) (start + key);
        String Ciphered = "";
        Ciphered = Ciphered + cipherChar; 

        return Ciphered;
        }

        return null;
    }//ceaserCipher

}//class
公共类加密{
公共静态void main(字符串[]args){
字符串文本=normalizeText(“ABD”);
加密字符串=ceaserCipher(文本,1);
System.out.println(加密);
}//主要
公共静态字符串normalizeText(字符串文本){
text=text.replaceAll(“[^A-Za-z]+”,”).toUpperCase();
返回文本;
}//规范化文本
公共静态字符串ceaserCipher(字符串文本,int键){
对于(int index=0;index
我所做的是将我移动到循环外部
加密
,否则每次在循环中都会创建它并返回到外部,否则它将在向字符串添加第一个字符后立即返回。在这里,它等待循环完成执行,然后返回字符串 替换此方法

    public static String ceaserCipher(String text, int key) {
    String Ciphered = "";

    for (int index = 0; index < text.length(); index++) {
    char start = text.charAt(index);
    char cipherChar = (char) (start + key);
    Ciphered = Ciphered + cipherChar; 

    }

    return Ciphered;
}//ceaserCipher
publicstaticstringceasercipher(字符串文本,int键){
字符串加密=”;
对于(int index=0;index
  • 通过在循环内为每个字符初始化
    Ciphered
    Ciphered
    字符串将返回到“”。因此,将声明置于循环之外
  • 如果在循环中返回
    Ciphered
    ,它将立即返回,并且不会运行下一次迭代。因此,将return语句也移到循环之外
  • 您的代码应该是:

    public static String ceaserCipher(String text, int key) 
    { 
    String Ciphered="";
    for (int index = 0; index < text.length(); index++) 
    { 
    char start = text.charAt(index);
    char cipherChar = (char) (start + key); 
    Ciphered = Ciphered + cipherChar;
    } 
    return Ciphered; 
    }//ceaserCipher
    
    publicstaticstringceasercipher(字符串文本,int键)
    { 
    字符串加密=”;
    对于(int index=0;index
    为什么要返回循环中?此外,toString()是否不起作用?for循环中的代码是“应用”密码更改的代码。我不认为你能从for循环中得到for循环中创建的变量(对还是错?)。不太清楚.toString()的用法,但我会尝试一下,这很简单!非常感谢。