简体   繁体   English

计算值以更新JProgressBar

[英]Calculate a value to update JProgressBar

I would like to display the proportion of an initial value in a JProgressBar. 我想在JProgressBar中显示初始值的比例。

    private void updateProgressBars() { //Update the progress bars to the new values.
        int p1 = 0, p2 = 1; //Player numbers
        double p1Progress = (intPlayer1Tickets/intInitialPlayer1Tickets) * 100;
        double p2Progress = (intPlayer2Tickets/intInitialPlayer2Tickets) * 100;
        progressbarPlayerTickets[p1].setValue((int) p1Progress);
        progressbarPlayerTickets[p1].setString("Tickets left: " + Integer.toString(intPlayer1Tickets));
        progressbarPlayerTickets[p2].setValue((int) p2Progress);
        progressbarPlayerTickets[p2].setString("Tickets left: " + Integer.toString(intPlayer2Tickets));
   }

In this code, the intention was to calculate the percentage of the amount of tickets left a player has. 在此代码中,其目的是计算玩家剩余的彩票数量的百分比。 intInitialPlayer1Tickets and intInitialPlayer2Tickets were both set to 50. intPlayer1Tickets and intPlayer2Tickets were then set to their respective initial tickets value (ie both set to 50 as well). intInitialPlayer1TicketsintInitialPlayer2Tickets都设置为50。然后将intPlayer1TicketsintPlayer2Tickets设置为其各自的初始票证值(即也都设置为50)。 When I subtract any number from intPlayer1Tickets or intPlayer2Tickets (eg intPlayer1Tickets = 49 , intInitialPlayer1Tickets = 50 ), their respective progress bars' value would be set to 0, which is not my intention. 当我从intPlayer1TicketsintPlayer2Tickets减去任何数字时(例如intPlayer1Tickets = 49intInitialPlayer1Tickets = 50 ),它们各自的进度条的值将设置为0,这不是我的意图。 Both progress bars have their min and max values set to 0 and 100. 两个进度条的最小值和最大值均设置为0和100。

So how would I make it so it would reflect the proportion of tickets left as a percentage? 那么,我将如何做到这一点呢?它将以百分比形式反映剩余门票的比例?

You are doing integer math and then converting it to a double. 您正在做整数数学,然后将其转换为双精度。 In integer math when you divide a number with a number that is bigger, the answer is always 0. 在整数数学中,当您将数字除以更大的数字时,答案始终为0。

You want to get Java to do your math with floating point numbers rather than with integers. 您想让Java使用浮点数而不是整数来进行数学运算。 The easiest way to do this is to make your divisor a double. 最简单的方法是将除数加倍。

When you run this code 当您运行此代码时

public class Numbers{
    public static void main(String []args){
        int five = 5;
        int ten = 10;
        System.out.println(five/ten);
        System.out.println(five/(double)ten);
        System.out.println((five/(double)ten)*100);
     }
}

You get this as the output 您将其作为输出

0
0.5
50.0

So, to answer your question what you want is something like this 因此,要回答您的问题,您想要的是这样的东西

double p1Progress = (intPlayer1Tickets/(double)intInitialPlayer1Tickets) * 100; 

But you'd be just fine using float for this instead of doubles. 但是,使用float代替双精度就可以了。

Cast the value of intPlayer1Tickets , intPlayer2Tickets , intInitialPlayer1Tickets and intInitialPlayer2Tickets to double before you calculate. 演员的价值intPlayer1TicketsintPlayer2TicketsintInitialPlayer1TicketsintInitialPlayer2Ticketsdouble你计算之前 As in: 如:

    double p1Progress = (((double) intPlayer1Tickets)/((double) intInitialPlayer1Tickets)) * 100;
    double p2Progress = (((double) intPlayer2Tickets)/((double) intInitialPlayer2Tickets)) * 100;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM