java - How to change variable outside a loop -


public static void main(string[] args) {      scanner keyboard = new scanner (system.in);     double budget, total, expensetotal;      system.out.print("enter budget month: ");      budget = keyboard.nextdouble();                system.out.println("type -99 stop calculations\n");     byte start = 1;       double expense = 0;        while (expense != -99) {          system.out.print("enter expense " + start + " :");          expense = keyboard.nextdouble();           start++;                       if (expense == -99) {             system.out.println();              total = (budget - expense) - 99;              system.out.printf("your current total budget is: $%,.2f \n" , total);          }     }    } 

current output:

enter budget month: 1000 type -99 stop calculations

enter expense 1 :100

enter expense 2 :-99

your current total budget is: $1,000.00

desired output:

your current total budget is: $900.00

problem : expense declared outside of while statement , given number 0. figured since expense = keyboard.nextdouble(); in loop update expense placed outside of loop didn't. solutions? thanks.

try below one:

public static void main(string[] args) {      scanner keyboard = new scanner (system.in);     double budget;       system.out.print("enter budget month: ");      budget = keyboard.nextdouble();                system.out.println("type -99 stop calculations");      system.out.println();      byte start = 1;       double expense = 0;        while (expense != -99) {          system.out.print("enter expense " + start + " :");          expense = keyboard.nextdouble();          if (expense == -99) {             system.out.println();              system.out.printf("your current total budget is: $%,.2f \n" , budget);          }else{             budget = (budget - expense);         }         start++;                  } } 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -