Beginner: Min Value in Array (Java) -
when execute program, prints max value fine, min value prints zero. continue scratching head... can see wrong here? looking.
import java.util.scanner; public class minmax { public static void main(string[] args) { scanner kb = new scanner(system.in); int [] numbers = new int[5]; int max = numbers[0]; int min = numbers[0]; (int = 0; < numbers.length; i++) { system.out.println("enter next number:"); numbers[i] = kb.nextint(); if (numbers[i] > max) { max = numbers[i]; } if (min > numbers[i]) { min = numbers[i]; } } system.out.println("the maximum value in array " + max); system.out.println("the minimum value in array " + min); } }
the issue when array declared, ints in array set 0. setting min numbers[0]
set min 0. if that's not min, code fail.
in case, don't need array - store whatever user inputted. aside, check whether i==0
, when does, set min , max numbers[0]
. (if didn't same max, array of negatives fail.)
Comments
Post a Comment