import java. io.*;

public class ifWhileLoop
{

public static void main(String [] args) throws IOException{

     int num = 0; 
     int count = 0;
     int total = 0;

System.out.println("Enter a whole number, and -99 to quit: ");
BufferedReader br;
br = new BufferedReader( new InputStreamReader( System.in ) );
num = Integer.parseInt(br.readLine());


if(num==-99) System.exit(1);

     while (num != -99) { // test the condition
          count++; // increment counter 
          total += num; //accumulate the sum 
         // System.out.print("Enter a whole number, and -99 to quit: ");
    num = Integer.parseInt(br.readLine());
     } 

     float average = (float) total/count; // casting total to a float and promote count to a float
     System.out.println("You keyed in " + count + " numbers \n");
     System.out.println("The average is: " + new Float(average));

    }
}

This code works. I'm trying to get the code in the bottom to accomplish the same things as the code on the top.
I'm suppose to take out the while statement and only use If/Else statements.
Can anyone assist? I'm so close I can touch it.
Have a great day

import java. io.*;

public class ifWhileLoop2
{

public static void main(String [] args) throws IOException{

     int num = 0; 
     int count = 0;
     int total = 0;
float average;

System.out.println("Enter a whole number, and -99 to quit: ");
BufferedReader br;
br = new BufferedReader( new InputStreamReader( System.in ) );
num = Integer.parseInt(br.readLine());

//if(num==-99) 

    if (num == -99) { // test the condition
           System.exit(1);
    }
    else
          count++; // increment counter 
          total += num; //accumulate the sum 
         // System.out.print("Enter a whole number, and -99 to quit: ");
          num = Integer.parseInt(br.readLine());
     } 
{
}
     float average = (float) total/count; // casting total to a float and promote count to a float
     System.out.println("You keyed in " + count + " numbers \n");
     System.out.println("The average is: " + new Float(average));
}

I can only think of two ways to repeat the code in your while loop. One, another type of loop, like a for loop (though in this case a while loop seems to make a lot more sense) or through recursive calls to a function. I can't think of any way to repeat code using only if statements and without using any type of loop or recursion. I don't think Java has anything like a GOTO statement. I'm guessing this is for some kind of school assignment? I'm guessing your teacher is looking for recursion or some type of for-loop that functions like a while-loop, probably recursion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.