[TEXT]I'm trying to write an application that will input five numbers from the user. The program should print out the largest and smallest numbers. It should print how many of the numbers are two digit and how many of the numbers are three digit. It should also print out the sum of all two digit numbers and the sum of all three digit numbers. I have tried, but having problems, the code below executes but does not give the results needed, please help[/TEXT]

import java.util.Scanner;
public class minNmax
{
	public static void main(String args[])
	{
		Scanner input = new Scanner(System.in);
		int n1, n2, n3, n4, n5, Num2Digit=0, Num3Digit=0,Max=0, Min=0;
		int Num2Sum, Num3Sum;
		 Num2Sum=Num2Digit;
		 Num3Sum=Num3Digit;
		System.out.print("Enter first number: ");
		n1 = input.nextInt();
		System.out.print("Enter second number: ");
		n2 = input.nextInt();
		System.out.print("Enter third number: ");
		n3 = input.nextInt();
		System.out.print("Enter forth number: ");
		n4 = input.nextInt();
		System.out.print("Enter fifth number: ");
		n5 = input.nextInt();
	System.out.println("Sum of tow-digit numbers is ---> " + Num2Sum);
	System.out.println("Sum of three-digit numbers is ---> " + Num3Sum);
	System.out.println("There are total " + Num2Digit + " two digits numbers");
	System.out.println("There are total " + Num3Digit + " three digits numbers");
	System.out.println("The smallest number is " + Min);
	System.out.println("The Largest numver is " + Max);

		if(n1 >= 10)
		{
                           n1=Num2Digit;
			Num2Sum += n1;
			Min +=1;
		}
                  else if (n1 > 99)
		{
			n1=Num3Digit;
			Num3Sum += n1;
			Max +=1;
		}
                  else if(n2 >= 10)
		{
			n2=Num2Digit;
			Num2Sum += n2;
			Min +=1;
		}
                  else if (n3 > 99)
		{
			n2=Num3Digit;
			Num3Sum +=n2;
			Max +=1;
		}
                  else if(n3 >= 10)
		{
			n3=Num2Digit;
			Num2Sum += n3;
			Min +=1;
		}
                  else if (n3 > 99)
		{
			n3=Num3Digit;
			Num3Sum +=n3;
			Max +=1;
		}
                  else if(n4 >= 10)
		{
			n4=Num2Digit;
			Num2Sum += n4;
			Min +=1;
		}
                  else if (n4 > 99)
		{
			n4=Num3Digit;
			Num3Sum += n4;
			Max +=1;
		}
                  else if(n5 >= 10)
		{
			n5=Num2Digit;
			Num2Sum += n5;
			Min +=1;
		}
                 else if (n5 > 99)
		{
			n5=Num3Digit;
			Num3Sum += n5;
			Max +=1;
		}
	}
}

Recommended Answers

All 6 Replies

That will be because you print results before you actually calculate them. You can either:

  • move system print message at the end of your if/else section
  • create methods that will do your calculations and just call them from system prints

I tried moving the print statements out of the main method, but still not getting the desired output, how can I get the smallest and largest numbers form the user input and display them using if statements? How you please explain using example code

I did not said to move out of main method I said move it at the end of last if/else. Dead simple...

import java.util.Scanner;
public class minNmax
{
	public static void main(String args[])
	{
		Scanner input = new Scanner(System.in);
		int n1, n2, n3, n4, n5, Num2Digit=0, Num3Digit=0,Max=0, Min=0;
		int Num2Sum, Num3Sum;
		 Num2Sum=Num2Digit;
		 Num3Sum=Num3Digit;
		System.out.print("Enter first number: ");
		n1 = input.nextInt();
		System.out.print("Enter second number: ");
		n2 = input.nextInt();
		System.out.print("Enter third number: ");
		n3 = input.nextInt();
		System.out.print("Enter forth number: ");
		n4 = input.nextInt();
		System.out.print("Enter fifth number: ");
		n5 = input.nextInt();
		if(n1 >= 10)
		{
                           n1=Num2Digit;
			Num2Sum += n1;
			Min +=1;
		}
                  else if (n1 > 99)
		{
			n1=Num3Digit;
			Num3Sum += n1;
			Max +=1;
		}
                  else if(n2 >= 10)
		{
			n2=Num2Digit;
			Num2Sum += n2;
			Min +=1;
		}
                  else if (n3 > 99)
		{
			n2=Num3Digit;
			Num3Sum +=n2;
			Max +=1;
		}
                  else if(n3 >= 10)
		{
			n3=Num2Digit;
			Num2Sum += n3;
			Min +=1;
		}
                  else if (n3 > 99)
		{
			n3=Num3Digit;
			Num3Sum +=n3;
			Max +=1;
		}
                  else if(n4 >= 10)
		{
			n4=Num2Digit;
			Num2Sum += n4;
			Min +=1;
		}
                  else if (n4 > 99)
		{
			n4=Num3Digit;
			Num3Sum += n4;
			Max +=1;
		}
                  else if(n5 >= 10)
		{
			n5=Num2Digit;
			Num2Sum += n5;
			Min +=1;
		}
                 else if (n5 > 99)
		{
			n5=Num3Digit;
			Num3Sum += n5;
			Max +=1;
		}
	System.out.println("Sum of tow-digit numbers is ---> " + Num2Sum);
	System.out.println("Sum of three-digit numbers is ---> " + Num3Sum);
	System.out.println("There are total " + Num2Digit + " two digits numbers");
	System.out.println("There are total " + Num3Digit + " three digits numbers");
	System.out.println("The smallest number is " + Min);
	System.out.println("The Largest numver is " + Max);

	}
}

Thanks for help but it still doesnot give me desired out put the display result is zero only. Any help regarding this? Please help.

Thanks for help but it still doesnot give me desired out put the display result is zero only. Any help regarding this? Please help.

Your logic in the code is not correct..

I dont think you know how your code works.
If you want to print minimun value & maximum value , what is the use of incrementing and decrementing Min/Max.and printing it.

.

@maverick420 more initiative would be better, instead of waiting to be spoon-fed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.