我如何将值从一种方法返回到另一种方法&交换

发布于 2024-10-07 17:05:19 字数 1187 浏览 8 评论 0原文

我有这个,

public class Mapper implements ScramblerIF
{
 private static String map = "drsjckpwrypwsftylmzxopqtdo";

public static String charAt(String str) 
{
 //char[] chars = str.toCharArray();
int length = str.length();

 for(int i=0; i<length; i++)
{
 char aChar = str.charAt(i);
 char upper = Character.toUpperCase(aChar);
 int num = (upper - 'A');
char mChar = map.charAt(num);
 //String chard = Character.toString(mChar);

 StringBuffer buf = new StringBuffer( str);
   buf.setCharAt( i, mChar );
 }
   return str;
 }

public String scramble(String str) {
return charAt(str);
 }
}

我试图将其到达该方法

 public String scramble(String str) {
return charAt(str);
 }

返回该方法的计算值的

 public static String charAt(String str)

位置。不知道我哪里错了。

另外,除了使用

StringBuffer buf = new StringBuffer( str);
   buf.setCharAt( i, mChar );

我如何能够使用交换功能? 当我尝试时,

 char temp = chars[i];
chars[i] = chars[mChar];
chars[mChar] = temp;

我得到了 ArrayIndexOutOfBoundsException。 我想做的总结是“对于原始字符串中的每个字符,使用其在字母表中的位置来查找其在映射字符串中的替换。例如,字符串“dog”将被翻译为“jtp”。 ”

I have this

public class Mapper implements ScramblerIF
{
 private static String map = "drsjckpwrypwsftylmzxopqtdo";

public static String charAt(String str) 
{
 //char[] chars = str.toCharArray();
int length = str.length();

 for(int i=0; i<length; i++)
{
 char aChar = str.charAt(i);
 char upper = Character.toUpperCase(aChar);
 int num = (upper - 'A');
char mChar = map.charAt(num);
 //String chard = Character.toString(mChar);

 StringBuffer buf = new StringBuffer( str);
   buf.setCharAt( i, mChar );
 }
   return str;
 }

public String scramble(String str) {
return charAt(str);
 }
}

I am trying to get it to where the method

 public String scramble(String str) {
return charAt(str);
 }

returns the computed value from the

 public static String charAt(String str)

method. Don't know where i went wrong.

Also instead of using

StringBuffer buf = new StringBuffer( str);
   buf.setCharAt( i, mChar );

how would i be able to use the swap function?
When I try

 char temp = chars[i];
chars[i] = chars[mChar];
chars[mChar] = temp;

I am given an ArrayIndexOutOfBoundsException.
Summary of what i am trying to do is "For each character in the original string, use its position in the alphabet to look up its replacement in the map string. For example, the string “dog” would be translated to “jtp”."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高冷爸爸 2024-10-14 17:05:19

此拼字游戏使用您提供的替换地图。它还处理大写和小写大小写字母:

public class Mapper {
    //                           abcdefghijklmnopqrstuvwxyz
    private static String map = "drsjckpwrypwsftylmzxopqtdo";

    public static String scramble(String str) {

        if (!str.matches("[A-Za-z]*"))
            throw new RuntimeException(str + " contains weird characters");

        String out = "";
        for (char c : str.toCharArray()) {
            if (Character.isUpperCase(c)) {
                out += Character.toUpperCase(map.charAt(c - 'A'));
            } else {
                out += map.charAt(c - 'a');
            }
        }

        return out;

    }

    public static void main(String[] args) {
        System.out.println(scramble("David"));
    }
}

This scrables with the replacement-map you have provided. It also handles upper and lower case letters:

public class Mapper {
    //                           abcdefghijklmnopqrstuvwxyz
    private static String map = "drsjckpwrypwsftylmzxopqtdo";

    public static String scramble(String str) {

        if (!str.matches("[A-Za-z]*"))
            throw new RuntimeException(str + " contains weird characters");

        String out = "";
        for (char c : str.toCharArray()) {
            if (Character.isUpperCase(c)) {
                out += Character.toUpperCase(map.charAt(c - 'A'));
            } else {
                out += map.charAt(c - 'a');
            }
        }

        return out;

    }

    public static void main(String[] args) {
        System.out.println(scramble("David"));
    }
}
別甾虛僞 2024-10-14 17:05:19

如果您尝试设置缓冲区之外的字符,StringBuffer.setCharAt() 将引发异常。您还没有在缓冲区中放入任何内容。

此外,请查看创建缓冲区的时间。

StringBuffer.setCharAt() will throw an exception if you try to set a character beyond the buffer. You haven't put anything in the buffer.

In addition, take a look at when you are creating the buffer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文