0
Scanner input = new Scanner(System.in);
int[] numbers = new int[10];

for (int i = 0; i < numbers.length; i++)
{
    System.out.println("Please enter number");
    numbers[i] = input.nextInt();
}

for (int i = 10 ; i > numbers.length ; i++)
{
    int min = numbers [i];
    if ( numbers [i] > min)
    numbers [i] = input.nextInt();
}
System.out.println("the min values is " + min  );

i stopped here ..please help
i want to count the minimum value from the input from the user

6
  • Why do you want to create an array for the min? By definition there is only one minimum value. Commented Dec 9, 2016 at 18:36
  • Do you have to use an array? You could just take the min of the input values. Commented Dec 9, 2016 at 18:36
  • Your loops are inconsistent, and probably that is part of your confusion. The 2nd loop is never entered. Commented Dec 9, 2016 at 18:37
  • ...and fix your compiler errors before posting, please. Commented Dec 9, 2016 at 18:37
  • 1
    i have to use the array and for while :) Commented Dec 9, 2016 at 18:37

3 Answers 3

0

You do not have to use 2 for loops. You do not even need an array. Take a look:

import java.util.*;

public class Main {
    public static void main (String[] args) {
        Scanner input = new Scanner(System.in);
        int min = Integer.MAX_VALUE;

        for (int i = 0; i < 10; i++) {
            System.out.println("Please enter number");
            int number = input.nextInt();

            if (number < min) {
                min = number;
            }
        }

        System.out.println("the min values is " + min);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

To take the minimum of 10 inputs you can do

Scanner input = new Scanner(System.in);
int min = InStream.range(0, 10)
                  .peek(i -> System.out.println("Please enter number"))
                  .map(i -> input.nextInt())
                  .min();
System.out.println("the min values is " + min);

what is wrong in your code

for (int i = 10 ; i > numbers.length ; i++)

This won't work because the last index is [9] not [10] also you are incrementing instead of decrementing, you need something like

for (int i = numbers.length-1 ; i >= 0 ; i--)

You would be able to see this in a debugger, or by reading the error message such as ArrayIndexOutOfBoundException: 10

5 Comments

Yes it works and does the job, but if the OP has problems with simple for loops, I don't think that your answer means anything to him.
@TDG I suspect using a debugger or reading error messages is some else which doesn't meaning anything either.
im here for learning ..
@mo7al876any my hint would be, when you get an error message, read it. If you don't know what it means, google it or at least post it in the question.
@mo7al876any I have added what would be the simplest fix to your code.
0

Firstly you need to understand a lot of thing with you code is wrong.

Approach 1:

Scanner input = new Scanner(System.in);
int[] numbers = new int[10];

for (int i = 0; i < numbers.length; i++)
{
    System.out.println("Please enter number");
    numbers[i] = input.nextInt();
}
int min = numbers [0]; // Need to initialize it here outside the loop
for (int i = 1 ; i < numbers.length ; i++) // Need to count from 2nd element to the last
{
    if ( numbers [i] < min) // changing condition if number is less than min
         min = numbers [i]; // setting min to that number
}
System.out.println("the min values is " + min  );

Approach 2: Here we check for the minimum value when the use is entering the value itself.

Scanner input = new Scanner(System.in);
int[] numbers = new int[10];
boolean flagForMin = true;
int min;
for (int i = 0; i < numbers.length; i++)
{
    System.out.println("Please enter number");
    numbers[i] = input.nextInt();
    if(flagForMin) {
        min=numbers[i];
        flagForMin=false;
    } else {
         if ( numbers [i] < min) 
            min = numbers [i];
    }
}
System.out.println("the min values is " + min  );

These are with the for loop approach you can change the loop to while by initialing before the loop & incrementing the counter in last statement of the loop.

EG:

Scanner input = new Scanner(System.in);
int[] numbers = new int[10];
boolean flagForMin = true;
int min;
int i = 0;
while( i < numbers.length)
{
    System.out.println("Please enter number");
    numbers[i] = input.nextInt();
    if(flagForMin) {
        min=numbers[i];
        flagForMin=false;
    } else {
         if ( numbers [i] < min) 
            min = numbers [i];
    }
    i++;
}
System.out.println("the min values is " + min  );

1 Comment

thannkkkks man you save me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.