0

プログラムのすべてのエラーにコメントしました。私のプログラムのポイントは、ユーザーが入力したものによって入力ファイルをシフトすることです。私がコメントアウトしたエラーは、私にとっては意味のないものです。なぜなら、それはすべて理にかなっていて、コンピューターは私が望むようにそれを読み取っていないからです。トークンの 1 つが "," であるとコメントしたエラーの 1 つと混同しないでください。それ以外は「(」と「)」です。これらはすべて、コメントした行のどこかにセミコロンが必要なエラーです。

プログラムは次のようになります。

import java.util.*;
import java.io.*;

class CaesarCipher
{
 public static void main (String [] args) throws FileNotFoundException
 {
  Scanner keyboard = new Scanner(System.in);
  System.out.println("What shift should I use? ");
  int shift = keyboard.nextInt();
  System.out.println("What is the name of the input file? ");
  String name = keyboard.next();
  File f = new File(name);
  Scanner inFile = new Scanner(f);
  System.out.println("What is the name of the output file? ");
  String text = keyboard.nextLine();
  PrintWriter outFile = new PrintWriter(text);
  String encrypted = "";

  while (inFile.hasNextLine())
  {
     String line = inFile.nextLine();
      if ( shift == 1)
     encrypted = caesarEncipher(line, shift);
      else if (shift == 2)
     encrypted = caesarDecipher(line, shift);// the method caesarDecipher(java.lang.String, int) is undefined for the type CaesarCipher
      System.out.println(encrypted);
      outFile.println(encrypted);
  }
 }
  static String caesarEncipher(String text ,int shift) throws FileNotFoundException
  {
   String t = "";
   int i = 0;
   while (i < t.length())
   {  
    if (shift < 0)
    {
        shift = (shift % 26) + 26;
     }
        int move = (char) ((text.charAt(0) - 'A' + shift) % 26 + 'A');
        t += move;
        i++;
        System.out.println(t);
        outFile.println(t);
        return "DONE!";
     }
                                                          // for each token listed, it expects the semi colon.
     static String caesarDecipher(String text, int shift) throws FileNotFoundException // Syntax error on token "(", "," , ")", ; expected      
     {
      return caesarEncipher(input, -shift);
      }
     }
    }
4

4 に答える 4

3

メソッドcaesarDecipher定義は method に埋め込まれcaesarEncipherます。それは正当な Java ではなく、貧弱なコンパイラを混乱させました。

厳密なインデントは、この種のことを非常に明確にします。IDE または emacs を使用している場合は、ファイル全体を再インデントするためのツールを探します (Unix にはコマンド ライン ツールもあります)。

Eclipse の場合: Ctrl+ Shift+F

Emacs の場合: 領域 (ファイル全体) を強調表示してから: Esc Ctrl+\またはAlt+ Ctrl+\

于 2012-08-07T16:28:55.237 に答える
2

メソッド caesarEncipher で終了の } を逃しました。これらのエラーをより迅速に発見する意図を使用してください。

于 2012-08-07T16:29:35.767 に答える
0

あなたはあなた}の最後に行方不明です

static String caesarEncipher(String text ,int shift) throws FileNotFoundException

そして、あなたは最後に余分なものを持っています

static String caesarDecipher(String text, int shift) throws FileNotFoundException

また、この ^ メソッドで戻るときはinput、このメソッドで未定義の変数を使用していることにも注意してください。多分あなたはtextどちらがあなたの主張であるかを言及します

于 2012-08-07T16:30:56.310 に答える
0

前述のように、閉じ括弧がありません。これらの間違いを見つけるには、 IDEを使用することをお勧めします。多くの人が日食を好みますが、個人的にはIntellijが好きです。Eclipse は無料ですが、完全版の intellij は無料ではありません。

于 2012-08-07T16:31:00.643 に答える