#Can someone help me with this LOOPS (while) question?
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
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.
Which part are you stuck on?
Detected code, here are some useful tools:
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);
}
}
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?
Detected code, here are some useful tools:
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
Did you read this @rancid sphinx ?
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
- You read the value
- You enter the loop
- You tell user to enter a value
- You read a value, overwriting the previous number stored in
val - 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
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?
Idk, I've never watched it
Yes I can vouch for Bro Code
Havent watched that vid in particular but he has good vids in general
You don't need to ping me. There are other helpers here
What's the question you had?
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 👍
What confuses you?
You need to be more specific on what you're struggling with
How would you ask how much 1 player scored?
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
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
you could save the current min and max in variables and adjust them whenever a new min/max appears