In: Computer Science
Java Program.
Sentinel While Loop Lab
Do the following:
Include the following:
NOTE 1:
NOTE 2:
1. Declare all variables within the data declaration section of
each class and method. (-.1)
2 Do not get input on the same line as a variable
declaration. (-.1)
3. Do not place an equation for computation on the same line as
declaring a variable. (-.1)
4. Do not place an equation for computation on the same line as an
output statement. (-.1)
Thanks to whoever helps me!!! :)
code:
import java.util.Scanner;
class grade
{
public static void main(String[] args)
{
int grade,count=0;
double sum=0,average;
/*Declaring variables*/
Scanner scnr=new
Scanner(System.in);/*Creating Scanner object*/
System.out.print("enter a grade or
a -1 to quit: ");
grade=scnr.nextInt();
/*Reading grde from the
user*/
sum+=grade;
while(grade!=-1)
{
/*This loop
execute untill grade is equals to -1*/
System.out.print("enter a grade or a -1 to quit: ");
grade=scnr.nextInt();/*Reading grade form the user*/
sum+=grade;
/*Adding grade
to the sum*/
count+=1;/*Increading the count*/
}
sum+=1;
/*HEre we add 1 to sum
because
in the loop we add -1 to sum when
user enters -1*/
if(count!=0)
{
average=(sum/count);
/*If count not
equals to we prin the average*/
System.out.println("Average Grade:"+average);
}
else
{
/*Else we print
average as 0*/
System.out.println("Average Grade:"+0.0);
}
}
}
Output:
Indentation: