Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.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
用于加密给定字符串的Java代码_Java - Fatal编程技术网

用于加密给定字符串的Java代码

用于加密给定字符串的Java代码,java,Java,我想要一个java代码,使用下面的两个主字符串对给定字符串进行加密 s1 = "qwertyuiopasdfghjklzxcvbnm"; s2 = "mnbvcxzasdfghjklpoiuytrewq"; 如果我们的输入字符串是“mnb”,则将其与s2进行比较,并在s1中添加相同的索引3,则输出将是“rty”,但我没有得到正确的输出 有人能帮我解决这个问题吗 public static void main(String[] args){ Scanner sc = new Scanne

我想要一个java代码,使用下面的两个主字符串对给定字符串进行加密

s1 = "qwertyuiopasdfghjklzxcvbnm";
s2 = "mnbvcxzasdfghjklpoiuytrewq";
如果我们的输入字符串是
“mnb”
,则将其与
s2
进行比较,并在
s1
中添加相同的索引3,则输出将是
“rty”
,但我没有得到正确的输出

有人能帮我解决这个问题吗

public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    String s1 = "qwertyuiopasdfghjklzxcvbnm";
    String s2 = "mnbvcxzasdfghjklpoiuytrewq";
    String input,out = "";
    System.out.println("enter input string");
    input = sc.nextLine();
    for(int i=0;i<s2.length();i++){
        if(input.charAt(i)==s2.charAt(i)){
            out+=s1.charAt(i+3);
        }
        System.out.println(out);
    }
    sc.close();
}
publicstaticvoidmain(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
字符串s1=“qwertyuiopasdfghjklzxcvnm”;
字符串s2=“mnbvcxzasdfghjklpoiuytrewq”;
字符串输入,out=“”;
System.out.println(“输入字符串”);
输入=sc.nextLine();

对于(int i=0;i您需要一个额外的循环来检查哪些字符与
s2
字符串匹配。除此之外,您还必须使用
模运算符来避免
数组索引脱离绑定

试试这个

public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String s1 = "qwertyuiopasdfghjklzxcvbnm";
        String s2 = "mnbvcxzasdfghjklpoiuytrewq";
        String input, out = "";
        System.out.println("enter input string");
        input = sc.nextLine();
        for (int i = 0; i < input.length(); i++) {
            for (int j = 0; j < s2.length(); j++) {
                if (input.charAt(i) == s2.charAt(j)) {
                     out += s1.charAt((j + 3)%26);
                }
            }

        }
        System.out.println(out);
        sc.close();
    }

您几乎找到了解决方案!问题是,当您输入s2的最后3个字符之一时,您必须使用模运算符(当位置大于25时,您将到达字符串的末尾,并且必须从开头开始搜索!)

publicstaticvoidmain(字符串[]args)引发异常{
扫描仪sc=新的扫描仪(System.in);
字符串s1=“qwertyuiopasdfghjklzxcvnm”;
字符串s2=“mnbvcxzasdfghjklpoiuytrewq”;
字符串输入,out=“”;
System.out.println(“输入字符串”);
输入=sc.nextLine();
对于(int i=0;i

为了避免错误的用户输入,您应该检查
位置,如果它是
-1
(如果在
s2
中找不到字符),并正确处理该情况(异常/输出打印+循环中断)

很抱歉,我真的不明白您想要实现什么……索引3从何而来?如果我的输入是“m”然后用s2搜索m,如果在s2中的1处找到m,那么在s1中的第一个索引是q.q,第三个字母将被输出。现在我想我知道你想做什么了…为什么不使用
模式
匹配器
来读取另一个字符串中的输入字符串?不,你不需要额外的循环…此代码提供StringIndexOfButo输入的undsException
“q”
out += s1.charAt((j + 3)%26);
public static void main(String[] args) throws Exception {
    Scanner sc = new Scanner(System.in);
    String s1 = "qwertyuiopasdfghjklzxcvbnm";
    String s2 = "mnbvcxzasdfghjklpoiuytrewq";
    String input,out = "";
    System.out.println("enter input string");
    input = sc.nextLine();
    for (int i = 0; i < input.length(); i++) {
        int position = s2.indexOf(input.charAt(i));
        position = (position + 3) % 26;
        out = out + s1.charAt(position);
    }
    sc.close();
}