Hi, Im trying to write a program of a salary from a textbook problem, but am having a hard time. Hopefully you can get the gist of the program, I want it to keep asking for items of what the "salesperson" has sold, until an invalid item is entered, where it breaks and does the calculations of $200 + %9 of sales. The loop stops after 1 input, and you have to enter the items value rather than just item1 in the input. I would like to know how to fix this. Thanks!

package rand;
import java.util.Scanner;
public class rand19 {
    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);
        while (true) {
            double item1 = 239.99;
            double item2 = 129.75;
            double item3 = 99.95;
            double item4 = 350.89;
            double pay = 0.0;
            System.out.printf("Enter an item value for the salesperson: ");
            double salesperson = input.nextDouble();
            if (salesperson == item1) {
                pay = pay + item1;
            }
            else if (salesperson == item2) {
                pay = pay + item2;
            }
            else if (salesperson == item3) {
                pay = pay + item3;
            }
            else if (salesperson == item4) {
                pay = pay + item4;
            }
            else {
                break;
            }
        double basepay = 200;
        double basepaypercent = 9 % pay;
        double finalpay = 200 + basepaypercent;
        System.out.printf("Salespersons earnings: " + finalpay + "\n");
        System.out.println("--------------------------------------");
        }

    }
}

Recommended Answers

All 6 Replies

if you "break" you are going to skip the calculations. does the calculation need to be in the loop?

I was trying to add the item to the total each time, but I do not think this way is working, the break is only supposed to be if the inputs do not match, item1-4, but it is not working that way.

did you even read my question?

did you even read my question?

If you read my answer it clearly has my respone but if it wasnt clear enoygh... Where would i put the calculations and what would be put where the calculations currently are. Instead of replying like that it would be more helpful to continue with the question even if i didnt answer up to your expectations.

you only want to execute the calculations if you have finished receiving user inputs, currently you are doing them as you get the inputs. you want to break out of the loop after the user has finished inputting values. your calculations should be outside the loop and only be executed at the end.

How does the program know when the inputs are done? What do I do with the inputs if I am not doing calculations? It is a while loop, so how will it end if it is not equal to input1-4? I am lost on a way to assign each input a variable...other ways?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.