Java新手尝试将字符串传递给实例java

标签 java string char instance

目前,我正在尝试在我正在创建的项目中执行凯撒密码。但是,当我尝试将字符串传递到处理它的实例时,它似乎根本不处理它。 (现在我忽略空格和标点符号)。

import javax.swing.*;
import java.text.*;
import java.util.*;
import java.lang.*;

public class Cipher {

private String phrase; // phrase that will be encrypted 
private int shift; //number that shifts the letters


///////////////
//Constructor//
//////////////

public Cipher( int new_shift)
{

    shift = new_shift;



}//end of cipher constructor


////////////
//Accessor//
////////////

public int askShift() {


return shift;
}//end of askShift accessor

////////////
//mutators//
////////////

public void changeShift (int newShift) {

shift = newShift;

}//end of changeShift mutator

/////////////
//instances//
/////////////

public String encryptIt(String message) {

char[] charArray = message.toCharArray(); //converts to a character array
//loop that performs the encryption
for (int count = 0; count < charArray.length; count++) {
int shiftNum = 2;
charArray[count] = (char)(((charArray[count] - 'a') + shiftNum) % 26 + 'a');

} // end of for loop    

 message = new String(charArray); //converts the array to a string



return message;
}//end of encrypt instance 


//////////
///Main///
//////////
public static void main(String[] args) {

Cipher cipher = new Cipher(1); //cipher with a shift of one letter
String phrase = JOptionPane.showInputDialog(null, "Enter phrase to be messed with ");
cipher.encryptIt(phrase);
JOptionPane.showMessageDialog(null, phrase);




}//end of main function



} //end of cipher class 

最佳答案

新的加密字符串是一个返回值。您传递给该方法的字符串保持不变。尝试例如

String encryption = cipher.encryptIt(phrase); 
JOptionPane.showMessageDialog(null, encryption ); 

关于Java新手尝试将字符串传递给实例java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13042503/

相关文章:

连续将一个 char 数组合并到另一个 char 数组中

string - 从路径中提取目录

c++ - 硬币翻转计数游戏 C++ 数据结构!

java - directExecutor的优点

java - 有没有办法将 MDB javadoc 作为其描述注入(inject)?

python - 在 Pandas DataFrame 中查找和计算字符串值

Python - 如何仅使用变量使用 dict() 函数?

c - C 中的字符串内存分配

java - Playframework 在动态 route 给出错误

java - 项目云计算仿真环境