Bir Caesar Shift şifreleme ve şifre çözme programı oluşturmaya çalışıyorum. Kullanıcıyı (q) bırakmayı isteyinceye kadar kullanıcı girdisini kabul edebilmek için ihtiyacım var, ama sonuçta her şeyin aşırı bir tekrarını elde ediyorum. İşte Döngü tekrar tekrar tekrar oluştururken, java
benim Shift Sınıfimport java.util.Scanner;
public class CaesarShift
{
//initialize private string for the alphabet
private final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
//public encryption code
public String encryptionMethod(String normText, int caesarShift)
{
normText = normText.toLowerCase();
String cipherText = "";
for (int a = 0; a < normText.length(); a++)
{
int charP = ALPHABET.indexOf(normText.charAt(a));
int shiftValue = (caesarShift + charP) % 26;
char replaceValue = this.ALPHABET.charAt(shiftValue);
cipherText += replaceValue;
}
return cipherText;
}
public String decryptionMethod(String cipherText,int caesarShift)
{
cipherText = cipherText.toLowerCase();
String normText = "";
for (int a = 0; a < cipherText.length(); a++)
{
int charP = this.ALPHABET.indexOf(cipherText.charAt(a));
int keyValue = (charP - caesarShift) % 26;
if(keyValue < 0)
{
keyValue = this.ALPHABET.length() + keyValue;
}
char replaceValue = this.ALPHABET.charAt(keyValue);
normText += replaceValue;
}
return normText;
}
} Burada
ben while döngüsüimport java.util.Scanner;
public class CaesarShiftTester
{
public static void main(String args[])
{
//import of the scanner method to ask the user for the input they would like
Scanner in = new Scanner(System.in);
System.out.println("What is the text you would like to do something with?(q to quit)");
String normText = in.nextLine();
System.out.println("What is the Caesar Shift Value?");
int caesarShift = in.nextInt();
//new declaration of the CaesarShift class to report back to easily
CaesarShift shift = new CaesarShift();
//declare the while method loop
while(normText != "q")
{
//decalre the need properties for the encryption
String cipherText = shift.encryptionMethod(normText, caesarShift);
System.out.println("Your normal text is: " + normText);
System.out.println("Your text after encryption is: " + cipherText);
String cnormText = shift.decryptionMethod(cipherText, caesarShift);
System.out.println("Your encrypted text is: " + cipherText);
System.out.println("Your decrypte text is: " + cnormText);
}
}
} burada
@Reimeus İster inanın ister inanmayın, burada dize karşılaştırması tek sorun değildir. Ayrıca döngü içinde hiç kullanıcı girişi istemez. Bu daha büyük sorun olan IMHO. –
Hata ayıklayıcı sizi daha hızlı alacak. NormText'i sadece bir kez atadığınıza benziyor. Ayrıca nesneleri (ve dolayısıyla dizeleri) '! = Ile karşılaştırmak, düşündüğün şeyi yapmaz. –