java解决凯撒密码问题

    xiaoxiao2021-03-25  149

    java 凯撒密码问题

    class hello { String s; int key; hello (String es,int n){ s=es; key=n; } public String process(){ String es=""; for (int i=0;i<s.length();i++){ char c=s.charAt(i); if(c>='a'&&c<='z'){ c+=key%26; if(c<'a') c+=26; if(c>'z') c-=26; } else if (c>='A'&&c<='Z'){ c+=key%26; if(c<'A') c+=26; if(c>'Z') c-=26; } es+=c; } return es; } public static void main(String args[]){ String s="hello"; hello c=new hello(s,3); String str =c.process(); System.out.println("Encrypted string: "+str); hello c1=new hello(str,-3); str =c1.process(); System.out.println("Decrypt string: "+str); } }

    这个问题也比较简单,注释也没写,但是我出现中文乱码了,等我解决了就来改。昨天晚上我用sublime写了之后,在控制台运行的时候中文出现乱码,我知道了就是sublime中文的编码格式是utf-8,但是windows默认的中文编码是ansi,所以就会乱码,要是直接用文本文档保存就不i会出现乱码。eclipse也是utf-8编码,但是他是自己编译运行,所以没错。要是在控制台运行也出现中文乱码。英文基本都是统一的没问题。

    转载请注明原文地址: https://ju.6miu.com/read-5761.html

    最新回复(0)