I am new to Java language. I have been taken some personal tutorials, reading on my own and practicing by writing and solving exercise in the book I am using. Below is an exercise I am trying to solve.
Question.
(Find the Smallest Value) Write an application that finds the smallest of several integers.
Assume that the first value read specifies the number of values to input from the user.
Inline Code Example HereInline Code Example Here
See my solution in the code below;
package findingsmallestnumber;
import java.util.Scanner;
int d;
int s = 999999999;
int smal;
int k;
public static void main(String[] args) {
FindingSmallestNumber sn = new FindingSmallestNumber();
sn.readFirstNumber();
}
public void readFirstNumber(){
System.out.print("Enter number ");
Scanner ts = new Scanner(System.in);
int p1 = ts.nextInt();
k = p1;
readNumber();
}
public void readNumber(){
Scanner tp = new Scanner(System.in);
while ( d < k){
System.out.print("Enter number ");
int x1 = tp.nextInt();
if(x1 < s)
{
smal = x1;
s = x1;
}
System.out.printf("s = %d\n",s);
System.out.printf("d is %d, smal is %d\n",d,smal);
d++;
}
System.out.printf("Smmallest number entered is %d\n",smal);
}
}
My question;
I want experienced java developers in this forum to help me review this code and point out areas of improvement. I will like someone to solve this same exercise using a shorter algorithm and approach that will avoid initializing s with 999999999 if possible. Just show me another way of solving the exercise.
Expecting your response...
Andy