0
我想为学校做一个凯撒密码,但由于某种原因我的代码不会工作。它在解码时也不会出错。问题出在scan.nextLine()之后;在它要求输入一条编码信息之后,该代码会跳过整个序列并停止。在我能输入任何东西之前。Java代码除了next .nextLine();扫描仪
import java.util.Scanner;
import java.lang.Math;
import java.io.*;
public class CodeAssignment4 {
public static void main(String[] args) throws IOException
{
Scanner scan= new Scanner (System.in);
System.out.println("Please enter the key to your cipher");
int key = scan.nextInt();
if (key >= 26) {
System.out.println("Key out of range");
return;
}
System.out.println("Please enter your coded message");
String codemessage = scan.nextLine();
int length = codemessage.length();
char[]codemessagearray = codemessage.toCharArray();
char value;
char newkey = (char)key;
String decodedmessage = " ";
int i;
for (i=0; i < length; i++) {
value = codemessagearray[i];
if ((value < 'a') || (value > 'z')) {
System.out.println("Character out of range");
break;
}
value -= 'a'; //send range to zero
value -= newkey; //decrament key
value += 26; //account for negative
value = (char)((int)value%26);
decodedmessage += value + 'a';
}
if (i == length) {
System.out.println(decodedmessage);
}
}
}
我觉得我的一个休息是造成问题。
你试图通过你的程序在调试器步进? – Robert