Java密码程序

时间:2014-04-05 18:43:31

标签: java encryption

首先,这里是我要整理的程序的说明......

提供主要方法。这应该:  获取字符串和移位值的输入  转换为上限 案件  仅对A和Z之间的字母字符执行以下项目  利用一个使用后缀递增运算符的for循环 Ø 将字符转换为ASCII等效值(类型转换) Ø 班次b 上面输入的y移位值  如果你达到alphabe的结尾 环绕  示例:向左移动2将变为Y. Ø 转换回其等效字符(类型转换) Ø 输出新角色  获取字符串和移位值的输入  执行上述相同步骤将加密文本转换回纯文本  请务必再次输入,因为可能会输入不同的加密文本  主方法可以调用单独的方法来执行加密/解密 不需要。 利用后缀增量/减量运算和复合赋值运算符 对于所有数学。 Ë xample:x ++或x + = 2。

我到目前为止的代码是:

/*
JursekGregChapter12t.java
Greg Jursek

This program will encrypt entered text by a user input 
shift value and then decrypt text in the same manner.
*/

import java.lang.*;

import java.util.*;

public class JursekGregChapter12t
{
    public static void main(String[] args)
{
    Scanner stdIn = new Scanner(System.in);
    String encryptText; // text to be encrypted
    String decryptText; // text to be decrypted
    int shiftValue; // the number of spaces that will be shifted via user input


    // User enters plain text for encryption
    System.out.print("Please enter text to encrypt");
    encryptText = stdIn.nextString();

    // User enters a shift value
    System.out.println("Please enter shift value");
    shiftValue = stdIn.nextInt();

    // System prints the encrypted text
    String encryptedText = encrypt(encryptText , shiftValue); // text that has been encrypted
    System.out.println(encryptedText);

    // User enters text for decryption
    System.out.print("Please enter text to decrypt");
    decryptText = stdIn.nextString();

    // User enters a shift value
    System.out.println("Please enter shift value");
    shiftValue = stdIn.nextInt();

    // System prints the decrypted text
    String decryptedText = decrypt(decryptText , shiftValue); // text that has been decrypted
    System.out.println(decryptedText);
} // end main


        // Shift and Character Manipulation
        public static String shift(String enteredText, int shiftValue) 
        {

            String convertedText = "";
            for(int i = 0; i< enteredText.length(); i++)
            {
                char lowerCase = enteredText.charAT(i);

                //Convert to upper case letters
                char lowerCase = Character.toUpperCase(lowerCase);
                int charNumber = upperLetter;

                //Shift letters and wrap text
                int rotateLetters = (charNumber + shiftValue) % 26;
                char shiftLetters = (char) rotateShift;

                //Populate new string of characters
                convertedText += shiftLetters;
            }
        return convertedText;
        }

        // Encryption code
        public static String encrypt(String enteredText, int shiftValue);
        {
            String encryptedString = rotate(enteredText , shiftValue);
            return encryptedString;
        }

        // Decryption code
        public static String decrypt(String enteredText, int shiftValue);
        {
            int negativeShiftValue = (-1) * (shiftValue);
            String decryptedString = rotate(enteredText , negativeShiftValue);
            return decryptedString;
        }


} //end class JursekGregChapter12t

此时我遇到了很多奇怪的错误,我不太确定出了什么问题......这些错误是:

/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:24: error: cannot find symbol
        encryptText = stdIn.nextString();
                           ^
  symbol:   method nextString()
  location: variable stdIn of type Scanner
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:36: error: cannot find symbol
        decryptText = stdIn.nextString();
                           ^
  symbol:   method nextString()
  location: variable stdIn of type Scanner
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:55: error: cannot find symbol
                    char lowerCase = enteredText.charAT(i);
                                                ^
  symbol:   method charAT(int)
  location: variable enteredText of type String
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:58: error: variable lowerCase is already defined in method shift(String,int)
                    char lowerCase = Character.toUpperCase(lowerCase);
                         ^
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:59: error: cannot find symbol
                    int charNumber = upperLetter;
                                     ^
  symbol:   variable upperLetter
  location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:63: error: cannot find symbol
                    char shiftLetters = (char) rotateShift;
                                               ^
  symbol:   variable rotateShift
  location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:72: error: missing method body, or declare abstract
            public static String encrypt(String enteredText, int shiftValue);
                                 ^
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:74: error: cannot find symbol
                String encryptedString = rotate(enteredText , shiftValue);
                                                ^
  symbol:   variable enteredText
  location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:74: error: cannot find symbol
                String encryptedString = rotate(enteredText , shiftValue);
                                                              ^
  symbol:   variable shiftValue
  location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:75: error: return outside method
                return encryptedString;
                ^
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:79: error: missing method body, or declare abstract
            public static String decrypt(String enteredText, int shiftValue);
                                 ^
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:81: error: cannot find symbol
                int negativeShiftValue = (-1) * (shiftValue);
                                                 ^
  symbol:   variable shiftValue
  location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:81: error: illegal start of type
                int negativeShiftValue = (-1) * (shiftValue);
                                                ^
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:82: error: cannot find symbol
                String decryptedString = rotate(enteredText , negativeShiftValue);
                                                ^
  symbol:   variable enteredText
  location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:83: error: return outside method
                return decryptedString;
                ^
15 errors
[Finished in 0.7s with exit code 1]

感谢任何帮助。

感谢。

解决了一些错误后:

/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:56: error: cannot find symbol
                char lowerCase = enteredText.charAT(i);
                                            ^
  symbol:   method charAT(int)
  location: variable enteredText of type String
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:59: error: variable      lowerCase is already defined in method shift(String,int)
                char lowerCase = Character.toUpperCase(lowerCase);    
                         ^
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:60: error: cannot find symbol
                int charNumber = upperLetter;
                                     ^
  symbol:   variable upperLetter
  location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:64: error: cannot find symbol
                    char shiftLetters = (char) rotateShift;
                                               ^
  symbol:   variable rotateShift
  location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:75: error: cannot find symbol
                String encryptedString = rotate(enteredText , shiftValue);
                                         ^
      symbol:   method rotate(String,int)
  location: class JursekGregChapter12t
/Users/Greg/Documents/Programming/Java/JursekGregChapter12t.java:83: error: cannot find symbol
                String decryptedString = rotate(enteredText , negativeShiftValue);
                                         ^
  symbol:   method rotate(String,int)
  location: class JursekGregChapter12t
6 errors
[Finished in 0.7s with exit code 1]

我花了最近2个小时阅读的内容,我仍然无法理解我的代码是什么让我回到这里。我是一个新手程序员,Java是我第一次涉足它并且我完全陷入困境。有没有人有任何想法?

1 个答案:

答案 0 :(得分:1)

这些是编译器错误,每一个都会告诉你到底出了什么问题。这里是问题和解决方案的快速列表(没有为您编写所有代码):

  • cannot find symbol:这意味着您使用了未定义的名称。例如,在第24行,您使用stdIn.nextString(),但Scanner个对象没有名为nextString的方法(您可能需要next()nextLine()) 。因此,编译器&#34;找不到符号&#34; nextString,因为它不存在(或charAT,因为大写错误)。
  • variable X is already defined:这正是它听起来的意思。当您收到此错误时,因为您已经两次声明变量。在declare变量(例如char lowercase)后,您只需在该范围内使用它(例如lowercase = 'a';),而无需再次声明。
  • missing method body:这意味着您已经声明了方法存根。除非它们是抽象的,否则所有方法必须都有一个用大括号括起来的主体,并且必须返回它们声明要返回的类型。在您的特定情况下,您只需要在encryptdecrypt声明的末尾删除分号。
  • return outside method表示您有一个return语句在一个类中独立浮动,这没有任何意义。在这种情况下,一旦你摆脱了encryptdecrypt末尾的分号,这个问题就会消失,这样这些块就会被识别为方法体。
  • illegal start of type是一个更微妙的错误,但在你的情况下,它刚刚发生,因为这些块目前处于类级而不是方法体。修复return outside method问题后,问题就会消失。