Kero Kero Kero Kero - 3 months ago 10
Java Question

Java Programming Looping

Write a java programs that prompts the user to enter user's name and 4 numbers, reads them, then prints the smallest and largest of all the numbers typed in by the user. The program will continue looping if the largest number is greater or equal to 10. At the end, program will show how many times the program has looped. How can i loop and find how many times the program looped ?

import java.io.*;
import java.util.*;

class largestLoop
{
public static void main(String [] args) throws IOException
{
String name;
int[] num = new int[4]; // save 4 number in array
int smallest = num[0], largest = num[0];

do{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your name : ");
name = stdin.readLine();
System.out.println(name + ", Please enter 4 numbers");

// Read for number using for loop
Scanner inData = new Scanner(System.in);
for(int i = 0; i < num.length; i++)
{
System.out.print("Enter " + (i+1) + " : "); // value i will + 1
num[i] = inData.nextInt();
}

// Find larger and smallest
for (int i : num)
{
if (i < smallest)
{
smallest = i;
} // end finding smallest
else if (i > largest)
{
largest = i;
} // end finding largest number
} // end finding largest and smallest values
System.out.println("Largest = " + largest);
System.out.println("Smallest = " + smallest);
}while(largest >= 10);
}
}


Output :

Enter your name : testing

testing, Please enter 4 numbers

Enter 1 : 2

Enter 2 : 6

Enter 3 : -5

Enter 4 : 16

Largest : 16

Smallest : 0

Enter your name : google

google, Please enter 4 numbers

Enter 1 : 5

Enter 2 : 8

Enter 3 : 1

Enter 4 : 6

Largest : 16

Smallest : 0

Enter your name : ....


The program become infinite loop. It should stop whenever user enter numbers that less than or equal to 10. The Largest and Smallest also does not right, when user enter <= 10 in next loop number. It will display a previous value or Largest. Also , the smallest keep display a 0 value.

Answer

First of all the assignment:

int smallest = num[0], largest = num[0];

is not dynamic in nature. Meaning the value of smallest and largest is not going to change everytime the value of num[0] changes. You need to do that manually everytime new values are entered.

Secondly you have not used any variable to actually count the number of loops.

Making these changes your code should look something like this:

package com.abhinav.testing;

import java.io.*;
import java.util.*;

class largestLoop
{   
public static void main(String [] args) throws IOException
{
    String name;
    int[] num = new int[4]; // save 4 number in array
    int smallest=0,largest=0;
    int count=0;
    do{
        count++;
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter your name : ");
        name = stdin.readLine();
        System.out.println(name + ", Please enter 4 numbers");

        // Read for number using for loop
        Scanner inData = new Scanner(System.in);
        for(int i = 0; i < num.length; i++)
        {
            System.out.print("Enter " + (i+1) + " : "); // value i will + 1 
            num[i] = inData.nextInt();
        }
        smallest = num[0];
        largest = num[0];
        // Find larger and smallest 
        for (int i : num) 
        {
            if (i < smallest) 
            {
                smallest = i;
            } // end finding smallest
            else if (i > largest) 
            {
                largest = i;
            } // end finding largest number 
        } // end finding largest and smallest values
        System.out.println("Largest = " + largest);
        System.out.println("Smallest = " + smallest);
    }while(largest >= 10);
    System.out.println("Number of times the loop ran= "+count);
}
}