简体   繁体   English

无法更改字符串中的字符

[英]Having trouble changing characters in strings

So the question asked is: To change the characters of a string with 3 characters ahead of them so lets say the string is "AB cd" it would be changed to: "DE fg". 所以问的问题是:要改变字符串前面有3个字符的字符,所以假设字符串是“AB cd”,它将被改为:“DE fg”。 I am not good at programing but I have tried my best and come up with this: 我不擅长编程,但我已经尽了最大努力并想出了这个:

import java.util.*;

public class encrypt{

    public static void main(String[] args){

        Scanner reader = new Scanner(System.in);
        System.out.println("Enter a message to encrypt: ");
        String message = reader.nextLine();

        List<Character> Lowercase = Arrays.asList('a','b','c','d','e','f','g','h','i','j',
  'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');

        List<Character> Uppercase = Arrays.asList('A','B','C','D','E','F','G','H','I','J',
  'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

        for ( int i = 0; i < message.length(); i++ ) {  
            char c = message.charAt( i );

            if( c == ' '){
                continue;
            }
            else if (c != ' '){
                for ( int i = 0; i < Lowercase.size(); i++ ) {
                    char b = Lowercase.indexOf(i);

                    if(c == b){
                        message.charAt(i)=Lowercase.indexOf(i+3);
                    }
                }
            }

            for ( int i = 0; i < Uppercase.size(); i++ ) {
                char j = Uppercase.indexOf(i);

                if(c == j){
                    message.charAt(i)=Uppercase.indexOf(i+3);
                }
            }
        }
    }               
}

I have been getting errors like : 我一直在收到如下错误:

Problem1.java:20: error: variable i is already defined in method main(String[]) for ( int i = 0; i < Lowercase.size(); i++ ) { ^ Problem1.java:21: error: possible loss of precision char b = Lowercase.indexOf(i); ^ required: char found: int Problem1.java:23: error: unexpected type message.charAt(i)=Lowercase.indexOf(i+3); ^ required: variable found: value Problem1.java:27: error: variable i is already defined in method main(String[])

any help would be appreciated :) thanks. 任何帮助将不胜感激:)谢谢。

Besides the helpful link presented with dmcqu314's answer here some thoughts on your code and the occurring errors. 除了dmcqu314答案中提供的有用链接,还有一些关于代码和发生错误的想法。

Error in line 20 第20行出错

for ( int i = 0; i < message.length(); i++ ) {

As @Jama Jurayevich stated, you really should use another variable than 'i' for the inner loops. 正如@Jama Jurayevich所说,你真的应该使用另一个变量而不是'i'来表示内循环。 Use 'k' for instance. 例如,使用'k'。 That will help a bit - not a lot because of the other errors. 这会有所帮助 - 因为其他错误而不是很多。

Error in line 21 第21行出错

char b = Lowercase.indexOf(i);

Lowercase.indexOf(i) will retrieve a (signed) int type. Lowercase.indexOf(i)将检索(签名)int类型。 Assigning this to a char type (char b) provokes a type cast to something like an unsigned int (namely the char) - thus the hint of "possible loss of precision". 将其赋值为char类型(char b)会引发类型转换为类似unsigned int(即char)的类型 - 因此提示“可能会丢失精度”。

Error in line 23 第23行出错

message.charAt(i)=Lowercase.indexOf(i+3);

Here you are trying to assign an int value to string method. 在这里,您尝试为字符串方法分配一个int值。 That's not possible at all. 这根本不可能。 Strings are final objects in Java. 字符串是Java中的最终对象。 And there is no way to assign something to a method. 并且无法为方法分配内容。 If you want to append a char to string, you can do it this way (example): 如果要将字符串附加到字符串,可以这样做(示例):

String newString = new String();
...
newString = newString + 'a'

The ellipse is for other codings of your choice. 椭圆用于您选择的其他编码。

Hope these hints will help you a little to fight some confusions. 希望这些提示能帮助你解决一些困惑。

I'm guessing what you're attempting to accomplish is a Caesar Cypher. 我猜你正试图完成的是凯撒赛克。 Take a look at this post: Java Int & ASCII Question 看一下这篇文章: Java Int&ASCII Question

Here is one solution. 这是一个解决方案。 Compare that to your current code to see how to correct your errors. 将其与您当前的代码进行比较,以了解如何更正错误。

I also made it so it would loop around to the front of the array for the letters at the end of the alphabet. 我也这样做了它会循环到数组的前面,用于字母表末尾的字母。 IE: Inputting the letter 'Z' will output 'C'. IE:输入字母'Z'将输出'C'。

import java.util.*;

class encrypt
{
    public static void main (String[] args) throws java.lang.Exception
    {
        char c,b,j;

        Scanner reader = new Scanner(System.in);
        System.out.println("Enter a message to encrypt: ");
        String message = reader.nextLine();

        char[] messageArray = message.toCharArray();

        List<Character> Lowercase = Arrays.asList('a','b','c','d','e','f','g','h','i','j',
        'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');

        List<Character> Uppercase = Arrays.asList('A','B','C','D','E','F','G','H','I','J',
        'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

        for (int i = 0; i < message.length(); i++ ) {  
            c = messageArray[i];
            if (c != ' '){
                for (int x = 0; x < Lowercase.size(); x++ ) {
                    b = (char)(Lowercase.get(x));
                    if(c == b){
                        int n = (x+3)%Lowercase.size();
                        messageArray[i]=Lowercase.get(n);
                    }
                }
                for (int y = 0; y < Uppercase.size(); y++ ) {
                    j = (char)(Uppercase.get(y));
                    if(c == j){
                        int m = (y+3)%Lowercase.size();
                        messageArray[i]=Uppercase.get(m);
                    }
                }
            }
        }
        System.out.println(messageArray);
    }
}

I believe there is a easier way to solve your issue, and you can find your answer in ASCII table 我相信有一种更简单的方法来解决您的问题,您可以在ASCII table找到答案 在此输入图像描述

As you see, words have some number related to them like capital A is 65 and small c is 99 , as a result, you can go through the number and use casting processes to get char you want. 如你所见,单词有一些与它们相关的数字,例如大写A65而小c99 ,因此,你可以浏览数字并使用转换过程来获得你想要的字符。

read more casting from string to int 阅读更多从字符串转换为int

I think you should try this 我想你应该试试这个

Code: 码:

        String s = "eh az";
        for (int i = 0; i < s.length(); i++) {
            int a = s.charAt(i);
            if (a >= 97 && a <= 119) {
                System.out.print((char) (s.charAt(i) + 3));
            } else if (a >= 120 && a <= 122) {
                a -= 23;
                System.out.print((char) a);
            } else if (a == 32) {
                System.out.print(" ");
            }
        }

output: 输出:

hk dc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM