编程思路:
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>
已经很晚了,我没时间翻书再检查错误了,明天一早我还要去工地搬砖,恳请列位大侠直接帮忙修改成能正确运行的代码,我明天抽空拜研之