The program will ask the user to enter the numbers and then will get the total sum and average inputted.
with methods
static Scanner scan = new Scanner (System.in);
public static void main(String[]args){
int limit=0;
int number=0;
int counter=0;
int sum=0;
message("Enter how many times");
limit = scan.nextInt();
while (counter<limit){
message("Please enter the following numbers");
number = scan.nextInt();
sum += number;
counter++;
}
System.out.println(sum);
}
static void message(String myText){
System.out.println(myText);
}
with methods
static Scanner scan = new Scanner (System.in);
static int sum=0;
public static void main(String[]args){
int limit=0;
int number=0;
int counter=0;
message("Enter how many times");
limit = scan.nextInt();
while (counter<limit){
message("Please enter the following numbers");
number = scan.nextInt();
getSum(number);
counter++;
}
/* you can use the following loops below...
do{
counter++;
message("Please enter the following numbers");
number = scan.nextInt();
getSum(number);
}while (counter<limit);
for (counter=0;counter<limit;counter++){
message("Please enter the following numbers");
number = scan.nextInt();
getSum(number);
} */
message("Total Sum is equals to " + sum);
message("Total Avearage is equals to " + sum/counter);
}
static void message(String myText){
System.out.println(myText);
}
static void getSum(int num){
sum += num;
}
0 comments:
Post a Comment