Java application that inputs one salesperson's items and their corresponding amount sold for last week, then calculates and displays that salesperson's earnings.
for (int item =1; item < 5; item++){
System.out.printf("\nHow many of item %d have you sold: ", item);
count=input.nextInt();
if (item == 1)
sales = item1* count;
if (item == 2)
sales2 = item2 * count;
if (item == 3)
sales3 = item3 * count;
if (item == 4)
sales4 = item4 * count;
}
You could keep this looping until user inputs the correct amount.You can make use of a default value which the user enters to exit the loop. Based on my understanding:
while(true)
{
System.out.printf("\nHow many of item %d have you sold: (Enter -999 to exit)", item);
count=input.nextInt();
if(count== -999) //Or some default value of your choice, exit the loop
{
System.out.printf("Program terminated");
break;
}
for (int item =1; item < 5; item++)
{
if (item == 1)
sales = item1* count;
if (item == 2)
sales2 = item2 * count;
if (item == 3)
sales3 = item3 * count;
if (item == 4)
sales4 = item4 * count;
}
}
A while loop is better, so you can do something like this:
While(true){
System.out.printf("\nHow many of item %d have you sold: ", item);
count =input.nextInt();
if(certain condition is reached)
break;
if (item == 1)
sales = item1* count;
else if (item == 2)
sales2 = item2 * count;
else if (item == 3)
sales3 = item3 * count;
else if (item == 4)
sales4 = item4 * count;
}
Then you can add a break
statement somewhere within one of the control if
conditions or else where within the loop to exit the loop. For example, you can use a counter to count how many times they have entered values and then break out of the loop when that is reached. It is all up to you and your requirements.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.