【问题标题】:How to print String如何打印字符串
【发布时间】:2015-12-31 16:44:26
【问题描述】:

我有这个我想返回的字符串,但我不能,因为它说“打印”不能被解析为变量。这是我的代码:

public static String enrcyptText(String str, int shift){
        int count = 0;
        String[] parts = str.split("[\\W]");
        for(String word : parts){ 
                shift = shift * (count + 1) + 1;
                count++;
                encryptWord(word, shift);
                String[] phrase = new String[]{word};
                String print = String.join(" ", phrase);
            }
        return print;
    }

有什么想法吗?

【问题讨论】:

  • 在开头声明变量,一个变量一般在作用域外是不可见的{ }
  • 正如 Lorenzo 所说,变量 print 仅存在于 for 循环中。在 for 循环之前定义它,您将能够打印它。

标签: java string for-loop return


【解决方案1】:

您的代码中有一个逻辑错误:您正确加密了每个单词,但您没有正确构建加密的短语。在循环的每次迭代中,当您应该将元素添加到 phrase 数组时,您都在重新创建短语。

public static String enrcyptText(String str, int shift) {
    int count = 0;
    String[] parts = str.split("[\\W]");
    String[] phrase = new String[parts.length]; // initialising an array containing each encrypted word
    for (String word : parts) {
        shift = shift * (count + 1) + 1;
        count++;
        String encryptedWord = encryptWord(word, shift);
        phrase[count - 1] =  encryptedWord; // updating the encrypted phrase array
    }
    return String.join(" ", phrase); // joining the phrase array
}

在这段代码中,我们在循环之前创建了一个phrase 数组。在每次迭代中,我们用加密的单词更新这个数组。当我们拥有所有加密单词时,循环终止,我们将所有部分连接在一起。

我也猜测encryptedWord 实际上返回了加密的单词。此方法不能修改作为参数给出的单词。

【讨论】:

    【解决方案2】:

    那里有几个问题。

    1. 您只在循环体内声明了print。它不存在于它之外。因此,您需要将String print 移出循环。

    2. 您还在每次循环迭代中为其赋值,这将覆盖它之前的值。目前还不清楚你想要做什么,但你不会想要那样做。

    3. 这两行也没有任何意义:

      String[] phrase = new String[]{word};
      String print = String.join(" ", phrase);
      

      由于phrase 中只有一个条目,因此您最终会得到具有与word 相同的值的print

    4. 您似乎期望encryptWord 可以修改传递给它的字符串。不能。

    试一试,我认为您的目标是“加密”句子中的单个单词,然后将结果重新组合成以空格分隔的一组加密单词。如果是这样,请参阅 cmets:

    public static String enrcyptText(String str, int shift){
        int count = 0;
        String[] parts = str.split("[\\W]");
        // For updating the array, better to use the classic
        // for loop instead of the enhanced for loop
        for (int i = 0; i < parts.length; ++i){ 
            shift = shift * (count + 1) + 1;
            count++;                      // Could do this before previous line and remove the + 1 in (count + 1)
            parts[i] = encryptWord(parts[i], shift); // See note below
        }
        return String.join(" ", parts);
    }
    

    请注意,我使用的是来自encryptWord 的返回值。这是因为 Java 中的字符串是不可变的(无法更改),因此encryptWord 无法更改我们传递给它的内容;它只能给我们一个新的字符串来代替。

    【讨论】:

    • 谢谢你这个工作除了我必须在 encryptWord 方法中使用的移位值我必须声明另一个值以与公式相等 (shift * (count + 1) + 1)否则我会得到一个错误的结果。(那是我的错:D)
    【解决方案3】:

    print 变量在大括号内有作用域。您应该将 print 变量移到大括号外以使其对代码可见。此外,由于它是一个局部变量,打印应该使用默认值初始化(在我的情况,它是空的)。编译器会抱怨打印仍未初始化(尽管这与主要问题无关)

    public static String enrcyptText(String str, int shift){
            int count = 0;
            String[] parts = str.split("[\\W]");
            String print = null;
            for(String word : parts){ 
                    shift = shift * (count + 1) + 1;
                    count++;
                    encryptWord(word, shift);
                    String[] phrase = new String[]{word};
                    print = String.join(" ", phrase);
                }
            return print;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多