#Logic mistakes?
1 messages Ā· Page 1 of 1 (latest)
<@&987246883653156906> please have a look, thanks.
Don't post screenshots
but idk if my code is right logic wise
What would make you think it isn't?
First. Don't post screanshots please. From the first look you're code logic's are correct
its not
From the first look you're code logic's are correct
*your code's logic seems
Off
especially the first number input gets ignored
Let me put this in quality fixing website
Idk chatgpt told me it's bad that I used sum+=number because the user types in new number and it can be confusing. But I didn't get it tbh
Also, what can I post if not screenshots?
Don't use LLMs for programming/coding
Please use this format for posting code:
```java
// Example java program
int value = 5;
System.out.println(value);
```
Which results in:
// Example java program
int value = 5;
System.out.println(value);
For syntax highlighting, you have to add the name of the language after the three backticks, like ```java. Please make sure to use exactly this format, so no space between the backticks and the language name, and a newline before the code starts. If done right, the syntax highlighting will even be applied to your text as you type, before sending.
use this
Ah, okay. Thank you
I mean, your code is text. so... as text
You can screenshot in pc. With snipping tool
Llms might make you're code logic's with no sense. It's better to not use it š
Have you ever tried copying and pasting code from an image?
*your code's logic
Don't pull up on me cause of a one word
I mean, I mentioned that exact phrase like 2 minutes ago, which is why I was quiet baffled that you messed it up the exact same way again
Srry. Didn't saw it
also you might want to use a do-while loop here
Why? Is it easier?
No, because you know you need to accept input at least once.
I don't understand. Doesn't the for loop also accept input too? Sorry, I am new to Java
any loop going to work here, but it makes sense using a do-while loop here because it is guaranteed to run once
do-while loop checks the condition after running while for/while loops check the condition before running
but you can try fixing your issue before changing to a different loop
But this for loop would also be guaranteed to run at least once, no? Like, if the user typed in 0 it would still fulfill the condition and give out the number 0 as the first output and sum
yeah in this case it would be guaranteed, as I said any loop works here
but the condition check in the for loop is kinda unnecessary which in reality doesnt matter
but the do-while makes it more clear to someone reading that code what is supposed to happen
But there is no added value in verifying it the first time.
Okay, then I will code this exercise also in a do-while loop to see the difference. Thank you
Verifying what? And which value should I add to verify it?
In your for loop the condition is always checked.
What use is there in checking whether sum <= 100 the first time?
Just write out which instructions happen in the first entry when using a for, versus a do while.
Oh I see now. Thanks a lot!
I have written the code in a do-while loop now. Are there any mistakes in the logic?public class Main{Ā
public class void main(string [] args) {
System.out.print("Enter the numbers");
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
int sum = 0;
do ( number = scanner.nextInt();
sum += number;
System.out.println(sum);
while(summ<=100))}}
Please use this format for posting code:
```java
// Example java program
int value = 5;
System.out.println(value);
```
Which results in:
// Example java program
int value = 5;
System.out.println(value);
For syntax highlighting, you have to add the name of the language after the three backticks, like ```java. Please make sure to use exactly this format, so no space between the backticks and the language name, and a newline before the code starts. If done right, the syntax highlighting will even be applied to your text as you type, before sending.
And why do the first number twice?
What do you mean?
please share it again with that formatting
Okay
int number = scanner.nextInt();
int sum = 0;
do ( number = scanner.nextInt();
public class Main{Ā
public class void main(string [] args) {
System.out.print("Enter the numbers");
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
int sum = 0;
do ( number = scanner.nextInt();
sum += number;
System.out.println(sum);
while(summ<=100))}}
Detected code, here are some useful tools:
[WARNING] The code couldn't end properly...
Problematic source code:
public class Main{Ā
public class void main(string [] args) {
System.out.print("Enter the numbers");
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
int sum = 0;
do ( number = scanner.nextInt();
sum += number;
System.out.println(sum);
while(summ<=100))}}```
Cause:
The code doesn't compile:
illegal character: '\u00a0'
<identifier> expected
')' expected
not a statement
while expected
')' expected
illegal start of expression
## System out
[Nothing]
because I want the program to read the input number
Idk how you call it again...Uh because I want to declare it as an int? Like, save it as an int number so I don't have to do it in the do while loop
Oh wait...should I do it in the do-while loop?
Because the number changes?
As it stands you're ignoring the first entered number, since you're not adding that to the sum nor initializing the sum at that value.
Ok, I changed it a little. Idk if it solves the issue
public class Summ{
public static void main(String [] args) {
System.out.print("Enter the numbers");
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
int sum = 0;
do {
System.out.println(sum);
number = scanner.nextInt();
sum+= number;}
while(sum<=100); }}
Detected code, here are some useful tools:
but when I run the code and enter 1 it gives me out 1
whatever you input first it is going to output 0
oh
it seems like you dont really understand your code
try to explain us line by line what happens
Okay
Wait I will do it in the code as comments
public class Summ{
public static void main(String [] args) {
//I prompt the user to enter a number
System.out.print("Enter the numbers");
//then I create an scanner object so the programm can read the input
Scanner scanner = new Scanner(System.in);
// net i declare a variable that is scanned by the scanner object
int number = scanner.nextInt();
// then i declare the sum variable and set it to 0 because it needs a value
int sum = 0;
// then I create a do while loop that will run as long as the sum doesn t exceed 100
do {
//when the user types in a number the current sum will be shown
System.out.println(sum);
//then if the user typds in the next number it will be scanned by the scanner object
number = scanner.nextInt();
//this number will be added to the current sum
sum+= number;}
while(sum<=100); }}
//So if I type in 1...the reason why it shows 0 must be because...i haven t told the code....that it has to add the first input to the sum?
Detected code, here are some useful tools:
public class Summ {
public static void main(String[] args) {
//I prompt the user to enter a number
System.out.print("Enter the numbers");
//then I create an scanner object so the programm can read the input
Scanner scanner = new Scanner(System.in);
// net i declare a variable that is scanned by the scanner object
int number = scanner.nextInt();
// then i declare the sum variable and set it to 0 because it needs a value
int sum = 0;
// then I create a do while loop that will run as long as the sum doesn t exceed 100
do {
//when the user types in a number the current sum will be shown
System.out.println(sum);
//then if the user typds in the next number it will be scanned by the scanner object
number = scanner.nextInt();
//this number will be added to the current sum
sum += number;
}
while (sum <= 100);
}
}
//So if I type in 1...the reason why it shows 0 must be because...i haven t told the code....that it has to add the first input to the sum?
I have a question
you assign a value to number (Scanner) before the loop starts
why?
//So if I type in 1...the reason why it shows 0 must be because...i haven t told the code....that it has to add the first input to the sum?
exactly, the first number that you scan outside of the do while is completely ignored
with the do while you dont even need it
you mean int sum = 0? I did that so the sum has a value at the start which I realized now that it is the mistake to my problem
Thank you very much for your help. It helped me a lot!
the variable is named number not sum, sum is ok
you ask twice
Yeah I just declare the number as an int which is scanned by the scanner object. Like each int number will be scanned so that s why I need it
But why if the value of the first one is unused
That's what we've all been saying.
I didn't know where to put it elsewhere so it reads the second value first
What? You do nothing with it, so you can just remove it.
you can add int to the second one and delete the first
Just mentally remove the do while, and look again at your code.
Because that's what the first do basically amounts to.
But I have added sum+=number; to the next line which is why I need it outside the loop or it shows me an error
It doesn't, I tried it
Do I not need the IntScanner at all?
You need to capture the number, but you ignore the first instance.
it does in my code if I remove it outside
why you have two sum+= number
you just need the one in the loop?
I have one outside the loop so the 0 will be added with the current input. Like if I type in 1 it shows 1...If I had not that sum+=number it would give me 0 as an output
Oh so you want to prompt a start value?
yeah
ask directly to sum
sum = scanner...
or if you just want sum to be shown after typing
put the println after the sum+= in the loop
that way will print it after it adds
Like this?
set sum to 0 since you already put the print last
no need to ask
that way, sum starts as 0
but then enters the loop
ask for the number
and prints it after
I have but now the output is as same as the input if I type in 1 all the time
show code
public class summ{
public static void main(String [] args) {
System.out.print("Enter the numbers");
Scanner scanner = new Scanner(System.in);
int sum = scanner.nextInt();
do {
int number = scanner.nextInt();
sum=0;
sum+= number;
System.out.println(sum);}
while(sum<=100); }}
```
Detected code, here are some useful tools:
I found the problem
sum=0
should be before loop
and delete sum= scanner.nextInt
the sum=0 is making so every time it iterates the value resets
System.out.print("Enter the numbers");
Scanner scanner = new Scanner(System.in);
int sum = 0;
do {
int number = scanner.nextInt();
sum += number;
System.out.println(sum);
} while (sum <= 100);```
oh
I see
Thank you very much. Gosh this is hard. I have to reread the whole chat so I am sure I got it
don't forget to print "Done" at the end š
XD yes, thanks
would suggest looking into how to use the debugger. your IDE comes with one, and it's very useful for these types of situations
debuggers can help a beginner dev catch about 90% of logic problems before the dev reaches out for help
@elfin snow
Your question has been closed due to inactivity.
If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.
Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.
When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.
Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.
With enough info, someone knows the answer for sure š
Okay, I will look into that. Thanks
Thank you, I will take a look!