编程思路:
1、由用户输入要加密的字符串(仅允许小写字母不包含空格)
2、把字符串中的每一个字母按字母表顺序向后移动两位输出新字符串
注释,暂不考虑“溢出”问题(即用户输入了x以后的字母),代码如下:
<script>
//用split()函数把用户输入的字符串分割并赋值给数组str[]
var str=prompt("请输入要加密的字符串:").split("");
//定义包含26个小写字母的alphabet数组
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"];
//定义alphabet数组的索引变量i
for(var i=0; i<=25;i++)
//定义str数组的索引变量n
for(var n=0; n<=str.length; n++)
//判断str数组中元素在alphabet中的位置,并往后移2位
if(str[n]==alphabet[i])
str[n]=alphabet[i+2]
//用jon()函数把str数组转换成字符串并赋值给newStr
var newStr=[str[n]].join("");
//输出结果
alert("加密后的新字符串是:"+newStr);
</script>
``
已经很晚了,我没时间翻书再检查错误了,明天一早我还要去工地搬砖,恳请列位大侠直接帮忙修改成能正确运行的代码,我明天抽空拜研之
var str=prompt("请输入要加密的字符串:").split("");var newStr=[];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"];for(var n=0; n<=str.length; n++)for(var i=0; i<=25;i++)if(str[n]==alphabet[i])newStr[n]=alphabet[i+2];alert("加密后的新字符串是:"+newStr.join(""))
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。