在 Java 中返回转换为 Unicode 的字符串

Returning a String converted to Unicode in Java

我正在参加 Java 课程,但我对这个问题感到困惑。我要完成其中的大部分,直到我需要将字符串转换为 ASCII 的部分。我能够获得输出到 Edit Unicode 的第一个字母,但它停在那里。当我将代码隔离在临时文件上并使用 print 语句时,它会打印出它应该如何打印:

class Scratch {
public static void main(String[] args) {
    String str = "yams";
    for (int i = 0; i < str.length(); i++) {
        int numUni = (int)str.charAt(i);
        int unicode = (int)numUni;
        System.out.print(unicode + " ");
        //return unicode; //  this line will need to be changed
    }
}

}

输出:

121 97 109 115 
Process finished with exit code 0

这是我到目前为止完成的代码,我的问题是第 4 步:

public class Strings{
// STEP one - concatenateStrings()
public String concatenateStrings(String word1, String word2){
    String concantWord = word1 + " " + word2;
return concantWord;
}

// STEP two - charToASCII()
public int charToASCII(char character){
    int convertedChar = character;
return convertedChar;
}

// STEP three
public char getLastChar(String str){
    //student code here\
    int strLength = str.length();

    char lastChar = str.charAt(str.length() - 1);

    return lastChar; //  this line will need to be changed
}

 // step 4

public static String translateWord(String str){
    //student code here


        for (int i = 0; i < str.length(); i++) {
            int numUni = (int)str.charAt(i);
            String unicode = numUni + " ";


            return unicode; //  this line will need to be changed
        }

    return "";
}

// step 5
public String madLib(String noun1, double number, String pastTenseVerb, String adjective, String noun2) {
    //student code here
    return ""; // this line will need to be changed
}
/**
 * A test block helps you test as you write. Eventually, you will learn
 * test driven development, in which every method you write will have tests you write
 * to make sure it works.
 *
 * Uncomment these lines out as you finish your various methods
 */
public void runTests() {
     System.out.println();
     //Concatenate Strings test
     System.out.println("Testing concatenateStrings method: ");
     System.out.println("Input: 'good','morning' \t Expecting: good morning\t Actual: " + concatenateStrings("good", "morning"));

     System.out.println();
     //Char to ASCII test
     System.out.println("Testing charToASCII method: ");
     System.out.println("Input: 'c' \t Expecting: 99\t Actual:" + charToASCII('c'));

     System.out.println();
     //Get Last Char test
     System.out.println("Testing getLastChar method: ");
     System.out.println("Input: 'Pterodactyl' \t Expecting: L\t Actual: " + getLastChar("Pterodactyl"));

     System.out.println();
     //Translate Word Test
     System.out.println("Testing Translate word method: ");
     System.out.println("Input: 'yams' \t Expecting: 121 97 109 115\t Actual: " + translateWord("yams"));

    // System.out.println();
    // Mad Libs Test
    // System.out.println("Testing Mad Libs method: ");
    // System.out.println("Input: 'pear, 202.356, swam, purple, bear'"
    //      + "\nExpecting: Today I went to the store and bought a pear for 2.36.\nThen I swam and saw a purple bear."
    //      + "\nActual: " + madLib("pear", 202.356, "swam", "purple", "bear"));

}


public static void main(String [] args) {
    // running test method
    Strings f = new Strings();
    f.runTests();  // this is not a static method (should it have been?) so you have to run it with the object
}

}

如有任何指导,我将不胜感激。

显然,如果您 return 在循环中,循环只会执行一次。

您想 'build up' 您的字符串,一次一个 ascii 代码(好吧,unicode 代码点,真的 - 正如其他人所指出的,我不知道您是什么 80 年代后期的过时垃圾接下来,伙计——ASCII 的时代已经一去不复返了),所以你需要一个 StringBuilder,你想在循环中将 'numUni + " "' 附加到它,然后 return stringbuilder,构建一个字符串:

StringBuilder out = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
    int uni = (int) str.charAt(i);
    out.append(uni).append(" ");
}
return out.toString();

您将在第一次迭代后立即返回 ascii 值,将您的代码编辑为如下所示:

public static String translateWord(String str) {
    //student code here
    String ascii = "";
    for (int i = 0; i < str.length(); i++) {
        int numascii = str.charAt(i);
        ascii += numascii + " ";
    }
    return ascii;
}

编辑:正如下面的评论者所提到的,一定要阅读有关字符串池的内容,StringBuilder and StringBuffer