java中如何清除一个全局变量的字符串原来的值
importjava.awt.*;importjava.awt.event.*;publicclassRegisterMyeclipseextendsFrameimple...
import java.awt.*;
import java.awt.event.*;
public class RegisterMyeclipse extends Frame implements ActionListener{
//定义全局变量
String message = new String();//创建一个空的串变量,String不能修改,StringBuffer可以修改
StringBuffer messageTest = new StringBuffer();
String key = new String("XINGCHUIPINGYEKUOYUEYONGDAJIANGLIU");
StringBuffer secretMessage = new StringBuffer();
private TextField input = new TextField();
private TextArea output = new TextArea();
private Button Test = new Button("Test");
private Button Enter = new Button("Enter");
private Button Encryption = new Button("Encryption");
private class WindowCloser extends WindowAdapter{
public void windowClosing(WindowEvent we) {
System.exit(0); }
}
public RegisterMyeclipse(){
super("My Vigenre System");
setup();
Encryption.addActionListener(this);
Test.addActionListener(this);
Enter.addActionListener(this);
addWindowListener(new WindowCloser());
pack(); setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == Enter) {
output.append("This is the plaintext:"+input.getText()+"\n");
message=input.getText();
}
else if (e.getSource() == Encryption) {
output.append("This is ciphertext:");
for(int i = 0;i < message.length();i++){//i要定义为局部变量,如果是全局变量无法指向字符串的每个字符
char m;
char k;
char s;
m = message.charAt(i);
k = key.charAt(i);
s =(char) ((m-65+k-65)%26+65);
secretMessage.append(s);
}
output.append(secretMessage.toString()+"\n");//append后要跟string
}
else if (e.getSource() == Test) {
output.append("Is the encryption right or wrong? ");
for(int i = 0;i < secretMessage.length();i++){//i要定义为局部变量,如果是全局变量无法指向字符串的每个字符
char a;
char k;
char c;
a = secretMessage.charAt(i);
k = key.charAt(i);
c =(char) ((91-a+k-65)%26+65);
messageTest.append(c);
}
if (message.equals(messageTest.toString())){//判断两字符串是否相等
output.append("Right\n");
}
else{
output.append("Wrong\n");
}
}
}
private void setup(){
Panel top = new Panel();
top.setLayout(new BorderLayout());
top.add("North",input);
top.add("East",Enter);
top.add("Center",Encryption);
top.add("South",Test);
setLayout(new BorderLayout());
add("North",top); add("Center",output);
}
public static void main(String args[])
{ RegisterMyeclipse tat = new RegisterMyeclipse(); }
} 展开
import java.awt.event.*;
public class RegisterMyeclipse extends Frame implements ActionListener{
//定义全局变量
String message = new String();//创建一个空的串变量,String不能修改,StringBuffer可以修改
StringBuffer messageTest = new StringBuffer();
String key = new String("XINGCHUIPINGYEKUOYUEYONGDAJIANGLIU");
StringBuffer secretMessage = new StringBuffer();
private TextField input = new TextField();
private TextArea output = new TextArea();
private Button Test = new Button("Test");
private Button Enter = new Button("Enter");
private Button Encryption = new Button("Encryption");
private class WindowCloser extends WindowAdapter{
public void windowClosing(WindowEvent we) {
System.exit(0); }
}
public RegisterMyeclipse(){
super("My Vigenre System");
setup();
Encryption.addActionListener(this);
Test.addActionListener(this);
Enter.addActionListener(this);
addWindowListener(new WindowCloser());
pack(); setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == Enter) {
output.append("This is the plaintext:"+input.getText()+"\n");
message=input.getText();
}
else if (e.getSource() == Encryption) {
output.append("This is ciphertext:");
for(int i = 0;i < message.length();i++){//i要定义为局部变量,如果是全局变量无法指向字符串的每个字符
char m;
char k;
char s;
m = message.charAt(i);
k = key.charAt(i);
s =(char) ((m-65+k-65)%26+65);
secretMessage.append(s);
}
output.append(secretMessage.toString()+"\n");//append后要跟string
}
else if (e.getSource() == Test) {
output.append("Is the encryption right or wrong? ");
for(int i = 0;i < secretMessage.length();i++){//i要定义为局部变量,如果是全局变量无法指向字符串的每个字符
char a;
char k;
char c;
a = secretMessage.charAt(i);
k = key.charAt(i);
c =(char) ((91-a+k-65)%26+65);
messageTest.append(c);
}
if (message.equals(messageTest.toString())){//判断两字符串是否相等
output.append("Right\n");
}
else{
output.append("Wrong\n");
}
}
}
private void setup(){
Panel top = new Panel();
top.setLayout(new BorderLayout());
top.add("North",input);
top.add("East",Enter);
top.add("Center",Encryption);
top.add("South",Test);
setLayout(new BorderLayout());
add("North",top); add("Center",output);
}
public static void main(String args[])
{ RegisterMyeclipse tat = new RegisterMyeclipse(); }
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询