Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why does it only get the first number?

+2 votes
asked Jan 27, 2021 by adafelly (140 points)
the code does not calculate count when while turns.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

int sum = 0, a, count = 0, count2 = 0, N, temp;

    Scanner scan = new Scanner (System.in);

    int b = scan.nextInt ();

    while (count2 < b)

      {

N = scan.nextInt ();

temp = N;

while (N != 0)

  {

    N = N / 10;

    count++; this count does not calculated after first one

  }

if (count == 3)

  {

    sum += temp; so it can't do this  and the result is wrong

  }

count2++;

      }

System.out.println("sum of three digit numbers is: "+sum);

    }

}

1 Answer

0 votes
answered Feb 8, 2021 by Peter Minarik (86,720 points)
edited Feb 8, 2021 by Peter Minarik

Quick Fix

You should reset count in every loop iteration.

A Better Solution

A better solution would include the following improvements:

  1. Provide instructions to the user. Tell them what the program is doing. Tell them what the program is expecting them to do.
  2. Declare variables on demand. This way you can limit their scope (visibility) to the bare minimum. No need to pollute your scope with names that do not really belong there. It confuses the reader and you as well (see above, you didn't reset count to 0 in every cycle of the loop; would you have limit the scope of count to the loop -- see digitCount below -- you wouldn't have had the problem).
  3. Use meaningful names. "N, b, tmp, count, count2" don't help the understanding of the code. Try to give names that really mean what the variable holds.
  4. Add comments to help the understanding what the code does. It's useful for calculations and instructions that are not self explanatory for the first glance.
Taking the above advices you could modify your code to look like something like below:
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please, enter how many numbers you'd like to input: ");
        int sum = 0;
        for (int numberCount = scanner.nextInt(); numberCount > 0; numberCount--)
        {
            System.out.println("Please, enter a number: ");
            int number = scanner.nextInt();
            int digitCount = 0;
            for (int tmpNumber = number; tmpNumber > 0; tmpNumber /= 10) // count the number of digits in `number`
                digitCount++;

            if (digitCount == 3)
                sum += number;
        }
        System.out.println("The sum of three digit numbers is: " + sum);
    }
}

I hope this helps. Good luck! :)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...