我是JS的初学者。我试图了解Caesar Cipher ROT13,但对我来说太复杂了。因此,我尝试编写自己的代码。在下面:
public class Home implements EasyPermissions.PermissionCallbacks{
...
private String[] permission_sounds = {Manifest.permission.WRITE_SETTINGS, Manifest.permission.WRITE_EXTERNAL_STORAGE};
private int RC_permissionSounds = 500;
private Activity act;
..onCreate{
act = this;
}
//I assign this onClickListener to ringtoneButton
private View.OnClickListener ringtoneMenuOpen(){
Log.d(TAG, "ringtoneMenuOpen accedemos, tenemos que entrar ahora en solicitar permisos e intentar ");
soundButtonMode = SoundButtonMode.RIGNTONE;
return new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "He pulsado ringtoneMenuOpen"); //Line 1 Logcat
if (EasyPermissions.hasPermissions(act, permission_sounds)) {
new SetSoundAsRingtone(act).execute();
} else {
EasyPermissions.requestPermissions(act, act.getString(R.string.permission_rationale), RC_permissionSounds, permission_sounds);
}
}
};
}
...
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, act);
}
@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
Log.d(TAG, "onPermissionsGranted");
buttonRingtone.performClick();
}
@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
Log.d(TAG, "onPermissionsDenied");//Line 2 Logcat
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
Log.d(TAG, "Hemos llegado aquí con permisos denegados y 'No volver a mostrar'"); //Line 3 Logcat
new AppSettingsDialog.Builder(this).build().show();
}
}
...
但是我被困住了。怎么做:
1。。从var str中获取值,然后访问var字母,将每个字母从var str值更改为字母表中的下3个字母(var str,每个元素的当前位置都会更改)。 :function encrip() {
var alphabet = ["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"];
var str = "Ni Hao";
var string = str.toUpperCase();
for (var i = 0; i < string.length; i++) {
for (var k = 0; k < alphabet.length; k++) {
if(string.charAt(i) == alphabet[k]) {
/* console.log(string.charAt(i) + ' ' + alphabet.indexOf(alphabet[k])); */
}
}
}
}
encrip();
2。。我的意思是,创建通用代码,不仅是为了将位置更改3,而且当我给定值“ 5”时,每个元素的字母顺序都将被更改5个位置。因此,当我更改其值时可以更改输出
我希望我能清楚地解释一切。预先感谢大家的帮助!
答案 0 :(得分:0)
您可以使用以下函数对英语单词进行加密,第一个参数是要加密的字符串,第二个参数是要移位的
function encryp(str,pos){
var alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var strUC=str.toUpperCase();
var enc="";
for(var i=0;i<strUC.length;i++){
if(strUC.charAt(i)!=" "){
enc+=alpha.charAt((alpha.indexOf(strUC.charAt(i))+pos)%26)
}
else{
enc+=" "
}
// in your case pos=3
}
return enc;
}
console.log(encryp("NiHao",3));
答案 1 :(得分:0)
您不需要两个for循环即可执行此操作。遍历输入string
并在alphabet
数组中找到每个字符的索引,如果找到,则向其中添加shift
以得到加密的字符。
要处理溢出,请使用模运算符在数组中循环。 我还假设您不会使用任何特殊符号进行加密。
function encrip(string, shift) {
var alphabet = ["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"];
string = string.toUpperCase();
let arr = [];
for (var i = 0; i < string.length; i++) {
let char = alphabet.indexOf(string[i]) !== -1 ? alphabet[(alphabet.indexOf(string[i]) %26) + shift] : " ";
arr.push(char);
}
let encryp = arr.join("");
console.log(encryp);
return encryp;
}
encrip("Ni Hao", 3);
答案 2 :(得分:-1)
首先,可以使用内置函数indexOf:
,而不是内部for
循环扫描整个alphabet
数组:
alphabet.indexOf('K') // returns 10
第二,您需要在单独的变量中构建加密的字符串。对于每个字母,获取该字母在字母表中的索引,将密码偏移量参数添加到该索引,并将结果从字母表中添加到新字符串中。重要的一步是,当添加到字母的索引时,您要确保结果索引在alphabet
数组的范围内。您可以使用%
(模)运算符来完成此操作,该运算符会将高值回绕到数组的开头。全部:
function encipher(input, offset) {
var alphabet = ["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"];
var str = input.toUpperCase();
var result = '';
for (var i = 0; i < str.length; i++) {
letterIndex = alphabet.indexOf(str.charAt(i));
if (letterIndex === -1) {
result += str[i]; // if the letter isn't found in the alphabet, add it to the result unchanged
continue;
}
cipheredIndex = (letterIndex + offset) % alphabet.length; // wrap index to length of alphabet
result += alphabet[cipheredIndex];
}
console.log(result);
}
encipher('Ni Hao', 5); // output: 'SN MFT'