jeudi 2 avril 2020

Validating input.nextFloat for double variable

This is a rather simple problem and I expect I'm making a silly mistake, but I'd much appreciate any help.

Context: user is inputting a double value in to the machine, but the value must only be in one the following denominations: 0.05 / 0.10 / 0.20 / 0.50 / 1.0 / 2.0. userValue and totalValue are variables of double type.

I am trying to figure out how to validate this on entry and at the moment have this written:

public static String addValue() {
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter your value in the following denominations only: 0.05/0.10/0.20/0.50/1.00/2.00");
        userValue = input.nextFloat();
        totalValue = (totalValue + userValue);
        if (totalValue != 0.05 || totalValue != 0.10 || totalValue != 0.20 || totalValue != 0.50 || totalValue != 1.0 || totalValue != 2.0) {
            userValue = 0;
            System.out.println("Please enter a value of correct denomination.");
        } else {
            totalValue = (totalValue + userValue);
            System.out.println("Total value: " + totalValue);

        }
        return Machine;
    }

It accepts any entry and adds to the total but doesn't present it, and presents "Please enter a value of correct denomination" regardless so I'm mixing something up somewhere.

Any help is much appreciated :)

Aucun commentaire:

Enregistrer un commentaire