gpt4 book ai didi

java - 如何使密文字符与明文字符相同?

转载 作者:行者123 更新时间:2023-12-01 13:17:49 24 4
gpt4 key购买 nike

package edu.secretcode;

import java.util.Scanner;

/**
* Creates the secret code class.
*
* @author
*
*/
public class SecretCode {
/**
* Perform the ROT13 operation
*
* @param plainText
* the text to encode
* @return the rot13'd encoding of plainText
*/

public static String rotate13(String plainText) {
StringBuffer cryptText = new StringBuffer("");
for (int i = 0; i < plainText.length() - 1; i++) {
char currentChar = plainText.charAt(i);
currentChar = (char) ((char) (currentChar - 'A' + 13)% 26 + 'A');
cryptText.append(currentChar);
if (currentChar <= 'A' && currentChar >= 'Z'){
cryptText.append(plainText);
}

}
return cryptText.toString();

}

/**
* Main method of the SecretCode class
*
* @param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (1 > 0) {
System.out.println("Enter plain text to encode, or QUIT to end");
Scanner keyboard = new Scanner(System.in);
String plainText = keyboard.nextLine();
if (plainText.equals("QUIT")) {
break;
}
String cryptText = SecretCode.rotate13(plainText);
String encodedText = SecretCode.rotate13(plainText);

System.out.println("Encoded Text: " + encodedText);
}

}

}

在静态字符串rotate13方法和if语句中,如果字符小于'A'或大于'Z',则使密文字符与明文字符相同。我的问题是如何使密文字符与明文字符相同?我所拥有的不起作用,我完全陷入困境。任何意见是极大的赞赏。提前致谢。

最佳答案

你的条件是错误的..更改

if (currentChar <= 'A' && currentChar >= 'Z')

if (currentChar < 'A' || currentChar > 'Z')

关于java - 如何使密文字符与明文字符相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22325602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com