java - 输出问题,用户输入通过 void 函数(Caesar's Cipher,Java,swing)
问题描述
最近几周我正在学习java(和菜鸟)。我正在尝试用我自己的方式编写一个类似于“Caesars Cipher”的程序(当然不是最有效的算法),在“当前”字母之后将字母更改 2 个位置。我试图在摇摆的帮助下解决这个问题。
当我在没有 GUI 的情况下测试它(eclipse)时,一切正常,例如:
输入:“我在这里。” -输出:“k co jgtg”。
但由于我是通过 GUI 运行它,所以在代码末尾的 actionPerformed中出现错误: “outputField.setText ( encryptInput (inputField.getText()));”
错误消息:JTextComponent 类型中的方法 setText() 不适用于参数 (void)。
我相信问题来自我的方法“encryptedInput”及其“返回”值。我知道 void 什么都不返回,但我觉得 system.out.println() 不知何故它不“适合”我的 outputField/ActionListener 部分。
我检查了很多主题,但不幸的是没有任何帮助。或者至少,我有限的编程经验并不能帮助我从这些主题中识别出可能的解决方案。
我希望有人能帮助我。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/* Caesar's Cipher function - 2 positions (indexes) after. */
public class CaesarCipher extends JFrame implements ActionListener{
JLabel inputLabel, outputLabel;
JButton calculatebtn;
JTextField inputField, outputField;
GridLayout grid;
String encryptedString;
public CaesarCipher()
{
setTitle("Caesar Cipher Converter");
setSize(350,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inputLabel = new JLabel("Put your Text here : ");
inputField = new JTextField(20);
outputLabel = new JLabel("Result : ");
outputField = new JTextField(20);
calculatebtn = new JButton("Convert");
grid = new GridLayout(5,1);
JPanel pane = new JPanel();
pane.setLayout(grid);
setContentPane(pane);
pane.add(inputLabel);
pane.add(inputField);
pane.add(calculatebtn);
pane.add(outputLabel);
pane.add(outputField);
calculatebtn.addActionListener(this);
}
public static void main(String[] args) {
CaesarCipher caeCipher = new CaesarCipher();
caeCipher.setVisible(true);
}
public void encryptInput(String inputString)
{
/* Array to calculate the new values (a-z)*/
char[] caesArray = {'a', 'b', 'c','d','e','f','g','h',
'i','j','k','l','m','n','o','p','q','r','s','t',
'u','v','w','x','y','z', ' ', '.'};
/* Array for users input */
char[] inputArray = new char[inputString.length()];
char resultChar= ' ';
/* saves the String characters into the inputArray */
for(int i = 0 ; i < inputArray.length; i++)
{
inputArray[i] = inputString.charAt(i);
}
for(int i = 0 ; i < inputArray.length ; i++)
for(int j = 0 ; j < caesArray.length ; j++)
{
if (caesArray[j] == inputArray[i])
{
/* My special characters */
switch(inputArray[i])
{
case ' ': resultChar = ' ';
break;
case '.': resultChar = '.';
break;
}
if(inputArray[i] == caesArray[caesArray.length - 1])
{
inputArray[i] = caesArray[1];
}
else if(inputArray[i] == caesArray[caesArray.length - 2])
{
inputArray[i] = caesArray[0];
}
else
{
resultChar = caesArray[j+2];
}
System.out.print(resultChar);
}
}
}
@Override
public void actionPerformed(ActionEvent arg0)
{
if(arg0.getSource() == calculatebtn)
{
outputField.setText(encryptInput(inputField.getText()));
}
}
}
解决方案
这是因为:
- 您的方法
encryptInput
不返回任何内容(返回类型为void
) - and
setText
方法接受 aString
作为参数
最后encryptInput
,您的方法只是resultChar
在每次计算时打印到标准输出。您需要更改它以返回包含您正在显示String
的所有 s 的串联的a 。resultChar
有很多方法可以做到这一点,这里有一个简单的例子,只使用你似乎知道的工具:
public String encryptInput(String inputString)
{
/* Array to calculate the new values (a-z)*/
char[] caesArray = {'a', 'b', 'c','d','e','f','g','h',
'i','j','k','l','m','n','o','p','q','r','s','t',
'u','v','w','x','y','z', ' ', '.'};
/* Array for users input */
char[] inputArray = new char[inputString.length()];
char resultChar= ' ';
char[] resultArray = new char[inputString.length()];
/* saves the String characters into the inputArray */
for(int i = 0 ; i < inputArray.length; i++)
{
inputArray[i] = inputString.charAt(i);
}
for(int i = 0 ; i < inputArray.length ; i++)
for(int j = 0 ; j < caesArray.length ; j++)
{
if (caesArray[j] == inputArray[i])
{
/* My special characters */
switch(inputArray[i])
{
case ' ': resultChar = ' ';
break;
case '.': resultChar = '.';
break;
}
if(inputArray[i] == caesArray[caesArray.length - 1])
{
inputArray[i] = caesArray[1];
}
else if(inputArray[i] == caesArray[caesArray.length - 2])
{
inputArray[i] = caesArray[0];
}
else
{
resultChar = caesArray[j+2];
}
System.out.print(resultChar);
resultArray[i] = resultChar;
}
}
return new String(resultArray);
}
PS:更一般地说,这段代码肯定可以改进,但这超出了该问题的范围:)
推荐阅读
- python - Pandas 加入字符串数据类型
- javascript - 尝试使用jQuery增加复选框的可点击区域
- asp.net - 如何在powershell中执行具有[[FromForm]]参数的API PUT方法
- typescript - 打字稿图书馆组织
- scala - 无法用于理解来解决未来
- intellij-idea - Intellij 中的控制台在应用程序崩溃时没有显示任何问题
- php - 如何计算登录系统的总时间?
- architecture - 保持准确股价的最佳实践
- php - .htaccess 错误,https 传输上的循环重定向
- docker - Docker 容器超过了 npm install 的最大调用堆栈大小