我对不同符号的IF和IF ELSE语句因某些原因而无法正常工作。我无法弄清楚为什么。当我输入“abcd:;”对于带有偏移“1”的文本和选择加密,我收到错误
java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Lab2.main(Lab2.java:132)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
当我输入“abcd:;”时对于带有偏移“1”的文本和选择加密,我得到“BCDE- ^”。似乎空间正在引发问题。
import java.io.*;
import java.util.Scanner;
public class Lab2
{
public static String encode(String original, int offset)
{
// declare constants
final int ALPHABET_SIZE = 26; // used to wrap around A-Z
String encoded = ""; // base for string to return
char letter; // letter being processed
// convert message to upper case
original = original.toUpperCase();
// process each character of the message
for (int index = 0; index < original.length(); index++)
{
// get the letter and determine whether or not to
// add the cipher value
letter = original.charAt(index);
if (letter >='A' && letter <= 'Z')
{
// is A-Z, so add offset
// determine whether result will be out of A-Z range
if ((letter + offset) > 'Z') // need to wrap around to 'A'
letter = (char)(letter - ALPHABET_SIZE + offset);
else
if ((letter + offset) < 'A') // need to wrap around to 'Z'
letter = (char)(letter + ALPHABET_SIZE + offset);
else
letter = (char)(letter + offset);
}
// change space to '~'
if (letter == ' ')
{
letter = (char)(126);
}
// change colon to '-'
else if (letter == ':')
{
letter = (char)(45);
}
// change semi colon to '^'
else if (letter == ':')
{
letter = (char)(94);
}
// change period to '&'
else if (letter == '.')
{
letter = (char)(38);
}
// change hyphen to '$'
else if (letter == '-')
{
letter = (char)(36);
}
// change exclamation point to '('
else if (letter == '!')
{
letter = (char)(40);
}
// change question marke to ')'
else if (letter == '?')
{
letter = (char)(41);
}
// change all other keyboard characters to '%'
else if (letter == '1')
{
letter = (char)(37);
}
// build encoded message string
encoded = encoded + letter;
}
return encoded;
}
public static String decode(String original, int offset)
{
// declare constants
final int ALPHABET_SIZE = 26; // used to wrap around A-Z
String decoded = ""; // base for string to return
char letter; // letter being processed
// make original message upper case
original = original.toUpperCase();
// process each letter of message
for (int index = 0; index < original.length(); index++)
{
// get letter and determine whether to subtract cipher value
letter = original.charAt(index);
if (letter >= 'A' && letter <= 'Z')
{
// is A-Z, so subtract cipher value
// determine whether result will be out of A-Z range
if ((letter - offset) < 'A') // wrap around to 'Z'
letter = (char)(letter + ALPHABET_SIZE - offset);
else
if ((letter - offset) > 'Z') // wrap around to 'A'
letter = (char)(letter - ALPHABET_SIZE - offset);
else
letter = (char) (letter - offset);
}
// build decoded message
decoded = decoded + letter;
}
return decoded;
}
public static void main(String[] args)
{
//Scan for Method
Scanner scan = new Scanner(System.in);
System.out.println("Please input 1 to encrypt and 2 to decrypt");
int action = scan.nextInt();
//If encryption is picked
if (action == 1)
{
System.out.println("Please input the text to be Encrypted");
String original = scan.next();
System.out.println("Please input shift value");
int offset = scan.nextInt();
System.out.println(encode(original, offset));
}
else if (action == 2)
{
System.out.println("Please input the text to be Decrypted");
String original = scan.next();
System.out.println("Please input shift value");
int offset = scan.nextInt();
System.out.println(decode(original, offset));
}
}
}