我有一个据说是
的程序我的代码现在只给我一个加密并停在2.它也只适用于第一个字母。我的班级还没有学过数组,但如果我们想要,我们可以试试。
import java.util.Scanner;
public class Encrypt{
Scanner keyboard = new Scanner(System.in);
String message = new String();
String g = new String();
char y;
public void input(){
System.out.printf("Welcome to Encrypt.java. Please enter a word,phrase, or sentence. \n");
System.out.println();
System.out.print("-> ");
message = keyboard.nextLine();
}
public void code(){
int x = message.length()-1;
boolean enter = true;
for(int i = 0; i <= x; i++){
int j = message.charAt(i);
if((j >= 32 && j <=64) ||
(j >= 91 && j <=96) ||
(j >= 123 && j <= 127)){
}
else if((j >= 65 && j <= 90)){
j = j + 2;
if(j>90){
j = (j-90)+64;
}
}
else if(j>=97 && j <= 122){
j = j + 2;
if(j>122){
j = (j-122) + 96;
}
}
if(enter == true){
System.out.println();
System.out.print(" ");
enter = false;
}
y = (char)(j);
g = g + y;
message = g;
x = message.length()-1;
}
System.out.print(g);
System.out.println();
}
public void print(){
for(int i = 1; i <= 13; i ++){
System.out.println("Encryption " + i + ":");
this.code();
}
}
public static void main(String [] args){
Encrypt e = new Encrypt();
e.input();
e.print();
}
}
答案 0 :(得分:2)
两件事:
public void code() {
int x = message.length() - 1;
boolean enter = true;
g = "";
for (int i = 0; i <= x; i++) {
int j = message.charAt(i);
if ((j >= 32 && j <= 64) || (j >= 91 && j <= 96)
|| (j >= 123 && j <= 127)) {
}
else if ((j >= 65 && j <= 90)) {
j = j + 2;
if (j > 90) {
j = (j - 90) + 64;
}
} else if (j >= 97 && j <= 122) {
j = j + 2;
if (j > 122) {
j = (j - 122) + 96;
}
}
if(enter == true){
System.out.println();
System.out.print(" ");
enter = false;
}
y = (char) (j);
g = g + y;
}
message = g;
}
输出:
Welcome to Encrypt.java. Please enter a word,phrase, or sentence.
-> abba
Encrypt.code() message >> abba
Encrypt.code() message >> cddc
Encrypt.code() message >> effe
Encrypt.code() message >> ghhg
Encrypt.code() message >> ijji
Encrypt.code() message >> kllk
Encrypt.code() message >> mnnm
Encrypt.code() message >> oppo
Encrypt.code() message >> qrrq
Encrypt.code() message >> stts
Encrypt.code() message >> uvvu
Encrypt.code() message >> wxxw
Encrypt.code() message >> yzzy
答案 1 :(得分:0)
这是你的问题
y = (char) (j);
g = g + y;
message = g;
在第一次运行中,g
只有一个字符,而您正在制作message = g;
。这使message
只有一个字符g
。我跑了它删除了message = g
,它运行正常。我不知道这是否是你想要的输出,但至少它已经过了Encryption 1
注意:您应该学习如何使用调试器。这就是我发现问题的方法。