【发布时间】:2020-09-01 23:03:26
【问题描述】:
我正在学习 Java 课程,但我被这个问题难住了。我要完成大部分工作,直到需要将字符串转换为 ASCII 的部分。我能够将第一个字母输出到 Edit Unicode,但它停在那里。当我在临时文件中隔离代码并使用打印语句时,它会打印出它应该如何:
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 $202.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
}
}
如有任何指导,我将不胜感激。
【问题讨论】:
-
它是
Unicode(UTF-16),而不是ASCII。 -
@saka1029 呃,我为混淆道歉。我正在学习 ASCII,我混淆了我将修复它的术语。感谢您的回复。
-
“将字符串转换为 ASCII”是什么意思?
121 97 109 115的输出对于“yams”是否正确,或者这是您的代码所做的,但您想要别的东西? -
教师想要在方法中输入“yams”并将yams作为unicode等效输出。当我运行程序时,我只得到 121,但是当我单独使用该方法并在暂存文件上输出时,它会输出 121 97 109 115,这就是我所追求的。
-
你贴了很多代码。似乎其中大部分与您的问题没有直接关系 - 请编辑您的问题以删除此类代码,留下显示问题所需的绝对最小值。= - 即可能只有几行。理想情况下,请创建一个MCVE