#Can someone help me with this LOOPS (while) question?

1 messages · Page 1 of 1 (latest)

winged cloudBOT
#

<@&987246399047479336> please have a look, thanks.

winged cloudBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

agile terrace
#

Which part are you stuck on?

winged cloudBOT
#

Detected code, here are some useful tools:

Formatted code
javaimport java.util.Scanner;

public class LoopsWhileStatmentsQ4 {
  static Scanner input = new Scanner(System.in);
  public static void main(String[] args) {
    double val, subTotal = 0, total = 0, tax = 0.13, onesEntered = 0, sum = 0;
    System.out.println("Enter a value");
    val = input.nextInt();
    while (val !=  - 1) {
      System.out.println("Enter a value");
      val = input.nextInt();
      onesEntered++;
      sum += val;
      subTotal = sum * tax;
      total = subTotal + tax;
    }
    System.out.println("Inputs: " + onesEntered);
    System.out.printf("Subtotal: $" + "%.2f", subTotal);
    System.out.printf("Total: $" + "%.2f", total);
  }
}
agile terrace
#

You are multiplying floating point values, which can result in rounding errors

#

Do you have to use double?

#

What subtotal & total are you getting? What subtotal & total should you be getting?

winged cloudBOT
#

Detected code, here are some useful tools:

agile terrace
#

The first value never gets added to the subtotal

#

You should step through your code with a debugger to see what it's doing

#

You read a value from the user. You enter a loop. You read another value from the user. You add the value to the subtotal

#

First value was never added to the subtotal

#

Step through your code. Seems like you aren't understanding what the code that you wrote is doing

#

What do you think should be changed?

#

The code is doing exactly what you're telling it to do

#

You understand the problem now, yeah?

#

Of why the subtotal is lower than what you expect

agile terrace
agile terrace
#

val, the value, never gets added to the total

#

You never use sum, so not sure why that's there

#

sum is always 0 in your code

#

You need to think about the code you're writing. It doesn't make sense to do java subTotal += sum;

#

Because you never seem to change the value of sum. You're basically doing java subTotal += 0;

#

Yeah, you fixed one out of many problems

#

You're still ignoring the first problem I mentioned

#

You are writing code without caring about what the code does

#

That's why you're encountering bugs

#

I tried pointing out a problem, but you're ignoring it, or hoping I'll hand you the solution

#

Yeah

#

That first val = input.nextInt()

#

The user enters a number, it gets stored in val

#

But what do you do with it?

#

Read line by line

#
  1. You read the value
  2. You enter the loop
  3. You tell user to enter a value
  4. You read a value, overwriting the previous number stored in val
  5. You add value into sub total
#

You tell me

#

You have to put some thought into this

#

Does it seem right?

#

The code you have

#

The steps I mentioned above

#

What seems wrong about it?

#

What IDE are you using?

#

Set a breakpoint on your main method

#

Run in debug mode

agile terrace
#

Next to the line number, in the "gutter", click so a red dot appears

#

Then, to run in debug mode, click the Bug button instead of the Play button

#

The problem you're having is that you aren't properly seeing what your code is doing

#

Using a debugger will let you step through your code line by line, so you can better see what's going on, and better see where mistakes are happening

#

You don't need it on every line

#

Only the line where you want to start debugging

#

So keep the first breakpoint, get rid of all the others. Then, run in debug mode by clicking the Bug icon (or the Play icon in the gutter by the main method - you can choose to "Run in Debug mode" from there)

#

Take a guess

#

You need to give a user a random math problem

#

Yeah, but they'd be random

#

Exactly

#

Have you Googled about generating random numbers in Java?

agile terrace
#

Idk, I've never watched it

flint heart
#

Yes I can vouch for Bro Code

#

Havent watched that vid in particular but he has good vids in general

agile terrace
#

You don't need to ping me. There are other helpers here

agile terrace
#

What's the question you had?

winged cloudBOT
#

Closed the thread due to inactivity.

If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍

agile terrace
#

What confuses you?

#

You need to be more specific on what you're struggling with

#

How would you ask how much 1 player scored?

sonic plover
#

and then u just repeat that

#

write the code for one player

#

and then we can build the loop around it

#

doesnt matter

#

but step by step. first write said code

#

the loop variants are all absolutely identical in what they can achieve. u can essentially substitute each with another

agile terrace
#

The first sentence is telling you to prompt the user for how many players

#

The loop would iterate based on how many players there are

#

Yup

charred jacinth
#

you could save the current min and max in variables and adjust them whenever a new min/max appears