修改:我稍微更改了代码。我试着让它更整洁......
好的,我是Java的初学者。我必须制作一个CaesaerCipher,你必须在消息中移动一定数量的字母(例如:移位1 = A变为B,B变为C,等等。移位2 = A-> C,B-> D等。)
我尝试了很多方法,而且我无处可去。我不知道该尝试什么。帮助
import java.util.*;
import java.io.*;
public class CaesarCipher {
public static void main(String[] args)
throws FileNotFoundException, IOException {
System.out.println("Welcome to CaesarCipher");
System.out.print("Enter 1 to encipher, or 2 to decipher (-1 to exit): " );
Scanner console = new Scanner(System.in);
int i = console.nextInt();
if (i == -1) {
System.out.print("DONE!");
//repeat question encipher/decipher/quit until choose quit
}
if (i == 1) {
System.out.println();
System.out.print("What shift should I use? ");
int j = console.nextInt();
System.out.println();
System.out.print("What is the input file name? ");
caesarEncipher();
if (i == 1) { //from answer to encipher, decipher, or exit
System.out.println(strLine);
//send to String caesarDecipher
//?????????????
}
// if (i == 2) { //from answer to encipher, decipher, or exit
//send to String caesarEncipher
//?????????????
// }
System.out.print("What is the output file name? ");
String fileName = new Scanner(System.in).nextLine();
//File file = new File( fileName );
// fstream = new FileInputStream("/Users/steph/Desktop/Programming/!6 Problem Set TUES/PartB/6/cipher.txt");
//change (".txt") to -fileName- ???
//File file = new File( fileName );
// while ((strLine = br.readLine()) != null) {
// System.out.println (strLine);
}
}
public static String caesarEncipher (int k)
throws FileNotFoundException, IOException {
//input file name code...
//String fileName = scanner.nextLine();
FileInputStream fstream = new FileInputStream("/Users/steph/Desktop/Programming/!6 Problem Set TUES/PartB/6/cipher.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
System.out.println (strLine);
}
in.close();
System.out.println();
int shiftKey = Integer.parseInt(k[1]);
shiftKey = shiftKey % 26;
String cipherText = "";
for (k=0; k<strLine.length(); k++){
int asciiValue = (int) strLine.charAt(k);
if (asciiValue < 65 || asciiValue > 90){
cipherText += strLine.charAt(k);
continue;
}
int basicValue = asciiValue - 65;
int newAsciiValue = 65 + ((basicValue + shiftKey) % 26) ;
cipherText += (char) newAsciiValue;
}
System.out.print(cipherText);
in.close();
System.out.println();
}
}
//ignore any character that is not an uppercase alphabetic!!!!!!!!!
// public static String caesarDecipher (String input, int shift) {
//reverse process to decipher
//go back to what shift (line 11-12): j
//make it -inputted number
//ignore any character that is not an uppercase alphabetic!!!!!!!!!
// }
// public static String alphabet (?) {
// Iterator<String> abc = new Abc<Integer>()??;
//list [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, Y, Z]
//rotate(list, distance)
编辑:问题出在我加星标的地方。它希望k成为一个数组。我不知道如何在不搞砸其余代码的情况下做到这一点 至于学习,你会认为所有这些代码都足够了。我知道只有BBCode(这是什么都没有)。我是一名心理专业的学生。我的大脑不会这样工作。这很有趣,但我遇到了一堵砖墙。
Edit2:确切的错误是“需要数组,但找到了int”
答案 0 :(得分:1)
使用StringBuilder尝试此实现。它是专门为多次修改的字符串而制作的类 编辑:此外,你最好将你的字符串转换为字符数组,因为你无论如何都要逐字处理它。
StringBuilder cipherText = new StringBuilder();
char[] letters = strLine.toCharArray();
for (k=0; k < letters.length; k++){
int asciiValue = (int) letters[k];
if (asciiValue < 65 || asciiValue > 90){
cipherText.append( letters[k] );
continue;
}
int basicValue = asciiValue - 65;
int newAsciiValue = 65 + ((basicValue + shiftKey) % 26) ;
cipherText.append( (char) newAsciiValue );
}
System.out.print( cipherText.toString() );