我希望我的代码取出0并放入M,B,T等。在1,10或100之后,但我不知道怎么做。基本上该程序询问你想要多少钱就像你要10000000000那么。然后kb得到那个int,并且int b得到a的长度,但是这是我遇到问题的地方,因为我找不到将10000000000变为10的方法,然后让它打印'你有$ 10T',
public class MinersHavenMoney_Client
{
private final static String filename = "input.txt";
public static void main(String[] args) throws FileNotFoundException {
int x;
Scanner kb = new Scanner(System.in);
MinersHavenMoney m1;
m1 = new MinersHavenMoney();
System.out.println("How much money do you want");
int a = kb.nextInt();
int b = String.valueOf(a).length();
kb.close();
if(b<=6)
System.out.println("You have $" + a);
else if(b>=7&&b<=9)
System.out.println("You have $" + c + "M");
else if(b>9&&b<=12)
System.out.println("You have $" + a + "B");
else if(b>12&&b<=15)
System.out.println("You have $" + a + "T");
//There is more but there is to much
}
}
答案 0 :(得分:4)
您可以比较这些值,而不是比较数字。
public static String scale(double value) {
return value < 1e3 ? asText(value) :
value < 1e6 ? asText(value / 1e3) + "K" :
value < 1e9 ? asText(value / 1e6) + "M" :
value < 1e12 ? asText(value / 1e9) + "B" :
asText(value / 1e12) + "T";
}
public static String asText(double d) {
return (long) d == d ? Long.toString((long) d) : Double.toString(d);
}
答案 1 :(得分:2)
首先你需要一个long
来支持数万亿(int
的范围-2 31 到2 31 -1 )。然后你可以使用正则表达式。如果数字以12 0结束,则为 trillions ,9 0s 数十亿,数百万为6 0,数千为0 。像,
long b = 10L * 1000 * 1000 * 1000 * 1000;
System.out.println(String.valueOf(b).replaceAll("[0]{12}$", "T")
.replaceAll("[0]{9}$", "B").replaceAll("[0]{6}$", "M")
.replaceAll("[0]{3}$", "K"));
输出(根据要求)
10T