mercredi 23 mars 2016

How to sum user input in a while loop-JAVA

Below I am simply trying to sum the results of the user input. This may seem easy enough not to ask a question on it but I am novice student.

The program runs fine except that when it loops, the user inputs do not get added.

I have searched through similar questions and have tried different options but it still will not give me what I am looking for.

Any help is greatly appreciated!

     Scanner keyboard = new Scanner(System.in);

     double NICKEL  = .05;
     double DIME    = .10;
     double QUARTER = .25;

    //Other declarations
    double coin  = 0; //to hold input from user
    double total = 0;

    while(coin <= 1 ){
        System.out.print("Insert coin: ");
        coin = keyboard.nextDouble();

        if(coin==NICKEL || coin==DIME || coin==QUARTER){
            total += coin;
            System.out.println("Amount entered: " +coin);
        }
        else{
            System.out.println("Invalid!");

            System.out.print("Insert coin: ");
            coin = keyboard.nextDouble();
        }
    }

Aucun commentaire:

Enregistrer un commentaire