此程序具体想实现这些功能:在明文框输入明文按回车键以后就可以在密文框显示相应的密文:同样地,在密文框输入收到的密文并回车就可以在明文框显示明文内容,明密文转换规则就是字母之间错开一位,比如说明文的A对应密文的B。但是写出来以后报错了,报错信息:“需要为 class、interface 或 enum”。小弟刚学java实在菜鸟弄不懂,谢谢各位了。
import java.awt.*;
import java.awt.event.*;
class YourWindow extends Frame implements ActionListener
{
TextField text1,text2,text3,text4;
Button b1;
YourWindow(String s)
{
super(s);
setLayout(new FlowLayout());
text1=new TextField(20);
text2=new TextField(20);
text3=new TextField("输入明文",20);
text4=new TextField("输入密文",20);
text3.setEditable(false);
text4.setEditable(false);
b1=new Button("退出");
add(text1);add(text2);add(text3);add(text4);
add(b1);
b1.addActionListener(this);
text1.addActionListener(this);
text2.addActionListener(this);
setBounds(200,200,550,350);
setVisible(true);
validate();
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
System.exit(0);
}
else if(e.getSource()==text1)
{
String s=text1.getText();
convertString(s);
char n[]=s.toCharArray();
int i;
int m=0;
char k[];
for(i=0;i<n.lenght;i++)
{
m = (int) n[i];
if((m+1)<=90&&(m+1)>=65)
{
k[i] = (char)(m+1)
}
else if((m+1)>90)
{
k[i] = (char)(m+1-26)
}
}
return String.valueOf(k);
text2.setText(String.valueOf(k));
}
else if(e.getSource()==text2)
{
String ss=text2.getText();
convertString(ss);
char nn[]=ss.toCharArray();
int ii;
int mm=0;
char kk[];
for(ii=0;ii<nn.lenght;ii++)
{
mm = (int) nn[i];
if((mm-1)>0)
{
kk[ii] = (char)(mm-1)
}
else if((mm-1)<=0)
{
kk[ii] = (char)(mm-1+26)
}
return String.valueOf(kk);
text1.setText(String.valueOf(kk));
}
}
}
public static String convertString(String src)
{
char[] array = src.toCharArray();
int temp = 0;
for (int i = 0; i < array.length; i++)
{
temp = (int) array[i];
if (temp <= 90 && temp >= 65)
{ // array[i]为大写字母
array[i] = (char) (temp + 32);
}
else if{}
}
return String.valueOf(array);
}
public class Acc
{
public static void main(String args[])
{
YourWindow a=new YourWindow("解密");
}
}
------解决方案--------------------
我给你代码改了一下
- Java code
import java.awt.*;import java.awt.event.*;class YourWindow extends Frame implements ActionListener { TextField text1, text2, text3, text4; Button b1; YourWindow(String s) { super(s); setLayout(new FlowLayout()); text1 = new TextField(20); text2 = new TextField(20); text3 = new TextField("输入明文", 20); text4 = new TextField("输入密文", 20); text3.setEditable(false); text4.setEditable(false); b1 = new Button("退出"); add(text1); add(text2); add(text3); add(text4); add(b1); b1.addActionListener(this); text1.addActionListener(this); text2.addActionListener(this); setBounds(200, 200, 550, 350); setVisible(true); validate(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == b1) { System.exit(0); } else if (e.getSource() == text1) { String s = text1.getText(); convertString(s); char n[] = s.toCharArray(); int i; int m = 0; char k[] = new char[n.length]; for (i = 0; i < n.length; i++) { m = (int) n[i]; if ((m + 1) <= 90 && (m + 1) >= 65) { k[i] = (char) (m + 1); } else if ((m + 1) > 90) { k[i] = (char) (m + 1 - 26); } } text2.setText(String.valueOf(k)); } else if (e.getSource() == text2) { String ss = text2.getText(); convertString(ss); char nn[] = ss.toCharArray(); int ii; int mm = 0; char kk[] = new char[nn.length]; for (ii = 0; ii < nn.length; ii++) { mm = (int) nn[ii]; if ((mm - 1) > 0) { kk[ii] = (char) (mm - 1); } else if ((mm - 1) <= 0) { kk[ii] = (char) (mm - 1 + 26); } text1.setText(String.valueOf(kk)); } } } public static String convertString(String src) { char[] array = src.toCharArray(); int temp = 0; for (int i = 0; i < array.length; i++) { temp = (int) array[i]; if (temp <= 90 && temp >= 65) { // array[i]为大写字母 array[i] = (char) (temp + 32); } // else if{} } return String.valueOf(array); }}public class Acc { public static void main(String args[]) { YourWindow a = new YourWindow("解密"); }}