2016-03-22 26 views
-1

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ıf

import 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

+1

@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. –

+1

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. –

cevap

3

Sen dolayısıyla sonsuza dönüyor, sizin while döngü içindeki herhangi bir kullanıcı girişi istemi asla: bu kadar

değişim. döngü içinde kullanıcı girişi kontrol ekleyin: @Stultuske onun cevapta belirtildiği üzere

while (!normText.equals("q")) { 
    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); 

    System.out.println("What is the text you would like to do something with?(q to quit)"); 
    normText = in.nextLine(); 
} 

, ayrıca String.equals() kullanarak iç, != operatörü kullanarak bir String değerini karşılaştırmak için çalışıyorlardı. Ancak, while döngüsündeki kullanıcı girdisini kontrol etmemek daha büyük bir sorundur.

+0

bir String değerini kullanarak! = – Stultuske

+0

Başar, yardım için teşekkürler. –

3
while(normText != "q") 

örneğini çalıştı benim tester yöntemidir olduğunu senin problemin mi. == ve != operatörleri, değerleri değil referansları karşılaştırmak için kullanılır.

while(!q.equals(normText)) 
İlgili konular