-1

编辑:我稍微更改了代码。我试着让它更整洁......

好的,我是 Java 的完整初学者。如果您必须将消息中的字母移动一定量,我必须制作一个 CaesaerCipher(例如:shift 1 = A 变为 B,B 变为 C,等等。 shift 2 = A->C,B->D, ETC。)

我已经尝试了很多方法,但我绝对无济于事。我不知道该尝试什么。帮助?

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”

4

1 回答 1

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() );
于 2012-08-06T18:06:48.653 回答