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));
}