我正在尝试创建一个循环,所以这个程序将继续,直到用户要求退出,但我知道我需要使用do-while循环,但由于某些原因,它不能工作,我如何能够修复这个问题。我希望用户按下像E(退出)和程序将停止。
import java.util.Scanner;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKey;
/**
*/
public class ENCRY
{
public static void main(String[] args)
{
try{
do{
Scanner input = new Scanner(System.in);
System.out.println("Please select an Option");
System.out.println("Encrypt the Sentence");
int File =input.nextInt();
System.out.println("--------------------");
System.out.print("Enter the sentence:");
String s = input.next();
KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = keygenerator.generateKey();
Cipher desCipher;
desCipher = Cipher.getInstance("DES");
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
byte[] text = s.getBytes();
System.out.println("" + new String(text));
byte[] textEncrypted = desCipher.doFinal(text);
System.out.println("sentence Encryted : " + textEncrypted);
desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
byte[] textDecrypted = desCipher.doFinal(textEncrypted);
System.out.println("sentence Decryted : " + new String(textDecrypted));
while()
}catch(NoSuchAlgorithmException S)
{
S.printStackTrace();
}catch(NoSuchPaddingException S)
{
S.printStackTrace();
}catch(InvalidKeyException S)
{
S.printStackTrace();
}catch(IllegalBlockSizeException S)
{
S.printStackTrace();
}catch(BadPaddingException S)
{
S.printStackTrace();
}
}
}发布于 2015-02-11 03:06:18
您可以做的事情如下所示
do{
//code you want to run
}while(!stop);在关键事件时,停止改为真(如。用户按下"e“键)或可能按JButton。
https://stackoverflow.com/questions/28445792
复制相似问题