#Logic mistakes?

1 messages Ā· Page 1 of 1 (latest)

elfin snow
#

I am practicing for loops but idk if my code is right logic wise

icy troutBOT
#

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

coral birch
#

but idk if my code is right logic wise
What would make you think it isn't?

kind summit
fickle dagger
#

its not

coral birch
#

From the first look you're code logic's are correct
*your code's logic seems

kind summit
#

Off

fickle dagger
#

especially the first number input gets ignored

kind summit
#

Let me put this in quality fixing website

elfin snow
#

Also, what can I post if not screenshots?

coral birch
icy troutBOT
#

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.

fickle dagger
#

use this

elfin snow
#

Ah, okay. Thank you

coral birch
kind summit
fickle dagger
#

no

#

share as text

kind summit
coral birch
kind summit
coral birch
fickle dagger
#

also you might want to use a do-while loop here

elfin snow
placid oriole
#

No, because you know you need to accept input at least once.

elfin snow
#

I don't understand. Doesn't the for loop also accept input too? Sorry, I am new to Java

fickle dagger
#

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

elfin snow
#

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

fickle dagger
#

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

placid oriole
elfin snow
elfin snow
placid oriole
#

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.

elfin snow
#

Oh I see now. Thanks a lot!

elfin snow
#

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))}}

icy troutBOT
#

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.

placid oriole
#

And why do the first number twice?

elfin snow
elfin snow
fickle dagger
elfin snow
#

Okay

placid oriole
#
int number = scanner.nextInt();
int sum = 0;
do ( number = scanner.nextInt();
elfin snow
# fickle dagger please share it again with that formatting
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))}}
icy troutBOT
# elfin snow ``` public class Main{Ā  public class void main(string [] args) { System....

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]
elfin snow
placid oriole
#

I know.

#

But why once outside of the do while?

elfin snow
#

Oh wait...should I do it in the do-while loop?

#

Because the number changes?

placid oriole
#

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.

elfin snow
#

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); }}
icy troutBOT
fickle dagger
#

not really

#

again the first number is not used

elfin snow
#

but when I run the code and enter 1 it gives me out 1

fickle dagger
#

whatever you input first it is going to output 0

elfin snow
#

oh

fickle dagger
#

it seems like you dont really understand your code

#

try to explain us line by line what happens

elfin snow
#

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?
icy troutBOT
# elfin snow ``` public class Summ{ public static void main(String [] args) { //I...

Detected code, here are some useful tools:

Formatted code
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?
exotic bay
#

you assign a value to number (Scanner) before the loop starts

#

why?

fickle dagger
#

//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

elfin snow
elfin snow
exotic bay
#

you ask twice

elfin snow
exotic bay
placid oriole
#

That's what we've all been saying.

elfin snow
placid oriole
#

What? You do nothing with it, so you can just remove it.

exotic bay
placid oriole
#

Just mentally remove the do while, and look again at your code.

#

Because that's what the first do basically amounts to.

elfin snow
elfin snow
placid oriole
#

You need to capture the number, but you ignore the first instance.

elfin snow
exotic bay
#

you just need the one in the loop?

elfin snow
exotic bay
elfin snow
exotic bay
#

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

elfin snow
#

Like this?

exotic bay
#

no need to ask

#

that way, sum starts as 0

#

but then enters the loop

#

ask for the number

#

and prints it after

elfin snow
elfin snow
#
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); }}
    ```
icy troutBOT
exotic bay
#

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);```
elfin snow
#

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

exotic bay
#

don't forget to print "Done" at the end šŸ‘

elfin snow
#

XD yes, thanks

distant bay
#

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

placid oriole
icy troutBOT
#

@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 šŸ‘

elfin snow
elfin snow