下面你会发现我使用 toCharArray 将字符串发送到数组。然后我使用 for 语句移动字母的值...
for(i = 0; i < letter.length; i++){
letter[i] += (shiftCode);
System.out.print(letter[i]);
}
但是,当我使用 shiftCode 移动值时,例如...
a 移位 -1;我得到一个符号@。有没有办法将字符串发送到 shiftCode 或告诉 shiftCode 只使用字母?我需要它来查看我的文本,比如“aaron”,当我使用 for 语句时,只遍历 az 并忽略所有符号和数字。我认为这很简单...
letter=codeWord.toCharArray(a,z);
但是尝试不同的形式并在谷歌上搜索它并没有给我任何结果。也许它与正则表达式或其他东西有关?您将在下面找到我的程序的完整副本;它完全按照我想要的方式工作;但它会遍历字母和符号。我还尝试在线查找 toCharArray 的说明,但如果存在任何参数,我无法找到它们。
我的程序...
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* Aaron L. Jones
* CS219
* AaronJonesProg3
*
* This program is designed to -
* Work as a Ceasar Cipher
*/
/**
*
* Aaron Jones
*/
public class AaronJonesProg3 {
static String codeWord;
static int shiftCode;
static int i;
static char[] letter;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// Instantiating that Buffer Class
// We are going to use this to read data from the user; in buffer
// For performance related reasons
BufferedReader reader;
// Building the reader variable here
// Just a basic input buffer (Holds things for us)
reader = new BufferedReader(new InputStreamReader(System.in));
// Java speaks to us here / We get it to query our user
System.out.print("Please enter text to encrypt: ");
// Try to get their input here
try {
// Get their codeword using the reader
codeWord = reader.readLine();
// Make that input upper case
codeWord = codeWord.toUpperCase();
// Cut the white space out
codeWord = codeWord.replaceAll("\\s","");
// Make it all a character array
letter = codeWord.toCharArray();
}
// If they messed up the input we let them know here and end the prog.
catch(Throwable t) {
System.out.println(t.toString());
System.out.println("You broke it. But you impressed me because"
+ "I don't know how you did it!");
}
// Java Speaks / Lets get their desired shift value
System.out.print("Please enter the shift value: ");
// Try for their input
try {
// We get their number here
shiftCode = Integer.parseInt(reader.readLine());
}
// Again; if the user broke it. We let them know.
catch(java.lang.NumberFormatException ioe) {
System.out.println(ioe.toString());
System.out.println("How did you break this? Use a number next time!");
}
for(i = 0; i < letter.length; i++){
letter[i] += (shiftCode);
System.out.print(letter[i]);
}
System.out.println();
/****************************************************************
****************************************************************
***************************************************************/
// Java speaks to us here / We get it to query our user
System.out.print("Please enter text to decrypt: ");
// Try to get their input here
try {
// Get their codeword using the reader
codeWord = reader.readLine();
// Make that input upper case
codeWord = codeWord.toUpperCase();
// Cut the white space out
codeWord = codeWord.replaceAll("\\s","");
// Make it all a character array
letter = codeWord.toCharArray();
}
// If they messed up the input we let them know here and end the prog.
catch(Throwable t) {
System.out.println(t.toString());
System.out.println("You broke it. But you impressed me because"
+ "I don't know how you did it!");
}
// Java Speaks / Lets get their desired shift value
System.out.print("Please enter the shift value: ");
// Try for their input
try {
// We get their number here
shiftCode = Integer.parseInt(reader.readLine());
}
// Again; if the user broke it. We let them know.
catch(java.lang.NumberFormatException ioe) {
System.out.println(ioe.toString());
System.out.println("How did you break this? Use a number next time!");
}
for(i = 0; i < letter.length; i++){
letter[i] += (shiftCode);
System.out.print(letter[i]);
}
System.out.println();
}
}