在Java中将字符串转换为Unicode后返回。

huangapple go评论141阅读模式
英文:

Returning a String converted to Unicode in Java

问题

以下是您所提供的代码部分的翻译:

  1. class Scratch {
  2. public static void main(String[] args) {
  3. String str = "yams";
  4. for (int i = 0; i < str.length(); i++) {
  5. int numUni = (int)str.charAt(i);
  6. int unicode = (int)numUni;
  7. System.out.print(unicode + " ");
  8. }
  9. }
  10. }
  11. public class Strings{
  12. // 步骤一 - concatenateStrings()
  13. public String concatenateStrings(String word1, String word2){
  14. String concantWord = word1 + " " + word2;
  15. return concantWord;
  16. }
  17. // 步骤二 - charToASCII()
  18. public int charToASCII(char character){
  19. int convertedChar = character;
  20. return convertedChar;
  21. }
  22. // 步骤三
  23. public char getLastChar(String str){
  24. int strLength = str.length();
  25. char lastChar = str.charAt(str.length() - 1);
  26. return lastChar;
  27. }
  28. // 步骤四
  29. public static String translateWord(String str){
  30. String unicodeOutput = "";
  31. for (int i = 0; i < str.length(); i++) {
  32. int numUni = (int)str.charAt(i);
  33. String unicode = numUni + " ";
  34. unicodeOutput += unicode;
  35. }
  36. return unicodeOutput;
  37. }
  38. // 步骤五
  39. public String madLib(String noun1, double number, String pastTenseVerb, String adjective, String noun2) {
  40. // 学生代码在这里
  41. return ""; // 这一行需要更改
  42. }
  43. public void runTests() {
  44. System.out.println();
  45. // 测试 concatenateStrings 方法
  46. System.out.println("测试 concatenateStrings 方法:");
  47. System.out.println("输入:'good','morning' \t 期望输出:good morning\t 实际输出:" + concatenateStrings("good", "morning"));
  48. System.out.println();
  49. // 测试 charToASCII 方法
  50. System.out.println("测试 charToASCII 方法:");
  51. System.out.println("输入:'c' \t 期望输出:99\t 实际输出:" + charToASCII('c'));
  52. System.out.println();
  53. // 测试 getLastChar 方法
  54. System.out.println("测试 getLastChar 方法:");
  55. System.out.println("输入:'Pterodactyl' \t 期望输出:L\t 实际输出:" + getLastChar("Pterodactyl"));
  56. System.out.println();
  57. // 测试 Translate word 方法
  58. System.out.println("测试 Translate word 方法:");
  59. System.out.println("输入:'yams' \t 期望输出:121 97 109 115\t 实际输出:" + translateWord("yams"));
  60. }
  61. public static void main(String [] args) {
  62. // 运行测试方法
  63. Strings f = new Strings();
  64. f.runTests();
  65. }
  66. }

请注意,我只对代码部分进行了翻译,而没有包括问题和附加信息。如果您有任何进一步的问题或需要解释,请随时提问。

英文:

I am taking a Java course and I am stumped on this question. I was to complete most of it up until the portion where I am required to convert a String to ASCII. I am able to get the first letter to output to Edit Unicode but it stops there. When I isolate the code on a scratch file and use a print statement it prints how it should:

  1. class Scratch {
  2. public static void main(String[] args) {
  3. String str = &quot;yams&quot;;
  4. for (int i = 0; i &lt; str.length(); i++) {
  5. int numUni = (int)str.charAt(i);
  6. int unicode = (int)numUni;
  7. System.out.print(unicode + &quot; &quot;);
  8. //return unicode; // this line will need to be changed
  9. }
  10. }

}

Output:

  1. 121 97 109 115
  2. Process finished with exit code 0

Here is the code that I have completed so far and my issue is with Step 4:

  1. public class Strings{
  2. // STEP one - concatenateStrings()
  3. public String concatenateStrings(String word1, String word2){
  4. String concantWord = word1 + &quot; &quot; + word2;
  5. return concantWord;
  6. }
  7. // STEP two - charToASCII()
  8. public int charToASCII(char character){
  9. int convertedChar = character;
  10. return convertedChar;
  11. }
  12. // STEP three
  13. public char getLastChar(String str){
  14. //student code here\
  15. int strLength = str.length();
  16. char lastChar = str.charAt(str.length() - 1);
  17. return lastChar; // this line will need to be changed
  18. }
  19. // step 4
  20. public static String translateWord(String str){
  21. //student code here
  22. for (int i = 0; i &lt; str.length(); i++) {
  23. int numUni = (int)str.charAt(i);
  24. String unicode = numUni + &quot; &quot;;
  25. return unicode; // this line will need to be changed
  26. }
  27. return &quot;&quot;;
  28. }
  29. // step 5
  30. public String madLib(String noun1, double number, String pastTenseVerb, String adjective, String noun2) {
  31. //student code here
  32. return &quot;&quot;; // this line will need to be changed
  33. }
  34. /**
  35. * A test block helps you test as you write. Eventually, you will learn
  36. * test driven development, in which every method you write will have tests you write
  37. * to make sure it works.
  38. *
  39. * Uncomment these lines out as you finish your various methods
  40. */
  41. public void runTests() {
  42. System.out.println();
  43. //Concatenate Strings test
  44. System.out.println(&quot;Testing concatenateStrings method: &quot;);
  45. System.out.println(&quot;Input: &#39;good&#39;,&#39;morning&#39; \t Expecting: good morning\t Actual: &quot; + concatenateStrings(&quot;good&quot;, &quot;morning&quot;));
  46. System.out.println();
  47. //Char to ASCII test
  48. System.out.println(&quot;Testing charToASCII method: &quot;);
  49. System.out.println(&quot;Input: &#39;c&#39; \t Expecting: 99\t Actual:&quot; + charToASCII(&#39;c&#39;));
  50. System.out.println();
  51. //Get Last Char test
  52. System.out.println(&quot;Testing getLastChar method: &quot;);
  53. System.out.println(&quot;Input: &#39;Pterodactyl&#39; \t Expecting: L\t Actual: &quot; + getLastChar(&quot;Pterodactyl&quot;));
  54. System.out.println();
  55. //Translate Word Test
  56. System.out.println(&quot;Testing Translate word method: &quot;);
  57. System.out.println(&quot;Input: &#39;yams&#39; \t Expecting: 121 97 109 115\t Actual: &quot; + translateWord(&quot;yams&quot;));
  58. // System.out.println();
  59. // Mad Libs Test
  60. // System.out.println(&quot;Testing Mad Libs method: &quot;);
  61. // System.out.println(&quot;Input: &#39;pear, 202.356, swam, purple, bear&#39;&quot;
  62. // + &quot;\nExpecting: Today I went to the store and bought a pear for $202.36.\nThen I swam and saw a purple bear.&quot;
  63. // + &quot;\nActual: &quot; + madLib(&quot;pear&quot;, 202.356, &quot;swam&quot;, &quot;purple&quot;, &quot;bear&quot;));
  64. }
  65. public static void main(String [] args) {
  66. // running test method
  67. Strings f = new Strings();
  68. f.runTests(); // this is not a static method (should it have been?) so you have to run it with the object
  69. }

}

I would appreciate any guidance.

答案1

得分: 1

很显然,如果你在循环内部使用return语句,那么循环只会执行一次。

你想要逐个ASCII码(实际上是Unicode代码点,正如其他人指出的,我不知道你在追随什么过时的80年代的陈旧东西,伙计——ASCII的时代早就过去了)地“构建”你的字符串,因此你需要一个StringBuilder,在循环中你想要追加 'numUni + " "' 到这个StringBuilder中,然后返回这个已经构建好的字符串:

  1. StringBuilder out = new StringBuilder();
  2. for (int i = 0; i < str.length(); i++) {
  3. int uni = (int) str.charAt(i);
  4. out.append(uni).append(" ");
  5. }
  6. return out.toString();
英文:

Obviously, if you return inside a loop, the loop will only ever execute once.

You want to 'build up' your string, one ascii code at a time (well, unicode codepoint, really - as others have pointed out, I don't know what dank late 80s outdated cruft you're following, mate - the days of ASCII are loooong gone), so you need a StringBuilder, you want to append 'numUni + " "' to this in the loop, and then return the stringbuilder, built up to a string:

  1. StringBuilder out = new StringBuilder();
  2. for (int i = 0; i &lt; str.length(); i++) {
  3. int uni = (int) str.charAt(i);
  4. out.append(uni).append(&quot; &quot;);
  5. }
  6. return out.toString();

答案2

得分: 0

你在第一次迭代后立即返回了 ASCII 值,编辑你的代码看起来应该像下面这样:

  1. public static String translateWord(String str) {
  2. // 学生代码在这里
  3. StringBuilder ascii = new StringBuilder();
  4. for (int i = 0; i < str.length(); i++) {
  5. int numascii = str.charAt(i);
  6. ascii.append(numascii).append(" ");
  7. }
  8. return ascii.toString();
  9. }

编辑:如下方评论者所提到的,请务必阅读有关字符串池、StringBuilder 和StringBuffer的内容:

英文:

You're returning the ascii value immediately after the first iteration, edit your code to look something like below:

  1. public static String translateWord(String str) {
  2. //student code here
  3. String ascii = &quot;&quot;;
  4. for (int i = 0; i &lt; str.length(); i++) {
  5. int numascii = str.charAt(i);
  6. ascii += numascii + &quot; &quot;;
  7. }
  8. return ascii;
  9. }

edit: As mentioned by commenter below, definitely read about String pool, StringBuilder and StringBuffer

huangapple
  • 本文由 发表于 2020年9月2日 07:03:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63696549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定