Java 凯撒密码程序,如何只使用字符a-z?
凯撒密码分别应用于字符串中的每个字母。字母表中的每个字母必须向前移动n步。如果一个字母从字母表的末尾('z')移走,那么它会一直移回字母表的开头('a')`Java 凯撒密码程序,如何只使用字符a-z?,java,string,caesar-cipher,Java,String,Caesar Cipher,凯撒密码分别应用于字符串中的每个字母。字母表中的每个字母必须向前移动n步。如果一个字母从字母表的末尾('z')移走,那么它会一直移回字母表的开头('a')` import java.util.*; 课堂问题7{ 公共静态void main(字符串[]args){ 字符串str=“”; //允许程序使用键盘进行用户输入 扫描仪kb=新扫描仪(System.in); 扫描仪s=新的扫描仪(System.in); int n=0; System.out.println(“将字符串中的字母增加n”);
import java.util.*;
课堂问题7{
公共静态void main(字符串[]args){
字符串str=“”;
//允许程序使用键盘进行用户输入
扫描仪kb=新扫描仪(System.in);
扫描仪s=新的扫描仪(System.in);
int n=0;
System.out.println(“将字符串中的字母增加n”);
while(true){
System.out.println(“请输入您的字符串”);
str=kb.nextLine();
System.out.println(“请输入您的n值”);
n=s.nextInt();
String incrementedword=新字符串();
对于(int i=0;i,这里有两个问题:
- 在字母表末尾增加一些字符后,您将得到非字母数字字符(例如,z+1将成为大括号{),请参见)。尝试使用余数运算符(%)。例如,117%100将是17,13%3将是1。这样(x+1)%10永远不会超过10,将从0开始计数,然后随着x的增加再次向上计数
- 如果您只想加密a..z和a..z范围内的字符,请排除其他字符。简单的比较有效:如果(x>='a'&&x您在这里遇到两个问题:
- 在字母表末尾增加一些字符后,您将得到非字母数字字符(例如,z+1将成为大括号{),请参见)。尝试使用余数运算符(%)。例如,117%100将是17,13%3将是1。这样(x+1)%10永远不会超过10,将从0开始计数,然后随着x的增加再次向上计数
- 如果您只想加密a..z和a..z范围内的字符,请排除其他字符。简单比较有效:如果(x>='a'&&x,则需要控制环绕,以防字母不在范围内。环绕是针对移动到字母z以外的字母进行的。下面的代码将仅移动字符串中的字母
import java.util.*;
class question7{
public static void main (String[] args ){
String str = "";
//allowing program to take user input using the keyboard
Scanner kb = new Scanner(System.in);
Scanner s = new Scanner(System.in);
int n = 0;
int a = 'a';
int z = 'z';
System.out.println("increasing the letters in string by n");
while (true){
System.out.println("Please enter your string");
str = kb.nextLine().toLowerCase();
System.out.println("Please enter your n value");
n = s.nextInt();
int newCharValue;
int currentChar;
String incrementedword=new String();
for (int i=0;i<str.length();i++){
currentChar = str.charAt(i);
if(!Character.isLetter(currentChar)){
incrementedword+=currentChar;
continue;
}
newCharValue = currentChar + n;
if(newCharValue <= z)
incrementedword+=(char)newCharValue;
else
incrementedword+=(char)(a + (newCharValue - a + 1)%26);
}
System.out.println ("your word is "+incrementedword);
}
}
}
import java.util.*;
课堂问题7{
公共静态void main(字符串[]args){
字符串str=“”;
//允许程序使用键盘进行用户输入
扫描仪kb=新扫描仪(System.in);
扫描仪s=新的扫描仪(System.in);
int n=0;
int a='a';
int z='z';
System.out.println(“将字符串中的字母增加n”);
while(true){
System.out.println(“请输入您的字符串”);
str=kb.nextLine().toLowerCase();
System.out.println(“请输入您的n值”);
n=s.nextInt();
int-newCharValue;
int currentChar;
String incrementedword=新字符串();
对于(int i=0;i,如果字母不在范围内,则需要控制环绕。环绕是针对移动到字母z以外的字母进行的。下面的代码将仅移动字符串中的字母
import java.util.*;
class question7{
public static void main (String[] args ){
String str = "";
//allowing program to take user input using the keyboard
Scanner kb = new Scanner(System.in);
Scanner s = new Scanner(System.in);
int n = 0;
int a = 'a';
int z = 'z';
System.out.println("increasing the letters in string by n");
while (true){
System.out.println("Please enter your string");
str = kb.nextLine().toLowerCase();
System.out.println("Please enter your n value");
n = s.nextInt();
int newCharValue;
int currentChar;
String incrementedword=new String();
for (int i=0;i<str.length();i++){
currentChar = str.charAt(i);
if(!Character.isLetter(currentChar)){
incrementedword+=currentChar;
continue;
}
newCharValue = currentChar + n;
if(newCharValue <= z)
incrementedword+=(char)newCharValue;
else
incrementedword+=(char)(a + (newCharValue - a + 1)%26);
}
System.out.println ("your word is "+incrementedword);
}
}
}
import java.util.*;
课堂问题7{
公共静态void main(字符串[]args){
字符串str=“”;
//允许程序使用键盘进行用户输入
扫描仪kb=新扫描仪(System.in);
扫描仪s=新的扫描仪(System.in);
int n=0;
int a='a';
int z='z';
System.out.println(“将字符串中的字母增加n”);
while(true){
System.out.println(“请输入您的字符串”);
str=kb.nextLine().toLowerCase();
System.out.println(“请输入您的n值”);
n=s.nextInt();
int-newCharValue;
int currentChar;
String incrementedword=新字符串();
对于(inti=0;i您可能可以将字母转换为ascii,然后以这种方式递增它们
我的意思是,您必须设置约束,“a”为97,“z”为122。这将确保它保持在小写字符内,并且您可以告诉它循环回“a”。您可能可以将字母转换为ascii,然后以这种方式递增它们
我的意思是,您必须设置约束,“a”为97,“z”为122。这将确保它保持在小写字符内,并且您可以告诉它循环回“a”。在循环中放置一个条件,以检查当前字符的大小写是否从字母“a”到“z”:
用于检查当前字符是否为小写字母(a到z)的代码段:
如果(currentChar>='a'&¤tChar='a'&¤tChar在循环中放置一个条件,该条件将检查当前字符是否从字母“a”到小写字母“z”:
用于检查当前字符是否为小写字母(a到z)的代码段:
如果(currentChar>='a'&¤tChar='a'&¤tChar您还可以“递增”空格字符。将其排除!此外,您的代码对'z'无效。我希望'z'为'a',但'z'为'{,我有点迷茫如果你在“z”中添加1,Java不会自动换回“a”,也不会自动排除空格和其他非字母字符。你必须告诉它做这些事情。你需要一些if
s:if(Character.isleter(str.charAt(i)){..}
你也可以“递增”空格字符。排除它!另外,您的代码对“z”无效。我希望“z”为“a”,但“z”为“a”{',我有点迷茫如果你在'z'中添加1,Java不会自动换回'a',也不会自动排除空格和其他非字母字符。你必须告诉它做这些事情。你需要一些if
s:if(Character.isleter(str.charAt(i)){..}
值得一提的是为什么会出现这些非字母数字字符,例如通过链接ASCII/UNICODE表:>按建议扩展。值得一提
if (currentChar >= 'a' && currentChar <= 'z') {
//Process the shifting of the characters here
}
if (currentChar >= 'a' && currentChar <= 'z') {
currentChar = (char) ((currentChar - 'a' + k) % 26 + 'a');
//Where n is the number of shifts forward (ex: a + 2 = c)
}