該程序的目標是從文件中獲取輸入並剝離每個數字並將其轉換爲文本。我們不能使用字符串操作。 例如 1:一個 21:二一 150:一五零向後打印並在同一行上
但礦山看起來像這樣1:一個21:一是一二150:一個一個兩個零五一
嗯,我圖出了很多,但礦井全部打印在同一行和向後你能指導我在正確的道路
謝謝
public class Main {
/**
* @param args the command line arguments
* @throws java.io.FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("translate.txt"));
while(scanner.hasNextInt()){
int number = scanner.nextInt();
System.out.println(number + ": " +NumberTanslatorTrial.tanslate(number));
// System.out.println(number + ": " + NumberTranslator.translate(number));
}
}
新類
public class NumberTanslatorTrial {
final private static String[] txt = {"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};
static int tempNumber;
static String finalString;
static int tempNumber1;
public static StringBuilder num = new StringBuilder();
public static String tanslate(int number) {
while (number > 9) {
tempNumber = number % 10;
number = number/10;
num.append(txt[tempNumber]);
num.append(" ");
finalString = num.toString();
}
if (number <= 9) {
num.append(txt[number]);
num.append(" ");
finalString = num.toString();
}
return finalString;
}
}
好吧,等NUM添加num.append( 「\ n」)<= 9,它給每個輸出新行,但仍然是錯誤的。 – alwill
而不是數學,你可以轉換爲一個字符串,並分析每個字符... – aliteralmind
@aliteralmind我們不能做字符串操作 – alwill