简体   繁体   English

我想用另一个字母替换一个字母。 “n”及以下的每个字母都有效,但上半部分不会改变字母。 有人可以解决这个问题吗?

[英]I want to replace a letter with another letter. Every letter from "n" and under works, but the top half doesn't change letters. Can somebody fix this?

import java.util.Scanner;导入 java.util.Scanner; //import //进口

public class project2 {公共 class 项目 2 {

public static void main(String[] args) {
    
   
    Scanner in = new Scanner(System.in); //setting scanner
    System.out.println("Please enter the text you wish to encode."); //question
    String place = in.nextLine();
    
    place = place.replace('a', 'z'); //Making what letters replace with what
    place = place.replace('b', 'y');
    place = place.replace('c', 'x');
    place = place.replace('d', 'w');
    place = place.replace('e', 'v');
    place = place.replace('f', 'u');
    place = place.replace('g', 't');
    place = place.replace('h', 's');
    place = place.replace('i', 'r');
    place = place.replace('j', 'q');
    place = place.replace('k', 'p');
    place = place.replace('l', 'o');
    place = place.replace('m', 'n');
    place = place.replace('n', 'm');
    place = place.replace('o', 'l');
    place = place.replace('p', 'k');
    place = place.replace('q', 'j');
    place = place.replace('r', 'i');
    place = place.replace('s', 'h');
    place = place.replace('t', 'g');
    place = place.replace('u', 'f');
    place = place.replace('v', 'e');
    place = place.replace('w', 'd');
    place = place.replace('x', 'c');
    place = place.replace('w', 'b');
    place = place.replace('z', 'a');
    
    System.out.println(place); //typing the encoded message
}

} }

This is what I have so far, but only the bottom half is working, the rest isn't replacing.这是我目前所拥有的,但只有下半部分在工作,rest 没有被替换。

So taking a look at this, I decided to send the following through your code.所以看看这个,我决定通过你的代码发送以下内容。

"abcdefghijklmnopqrstuvwxyz" 

In response, I got作为回应,我得到了

"aycdefghijklmmlkjihgfedcya".

Which is almost what I would expect.这几乎是我所期望的。 What I would expect is我期望的是

"abcdefghijklmmlkjihgfedcba". 

This leads me to the first problem.这让我想到了第一个问题。

place = place.replace('w', 'b');

That 'w' should be a 'y'.那个'w'应该是'y'。

Now the second problem is the one you're more specifically asking about.现在第二个问题是您更具体地询问的问题。 Why does it mirror at m?为什么它反映在 m 处? Well, we are going through this string sequentially and updating it after each change.好吧,我们将按顺序检查这个字符串并在每次更改后更新它。 So when we get to m al has been changed to zn.因此,当我们到达 m al 时,它已更改为 zn。 So as we continue through your code we will be changing all of the characters that were once al back to al.因此,当我们继续执行您的代码时,我们将把所有曾经是 al 的字符改回 al。

Instead, try this answer https://stackoverflow.com/a/45668235/12572585 I've tested it and it works fine.相反,试试这个答案https://stackoverflow.com/a/45668235/12572585我已经测试过它并且工作正常。

暂无
暂无

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

相关问题 Searchview 仅识别大写字母。 但我想要大写和小写字母结果 - Searchview identifying only Uppercase letter. But I want both upper and lower case letter results 需要用另一个特定字母替换输入文本的每个字母 - Need to replace every letter of inputed text with another specific letter 我需要在课堂上显示学生的数字标记以及他们的字母成绩。 我的代码不输出字母。 我怎样才能解决这个问题? - I need to display a student's numerical mark in a class as well as their letter grade. My code does not output the letter. How can I fix this? 无法替换字符串中的字母 - Can't replace a letter in a string 如何添加导致字母表中另一个字母的字母? - How can i add letters that result in another letter in the alphabet? 如何读取文件中的每个字母? - How can I read every letter from the file? 当我希望它返回每个字母词的计数时,这仅返回最大的字母词 - This is only returning the largest letter word, when I want it to return the count for every letter word Java 一个字母替换另一个字母的方法 - Java method to replace a letter with another 用字母表中的下一个字母替换字符串的每个字母,但保留空格和标点符号? - Replace every letter of string with next letter in alphabet but preserve spaces and punctuation? 程序仅打印出倒数第二个字母。 想要整个词倒退 - Program only prints out second last letter. want the whole word backwards instead
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM