开发者

Java do while look proper coding return value?

My question is about the increment I am asking how do you return data from the do while loop that is suppose to be set in each 开发者_JS百科value.

the totalSales inside of my do while loop should have data set when it comes out of the loop it does not save the data into the totalSales or totalItems values.

// The system print out values are empty.
      System.out.printf(salesID + " you entered a total of: " + totalItems + "With a total sales of $%15.2f" + totalSales); 

I am trying to return data from a freaky do while or the if statement loop and it will not return the items I set why?

// yesno to n for first loop through.

do {
      System.out.println("Welcome Sales ID: " + salesID + " please enter the samsung TV sold: \n");
      samsungProduct = scanValue.next();
      System.out.println("Please enter your total TV sales amounts individually for today \n");
      singleSale = scanValue.nextDouble();
      totalSales = totalSales + singleSale;
      totalItems = totalItems++;
      System.out.println("Do you want to enter another sales total(Y/N): \n ");
      yesNo = scanValue.next();

}        while (!yesNo.equalsIgnoreCase("N"));

// get the items set inside of the do while loop.

// The system print out values are empty.
      System.out.printf(salesID + " you entered a total of: " + totalItems + "With a total sales         of  $%15.2f" + totalSales); 


This is wrong:

totalItems = totalItems++;

You should do

totalItems++;

What you did is equal to:

int temp = totalItems;
totalItems = totalItems + 1;// (totalItems++)
totalItems = temp;


This is your problem: totalItems = totalItems++;

After this line of code the value of the totalItems is not changed.

Use totalItems++; instead

The problem is that i++ increment i, but returns the old value. So when you do i = i++ you assign the old value to i - so i is unchanged.


totalItems = totalItems++; is a do nothing operation.

See here: Post Increment Operator And Assignment


System.out.printf(salesID + " you entered a total of: " + totalItems 
    + "With a total sales of $%15.2f" + totalSales);

should be

System.out.printf(salesID + " you entered a total of: " + totalItems 
       + "With a total sales of $%15.2f", totalSales);
                                       ^^^^


I don't see anything obviously wrong with your loop, but running it (having created the appropriate variables) results in this exception:

Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '15.2f'
    at java.util.Formatter.format(Formatter.java:2432)
    at java.io.PrintStream.format(PrintStream.java:920)
    at java.io.PrintStream.printf(PrintStream.java:821)
    at ScratchMain.main(ScratchMain.java:26)

This tells me that you're not using printf correctly. If you use $f (or the more complex $%15.2f), then the value you want to put there needs to be passed to printf as an argument and not just concatenated to the String:

System.out.printf(salesID + " you entered a total of: " + totalItems + "With a total sales         of  $%15.2f", totalSales);

Also, you have the code totalItems = totalItems++; in there, which is effectively a no-op (i.e. it does nothing). Replace it with either totalItems++; or totalItems = totalItems + 1; (if you want to make it explicit).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜