步骤1.选择虚拟键。 (苹果)
步骤2.从虚拟键中重复数据删除。 (APLE)步骤3.剩余的字母表连接在一起,因此没有 从最后一个字母表重复。这时,总长度是 26个字符。如果' z'填补,它从' a'试。
结果:aplefghijkmnoqrstuvwxyzbcd
修正了第2步,我想在文本字段中使用.getText()
我不知道如何从第3步开始。
我该怎么办?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import com.jgoodies.forms.factories.DefaultComponentFactory;
public class monoalphabetic_Cipher extends JFrame implements ActionListener {
private JPanel p1, p2, p3; // default, staging job, job selection
private JLabel title, l1, l2, l3, l4; // title, virtual key input, de-duplicated virtual key, encryption, decryption
private JButton b1, b2, b3; // Encryption, decryption, deduplication
private JTextField t1, t2, t3, t4;
int i,j;
public monoalphabetic_Cipher() {
super("monoalphabetical cipher");
p1 = new JPanel();
p1.setForeground(new Color(0, 0, 0));
p1.setBackground(UIManager.getColor("Button.background"));
title = new JLabel("<html><h1>monoalphabetical cipher</h1><hr></html>");
title.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
title.setBounds(204, 20, 175, 57);
p1.add(title);
b2 = new JButton("encryption");
b2.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
b2.setBackground(UIManager.getColor("Button.background"));
b2.setBounds(67, 210, 119, 23);
p1.add(b2);
b3 = new JButton("decryption");
b3.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
b3.setBackground(UIManager.getColor("Button.background"));
b3.setBounds(67, 252, 119, 23);
p1.add(b3);
t3 = new JTextField(20);
t3.setBounds(260, 212, 297, 21);
p1.add(t3);
t4 = new JTextField(20);
t4.setBounds(260, 254, 297, 21);
p1.add(t4);
p1.setLayout(null);
t2 = new JTextField(10);
t2.setBounds(395, 123, 128, 21);
p1.add(t2);
b1 = new JButton("overlap remove");
b1.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
b1.setBounds(264, 122, 119, 23);
p1.add(b1);
l1 = new JLabel("virtual key");
l1.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
l1.setBounds(55, 126, 57, 15);
p1.add(l1);
t1 = new JTextField(10);
t1.setBounds(124, 123, 128, 21);
p1.add(t1);
p3 = new JPanel();
p3.setBorder(new TitledBorder(new LineBorder(Color.lightGray), " selection", TitledBorder.LEADING, TitledBorder.TOP, new Font("맑은 고딕", Font.PLAIN, 12), new Color(0, 0, 0)));
p3.setBounds(34, 176, 184, 122);
p1.add(p3);
l2 = new JLabel("\u2460");
l2.setFont(new Font("맑은 고딕", Font.PLAIN, 16));
l2.setBounds(230, 211, 28, 23);
p1.add(l2);
l3 = new JLabel("\u2461 ");
l3.setFont(new Font("맑은 고딕", Font.PLAIN, 16));
l3.setBounds(230, 252, 28, 23);
p1.add(l3);
p2 = new JPanel();
p2.setBorder(new TitledBorder(new LineBorder(Color.lightGray), "ready", TitledBorder.LEADING, TitledBorder.TOP, new Font("맑은 고딕", Font.PLAIN, 12), new Color(0, 0, 0)));
p2.setBounds(34, 97, 523, 69);
p1.add(p2);
getContentPane().add(p1);
setSize(600,355);
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
// duplication remove
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int vk_1 = t1.getText().length();
Set chk = new LinkedHashSet();
for(i=0;i<vk_1;i++) {
// Convert all lowercase letters to uppercase and lowercase letters
chk.add(t1.getText().toLowerCase().charAt(i));
}
/// Remove brackets and commas in the [result, result] format, which is the default output method of LinkedHashSet()
String result = Arrays.toString(chk.toArray()).replace("[", "").replace(",", "").replace("]", "");
/ * With the usual replaceAll ("", "") or trim ()
* If spaces are not removed, use regular expressions
* /tText(result.replaceAll("\\p{Z}", ""));
if(t1.getText().equals("")) {
t2.setText("no input key");
}
}
});
// encryption
b2.addActionListener(new ActionListener() {
final String curString = t2.getText();
final char startChar = curString.charAt(curString.length()-1);
public void actionPerformed(ActionEvent e) {
StringBuilder sb = new StringBuilder();
sb.append(curString);
char c = nextChar(startChar);
while (sb.length() < 26) {
while (hasChar(sb, c)) {
c = nextChar(c);
}
sb.append(c);
c = nextChar(c);
}
System.out.printf("Final String %28s has length of %3d", sb.toString(), sb.length());
}
private char nextChar(int idx) {
++idx;
if (idx > 'z') {
idx = 'a';
}
return (char)idx;
}
private boolean hasChar(StringBuilder sb, char c)
{
for (int i = 0; i < sb.length(); ++i) {
if (sb.charAt(i) == c) {
return true;
}
}
return false;
}
});
// Decrypt
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(t2.getText().equals("")) {
t4.setText("no input key");
}
}
});
}
public static void main(String[] args) {
new monoalphabetic_Cipher();
}
}
答案 0 :(得分:2)
假设:输入字符串经过重复数据删除,它具有适当的长度,要添加的字符为“a”到“z”(包括端点),不应重复任何字符,最终字符串应为26个字符长,以下提供了一个快速的解决方案。需要进行额外的错误检查和更好的变量命名。
public static void main(String[] args)
{
// the example string
final String curString = "aple";
// get the starting character, which is the end of the string
// in the example, will be 'e'
final char startChar = curString.charAt(curString.length() - 1);
// construct our compilation; this could also be List<Character>
// which would make somethings easier, but would have to assemble
// at the end
StringBuilder sb = new StringBuilder();
sb.append(curString);
// we want the character after the one on the end of the string to start
char c = nextChar(startChar);
// we want 26 total entries
while (sb.length() < 26) {
// we cannot append a character that already exists, so spin
// until we find one not in the compilation
while (hasChar(sb, c)) {
c = nextChar(c);
}
// we now have the one we want, so append it
sb.append(c);
// we know we need at least the next character after the one we
// just appended, so get it and loop; loop will terminate if
// we've added enough
c = nextChar(c);
}
// display the result
System.out.printf("Final String %28s has length of %3d", sb.toString(),
sb.length());
}
private static boolean hasChar(StringBuilder sb, char c)
{
for (int i = 0; i < sb.length(); ++i) {
if (sb.charAt(i) == c) {
return true;
}
}
return false;
}
private static char nextChar(int idx)
{
++idx;
if (idx > 'z') {
idx = 'a';
}
return (char)idx;
}
输出:
Final String aplefghijkmnoqrstuvwxyzbcd的长度为26