Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javaacm中的Caesar密码_Java_Acm Java Libraries - Fatal编程技术网

javaacm中的Caesar密码

javaacm中的Caesar密码,java,acm-java-libraries,Java,Acm Java Libraries,我在Java和ACM中使用caesar密码时遇到问题。这是我的密码: import acm.program.*; public class Ceasar extends ConsoleProgram{ public void run() { println("This program encodes a message using a Caesar cipher."); int shifter=readInt("Enter the number of chara

我在Java和ACM中使用caesar密码时遇到问题。这是我的密码:

import acm.program.*;

public class Ceasar extends ConsoleProgram{

  public void run() {
    println("This program encodes a
    message using a Caesar cipher.");

    int shifter=readInt("Enter the number of character positions to shift: ");
    String msg=readLine("Enter a message :");
    String Solution=encodeCaesarCipher(msg,shifter);

    println("Encoded message: "+Solution);
  }

  private String encodeCaesarCipher(String str,int shift){
    String result="";

    for (int i=0;i<str.length();i++){
      char helper=str.charAt(i);
      helper=(helper+shift);

      if (helper>'Z'||helper>'z') helper =(helper-26);
      if (helper<'A'||helper<'a') helper=(helper+26);

      result= result+helper;
    }

    return result;
  }
}
导入acm.program.*;
公共类Ceasar扩展控制台程序{
公开募捐{
println(“该程序对
使用凯撒密码的消息。”);
int shifter=readInt(“输入要移位的字符位置数:”);
字符串msg=readLine(“输入消息:”);
字符串解决方案=密码(msg,移位器);
println(“编码消息:+解决方案”);
}
私有字符串密码(字符串str,int-shift){
字符串结果=”;
for(inti=0;i'Z'| helper>'Z')helper=(helper-26);
如果(helper'z')helper=helper-26;
^
必需:字符
找到:int
java:23:错误:可能丢失精度

如果(helper您不能在Java中将
int
添加到
char
中,除非明确表示您同意可能的精度损失(溢出/下溢)。添加
(char)
int
char

一起使用时,在Java中,如果不明确表示您同意可能的精度损失(溢出/下溢),就不能将
int
添加到
char
。添加
(char)
int
char

一起使用到每个位置,这是代码的固定版本。
将其与您的版本进行比较,以确保
您了解这些变化。

    public static void main(String[] args){
        String s = encodeCaesarCipher("abc", 5);
        System.out.println(s);
        s = encodeCaesarCipher("abc", 2);
        System.out.println(s);
        s = encodeCaesarCipher("abc", 1);
        System.out.println(s);
    }

    private static String encodeCaesarCipher(String str,int shift){
         String result="";
         for (int i=0;i<str.length();i++){
             int helper=str.charAt(i);
             helper=(helper+shift);
             if (helper>'Z'||helper>'z') helper =(helper-26);
             if (helper<'A'||helper<'a') helper=(helper+26);
                result= result+ (char)helper;
         }
         return result;
     }

这是您的代码的固定版本。
将其与您的版本进行比较,以确保
您了解这些变化。

    public static void main(String[] args){
        String s = encodeCaesarCipher("abc", 5);
        System.out.println(s);
        s = encodeCaesarCipher("abc", 2);
        System.out.println(s);
        s = encodeCaesarCipher("abc", 1);
        System.out.println(s);
    }

    private static String encodeCaesarCipher(String str,int shift){
         String result="";
         for (int i=0;i<str.length();i++){
             int helper=str.charAt(i);
             helper=(helper+shift);
             if (helper>'Z'||helper>'z') helper =(helper-26);
             if (helper<'A'||helper<'a') helper=(helper+26);
                result= result+ (char)helper;
         }
         return result;
     }

请一致地拼写凯撒,还有-问题是什么?请一致地拼写凯撒,还有-问题是什么?
fgh
cde
bcd