我正在尝试创建一个循环,所以这个程序将继续,直到用户要求退出但我知道我需要使用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();
}
}
}
答案 0 :(得分:0)
您可以做的是以下内容
do{
//code you want to run
}while(!stop);
在键事件(例如,用户按下“e”键)或可能是JButton时,stop被更改为true。