我想用Java创建一个程序,以从user_input获取数字并将其设置为货币格式...这是我的代码
package Seperator_checker;
import java.util.Scanner;
public class Seperator {
public static void main(String[] args) {
Scanner number=new Scanner(System.in);
System.out.print("Please Enter Your Number: ");
String user_number=number.next();
if(user_number.length()> 3) {
user_number=user_number.substring(0,user_number.length()-3) + "," + user_number.substring(0,1);
System.out.println("________________________________________");
System.out.println("Your Currency Number Is: "+ user_number);
}
}
}
答案 0 :(得分:0)
我能想到的一个自发答案是将字符串拆分为一个字符列表,并向后浏览并在每第三步之后插入,
,直到到达起点为止。
伪代码:
Convert String to Char List
goto end of List
counter = 0
while havent reached beginning of list
counter += 1
if counter == 3
counter = 0
insert ',' into List
//maybe go back one here too depending on implementation of
//insert and which is the current element after inserting
end if
go one back one element in List
end while
对不起,我不知道如何编写伪代码