简体   繁体   English

构造函数参数值未实现

[英]Constructor Parameter Value Not Implemented

I'm creating a very simple encoder that will shuffle the characters in a string. 我正在创建一个非常简单的编码器,它将对字符串中的字符进行混洗。 I've written it to split this string in half, forming two new variables. 我已经写好了将这个字符串分成两半,形成两个新变量。 The user chooses the number of shuffles they want and that is passed as a parameter in the new class constructor -- which should then use that shuffle value throughout the class. 用户选择所需的随机播放次数,并将其作为参数传递给新的类构造函数-然后,该变量应在整个类中使用该随机播放值。 Mine is not. 我的不是。 The shuffleEncryption method is using the class variable, 0, instead. shuffleEncryption方法使用类变量0代替。 I know this must be something very obvious, but I am not catching it. 我知道这一定很明显,但是我没有抓住。 :/ :/

//From Main Class //来自主类

        System.out.println("Enter message to encrypt: ");
        String message = input.next();
        System.out.print("Number of shuffles: " );
        int numShuffles = input.nextInt();
        ShuffleCipher shuffle = new ShuffleCipher(numShuffles);
        System.out.println(shuffle.encode(message));

//The shuffle class //随机播放类

public class ShuffleCipher implements MessageEncoder {
int shuffle;

public ShuffleCipher(int shuffle) {
    shuffle = this.shuffle;     
}

private String shuffleEncryption(String str) {
   int middle = str.length()/2;
   int loop = 1;
   System.out.println("shift" + shuffle);
   StringBuilder sb = new StringBuilder();

   do {
        String firstHalf = str.substring(0, middle);
        System.out.println("first:" + firstHalf);
        String secondHalf = str.substring(middle);
        System.out.println("second:" + secondHalf);

        for(int i = 0, j = 0; i < firstHalf.length(); i++, j++) {
            sb = sb.append(secondHalf.charAt(i));

           if(j < secondHalf.length()) {
           sb = sb.append(firstHalf.charAt(i));           
       }
       str = sb.toString();
   }
   loop++;
   } while (loop <= shuffle);

    return str;
 } 

@Override
public String encode(String plainText) {
    String shuffled; 
    shuffled = shuffleEncryption(plainText);
    return shuffled;
 }
}

You are not setting the shuffle member variable in the constructor. 您没有在构造函数中设置shuffle成员变量。

Change this:- 改变这个:

public ShuffleCipher(int shuffle) {
    shuffle = this.shuffle;     
}

to this:- 为此:

public ShuffleCipher(int shuffle) {
    this.shuffle = shuffle;     
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM