正确编码大写字母 package edu.secretcode; 导入java.util.Scanner; /** *创建密码类。 * *@作者 * */ 公共类加密码{ /** *执行ROT13操作 * *@param纯文本 *要编码的文本 *@返回纯文本的rot13'd编码 */ 公共静态字符串13(字符串纯文本){ StringBuffer cryptText=新的StringBuffer(“”); 对于(int i=0;i0){ System.out.println(“输入要编码的纯文本,或退出以结束”); 扫描仪键盘=新扫描仪(System.in); 字符串纯文本=keyboard.nextLine(); if(明文等于(“退出”)){ 打破 } 字符串cryptText=SecretCode.rotate13(纯文本); 字符串encodedText=SecretCode.rotate13(纯文本); System.out.println(“编码文本:“+encodedText”); } } }
大家好,我希望这个程序能够对大写字母进行编码,而不使用其他字符,并通过输出传递它们。例如,运行程序“URYY\d_YQ!”后,“HELLO WORLD!”应该变成“URYY\d_YQ!”,我得到的是yluhreylbowjwboerylqdxk,而不是我应该得到的“URYY\d_YQ!”。如果有人能让我知道我做错了什么,我将不胜感激。提前感谢。如果使用字符串匹配正则表达式,会更容易。 首先,在循环条件中,消除正确编码大写字母 package edu.secretcode; 导入java.util.Scanner; /** *创建密码类。 * *@作者 * */ 公共类加密码{ /** *执行ROT13操作 * *@param纯文本 *要编码的文本 *@返回纯文本的rot13'd编码 */ 公共静态字符串13(字符串纯文本){ StringBuffer cryptText=新的StringBuffer(“”); 对于(int i=0;i0){ System.out.println(“输入要编码的纯文本,或退出以结束”); 扫描仪键盘=新扫描仪(System.in); 字符串纯文本=keyboard.nextLine(); if(明文等于(“退出”)){ 打破 } 字符串cryptText=SecretCode.rotate13(纯文本); 字符串encodedText=SecretCode.rotate13(纯文本); System.out.println(“编码文本:“+encodedText”); } } },java,encode,Java,Encode,大家好,我希望这个程序能够对大写字母进行编码,而不使用其他字符,并通过输出传递它们。例如,运行程序“URYY\d_YQ!”后,“HELLO WORLD!”应该变成“URYY\d_YQ!”,我得到的是yluhreylbowjwboerylqdxk,而不是我应该得到的“URYY\d_YQ!”。如果有人能让我知道我做错了什么,我将不胜感激。提前感谢。如果使用字符串匹配正则表达式,会更容易。 首先,在循环条件中,消除-1。否则,你只能进入倒数第二个角色 接下来,消除cryptText.append(cu
-1
。否则,你只能进入倒数第二个角色
接下来,消除cryptText.append(currentChar)。
那么,用这个
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.charAt(i));
}
else {
currentChar = (char) ((char) (currentChar - 'A' + 13) % 26 + 'A');
cryptText.append(currentChar);
}
}
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);
}
}
}
这实际上是可行的,只是我从你想要的角色中得到了一些不同的角色。你好,世界!=>URYYB JBEYQ 如果(currentChar='Z')对于小写字符为false,则正确编码大写字母的ll实际上是HELLO WORLD!!!我想得到编码文本:URYY \-d\uyq。。。你知道怎么做吗。谢谢你的帮助。非常感谢。这一定是在您计算currentChar
时,因为我的代码片段使用了您计算的currentChar
是的,我正在尝试修复它!是我现在得到的,仍然缺少-
,只是为了让您知道,如果您将%26更改为%255,它将返回URYY\d\YQ!
char currentChar = plainText.charAt(i);
String cS = currentChar+"";
currentChar = (char) ((char) (currentChar - (int)'A' + 13) % 26 + (int)'A');
if (!cS.matches("[A-Z]")) {
cryptText.append(plainText.charAt(i));
}
else {
cryptText.append(currentChar);
}