【问题标题】:Caesar Cipher program, How to use only characters a-z?凯撒密码程序,如何仅使用字符 a-z?
【发布时间】:2018-07-16 17:31:42
【问题描述】:

Caesar Cipher 单独应用于字符串中的每个字母。每个字母必须在字母表中向前移动 n 步。如果一个字母从字母表的末尾('z')移开,那么它会一直移回字母表的开头('a')。 `

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;
        System.out.println("increasing the letters in string by n");

        while (true){
            System.out.println("Please enter your string");
            str = kb.nextLine(); 

            System.out.println("Please enter your n value");
            n = s.nextInt(); 

            String incrementedword=new String();
            for (int i=0;i<str.length();i++){
                incrementedword+=(char)(str.charAt(i)+n);
            }

            System.out.println ("your word is "+incrementedword);

        }
    }
}

例如,以下输入 (“hello world”,1) 应返回“ifmmp xpsme”

但是当我输入 (“hello world”,1) 时,输出是“ifmmp!xpsme”

我做错了什么?

【问题讨论】:

  • 您还“增加”了空格字符。排除它!
  • 另外,您的代码不适用于“z”。
  • 我希望 'z' 是 'a',但 'z' 会是 ' { ',我有点迷茫
  • 如果你将 1 加到 'z',Java 不会自动回绕回 'a',也不会自动排除空格和其他非字母字符。你必须告诉它做这些事情。
  • 你需要一些ifs:if (Character.isLetter(str.charAt(i))) {....}

标签: java string caesar-cipher


【解决方案1】:

在循环中放置一个条件,检查当前字符是从字母“a”到“z”小写:

用于检查当前字符是否为小写字母(a 到 z)的片段代码:

if (currentChar >= 'a' && currentChar <= 'z') {
//Process the shifting of the characters here
}

现在要将字母表中的每个字母向前移动 n 步,试试这个公式:

if (currentChar >= 'a' && currentChar <= 'z') {
   currentChar = (char) ((currentChar - 'a' + k) % 26 + 'a');
//Where n is the number of shifts forward (ex: a + 2 = c)
}

【讨论】:

    【解决方案2】:

    如果字母不在范围内,您需要控制环绕。对超出字母 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);
          }
     }
    }
    

    【讨论】:

      【解决方案3】:

      您也许可以将字母转换为 ascii,然后以这种方式递增它们?

      我的意思是,你必须设置约束,'a' 是 97,'z' 是 122。这将确保它保持在小写字符内,你可以告诉它循环回 'a '。

      【讨论】:

        【解决方案4】:

        这里有两个问题:

        • 在接近字母表末尾增加一些字符后,您将获得非字母数字字符(例如,z+1 将变成大括号 { - 请参阅 ASCII table)。尝试使用余数运算符 (%)。例如,117 % 100 会是 17,13 % 3 会是 1。这样 (x + 1) % 10 永远不会超过 10,会随着 x 的增加从 0 开始计数。
        • 如果您只想加密 a..z 和 A..Z 范围内的字符,请排除其他字符。简单的比较有效: if (x>='a' && x

        我不会在这里提供完整且有效的代码,因为看起来像家庭作业。

        【讨论】:

        • 值得一提为什么会出现那些非字母数字字符,例如通过链接 ASCII / UNICODE 表:>
        • 按照建议扩展。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多