Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.ArrayList;
- public class TwoSmallest{
- public static double getRealNumber(Scanner in, double term, int count) {
- System.out.println("enter real number");
- double num = Double.parseDouble (in.nextLine());
- if (count>=2 && num==term) return num;
- while (num == term){
- System.out.println(num+" [this is the terminating value, not part of the set of numbers]");
- System.out.println("Enter different number: ");
- num = Double.parseDouble (in.nextLine());
- }
- return num;
- }
- public static void main(String[] args) {
- System.out.println("java TwoSmallest ");
- System.out.println("type a terminating value (real number) ");
- Scanner in = new Scanner(System.in);
- ArrayList<Double> arl = new ArrayList<Double>();
- double term = Double.parseDouble (in.nextLine());
- System.out.println(term+" [this is the terminating value, not part of the set of numbers]");
- double secondSmallest=0, smallest=0, num=0;
- int count=0, index=0;
- while(true) {
- num = getRealNumber(in, term, count);
- if (count>=2 && num==term) break;
- while (num==term) {
- num = getRealNumber(in, term, count);
- }
- arl.add(num);
- count++;
- }
- //System.out.println(arl.toString());
- for (int i=0; i<arl.size(); i++) {
- if (i==0 || arl.get(i)<smallest) {
- smallest = arl.get(i);
- index = i;
- }
- }
- arl.remove(index); // remove the smallest number
- for (int i=0; i<arl.size(); i++) {
- if (i==0 || arl.get(i)<secondSmallest) {
- secondSmallest = arl.get(i);
- index = i;
- }
- }
- System.out.println("\nSmallest = "+smallest);
- System.out.println("Second smallest = "+secondSmallest);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement