簡體   English   中英

如何顯示星號的數量至最接近的千位?

[英]How to show number of asterisks to nearest thousand?

    bar1 = store1/1000;
    System.out.print("Store 1: ");
    for(int i = 1; i <= bar1; i++)
        System.out.print("*");

如何顯示“ *”的數字四舍五入到最接近的千位。 現在,它只是四舍五入。

采用

bar1 = (store1+500)/1000;
    System.out.print("Store 1: ");
    for(int i = 1; i <= bar1; i++)
        System.out.print("*");

使用此代碼:

    double store1 = 1095;
    long bar1 = Math.round(store1 / 1000);
    // int bar1 = store1/1000;
    System.out.print("Store 1: ");
    for (int i = 1; i <= bar1; i++)
        System.out.print("*");

或者如果stre 1是一個int並且您不能更改它,則可以使用:

    int store1 = 1095;
    long bar1 = Math.round(((double)store1) / 1000);
    // int bar1 = store1/1000;

它將把store1 / 1000的值四舍五入到最接近的1000。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM