Find two smallest numbers using java? -
i need compute 2 smallest numbers in java?... plzz help.. usage arraylist , arrays not allowed.. user shall asked terminating value first , continue provide inputs , once user enters same terminating value program should output 2 smallest numbers inputs excluding terminating value..
additional details show me how it... sentinel value value if entered again user shall stop program , output 2 smallest numbers.
package hw4; public class s1 { public static void main(string[] args) { int input; int min; int min2; system.out.print("enter value act sentinel:"); int sentinel = io.readint(); system.out.println("enter numbers"); input = io.readint(); { min = input; min2 = input; if (input < min) { min = input; } input = io.readint(); } while (input != sentinel); // return results system.out.println("the smallest number is: " + min); system.out.println("the second smallest number is: " + min2); } }
i'm assuming homework, based on nature of question. ...
hint: if x
was smallest , y
is now smallest, x
is now second smallest.
hint 2: make sure program works when smallest , second smallest values equal.
Comments
Post a Comment