-2

How do you write a for loop that will input 10 integer values from the user and keep track of smallest value entered? This is what I have so far. I'm so confused right now.

int value;
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
   System.out.print("Enter 1st number:");
   value = scan.nextInt();
Anindya Dutta
  • 1,972
  • 2
  • 18
  • 33
  • 1
    Now keep track of the min by maintaining the min seen so far and comparing it with the element just entered. – Anindya Dutta Feb 27 '17 at 02:31
  • I'm voting to close this question as off-topic because it's a request for hand-holding, not a request for programming help. – Jon Kiparsky Feb 27 '17 at 02:40
  • @JonKiparsky that's subjective, I am sure when you were starting out you didn't start out with red-black tree, stop being so condescending. One of your top answers is an answer to how do I append to a text file a simple google can give you the answer to that.. – Eddie Martinez Feb 27 '17 at 02:44
  • Possible duplicate of [Finding min element in an array](http://stackoverflow.com/questions/28569181/finding-min-element-in-an-array) – Anindya Dutta Feb 27 '17 at 02:52

1 Answers1

-2

This is a simple use of an if statement. You have to maintain a variable to keep track of lowest value entered and compare every input to it, if it is lower you replace it with the value new value.

I have provided a solution below, there are multiple ways of doing it. Try and see if you can find an alternative way so you can practice. Also notice the or statement if i == 0, analyze what happen if you remove that.

    int value=0;      
    Scanner scan = new Scanner(System.in);

    for (int i = 0; i < 10; i++)
    {
    System.out.print( "Enter 1st number:" );

    int inValue = scan.nextInt();
    if(inValue < value || i==0 )
     value = inValue;    
   }
Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
  • 1
    You should let the OP try to do something as easy as this himself by providing hints. Giving away code is useless. – Anindya Dutta Feb 27 '17 at 02:32
  • Also, your code won't run for basic inputs like `1 2 3 4`. – Anindya Dutta Feb 27 '17 at 02:33
  • Oy gewalt. Eduardo, are you planning on following the OP around for the rest of their career and doing their work for them? If not, it would be better to help them learn than to dump code and run. – Jon Kiparsky Feb 27 '17 at 02:41
  • @JonKiparsky thats what exactly what you did here no ? Oy? http://stackoverflow.com/a/17702136/1754020 – Eddie Martinez Feb 27 '17 at 02:46
  • 2
    (1) A code dump without an explanation of how it works is not a suitable answer for Stack Overflow. (2) You are not helping OP by doing their homework for them. – Dawood ibn Kareem Feb 27 '17 at 03:04
  • (1) there is an explanation, I don't know how much more you want me to explain. (2) He obviously has doubts and doesn't have someone to explain it to him, why shouldn't I provide an answer like everyone else here asks and gets, because it is "easy" for you, well it isn't easy for him. (3) Homework questions are allowed on SO http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions – Eddie Martinez Feb 27 '17 at 03:19
  • @EduardoDennis thank you so much Eduardo. Yes I'm new to coding and I'm learning Java in university and I don't really have anyone to help me out so I thought id try stack overflow. Thank you so much for your feedback! – mikowankenobi Feb 27 '17 at 03:28
  • @mikowankenobi no worries, we have all been there, some of us choose to forget :) – Eddie Martinez Feb 27 '17 at 03:49