jeudi 24 septembre 2015

Printing negative values in Java

My variables are

float amountLeft, amount;
char v1;

My goal is to let the user know If he uses more than a $1000 he will be in debt, the user can keep purchasing but he will be in debt. I can't get my head around negative numbers. How would I keep track of the negative numbers? For example: "You just went over your limit. You owe us -5$ "

If you want to take a look at my code here it is.

 amountLeft = 1000.0f;
    while(amountLeft > 0) {
      System.out.println(String.format("%3s %,1.2f %3s", "You have"  , amountLeft, "money remaining to spend."));
      System.out.println("Enter the cost of the item you want buy");
    amount = new Scanner(System.in).nextFloat();
      System.out.println("are you sure you wanna purchase this item?");
      v1 = keyboard.next().charAt(0);
      if (v1 == 'y')
      {
        amountLeft = amountLeft - amount;
        System.out.printf("%.2f",amountLeft);

Aucun commentaire:

Enregistrer un commentaire