- Joined
- Apr 19, 2017
- Posts
- 734
- Solutions
- 3
- Reaction
- 1,078
- Points
- 395
paano mag divide after looping ??
Code:
Last edited:
i stop ung programanong silbi ng true nong sa while mo
need i pag add tapos ididivide kung ilan ung pinag addtry mo palitan ung jeff += jeff2 mo ng jeff = jeff / jeff2;
nopeganito gusto mo
pag 10 maximum ayy sampo ang ma.print na name.ng user??
System.out.println("Input number you want to add /n enter 0 to end ");
int jeff = 0;
float ave;
int counter = 0;
while(true) {
int jeff2 = input.nextInt();
if (jeff2 == 0)break;
counter += counter;
jeff += jeff2;
}
ave = jeff / counter;
System.out.println("Sum is : " + jeff);
System.out.println("Average is : " + ave );
int jeff = 0;
int counter = 0;
float ave;
char rep;
char repnum;
do{
System.out.println("Input any number you want to add");
do{
int jeff2 = input.nextInt();
jeff += jeff2;
counter += 1;
System.out.println("Do you want to add another number?/nY - Yes/nN - No/n");
repnum = input.next().charAt(0);
}while(repnum == 'N' || repnum == 'n');
ave = jeff / counter;
System.out.println("Sum is : " + jeff);
System.out.println("Average is : " + ave);
System.out.println("/n/nDo you want to get another average?/nY - Yes/nN - No/n");
}while(rep == 'N' || rep == 'n');
salamat sir cge try ko ...Ganto boss. kaso diko maintindihan bakit need pa ng 0 para i out, i do while mo na lang pag gusto na i cut yung pag enter ng number
Java:System.out.println("Input number you want to add /n enter 0 to end "); int jeff = 0; float ave; int counter = 0; while(true) { int jeff2 = input.nextInt(); if (jeff2 == 0)break; counter += counter; jeff += jeff2; } ave = jeff / counter; System.out.println("Sum is : " + jeff); System.out.println("Average is : " + ave );
System.out.println("Enter integer numbers only. Type 0 to stop.");
Scanner scan = new Scanner(System.in);
int input = 1;
int counter = 0;
int total = 0;
while(input != 0) {
input = scan.nextInt();
total += input;
if(input!=0) {
counter++;
}
}
System.out.println("SUM: " + total);
System.out.println("AVERAGE: " + total/counter);
System.out.println("Enter integer numbers only. Type 0 to stop.");
Scanner scan = new Scanner(System.in);
int input = 1;
int counter = 0;
int total = 0;
while(input != 0) {
input = scan.nextInt();
total += input;
if(input!=0) {
counter++;
}
}
System.out.println("SUM: " + total);
//divide by zero error checker
if(total>0){
System.out.println("AVERAGE: " + total/counter);
}
System.out.println("Enter integer numbers only. Type 0 to stop.");
Scanner scan = new Scanner(System.in);
int input = 1;
int counter = 0;
int total = 0;
while(input != 0) {
boolean isValid = false;
while(isValid==false) {
//invalid input checker
try {
input=scan.nextInt();
isValid=true;
}catch(Exception e) {
System.out.println("Invalid. Integer only.");
scan.nextLine();
isValid = false;
}
}
total += input;
if(input!=0) {
counter++;
}
}
System.out.println("SUM: " + total);
//divide by zero error checker
if(total>0) {
System.out.println("AVERAGE: " + total/counter);
}
daming error paps.. pero konting ayos nalangTry mo to boss, hirap magcode sa cp. Hahahah
Code:int jeff = 0; int counter = 0; float ave; char rep; char repnum; do{ System.out.println("Input any number you want to add"); do{ int jeff2 = input.nextInt(); jeff += jeff2; counter += 1; System.out.println("Do you want to add another number?/nY - Yes/nN - No/n"); repnum = input.next().charAt(0); }while(repnum == 'N' || repnum == 'n'); ave = jeff / counter; System.out.println("Sum is : " + jeff); System.out.println("Average is : " + ave); System.out.println("/n/nDo you want to get another average?/nY - Yes/nN - No/n"); }while(rep == 'N' || rep == 'n');
import java.util.Scanner;
public class test2 {
static Scanner sc = new Scanner (System.in);
public static void main(String []args){
int number,sum = 0,counter = 0;
while (true){
System.out.print("Input any Integer: ");
number = sc.nextInt();
if(number != 0){
sum += number;
counter += 1;
}
else break;
}
System.out.println("Sum: "+sum);
System.out.printf("Average : %.2f" , (float)(sum / counter));
}
}
Baka hindi mo nailagay si Scanner, nagbase lang kasi ako sa sinend mo ehdaming error paps.. pero konting ayos nalang
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
char rep;
char repnum;
int loop;
do{
int jeff = 0;
int counter = 0;
float ave;
System.out.println("How many numbers that you want to add?");
loop = input.nextInt();
do{
for (int x = 0;loop > x;x++){
System.out.println("Input any number you want to add");
int jeff2 = input.nextInt();
jeff += jeff2;
counter += 1;
}
System.out.println("Do you want to add another number?\nY - Yes\nN - No\n");
repnum = input.next().charAt(0);
}while(repnum == 'Y' || repnum == 'y');
ave = jeff / counter;
System.out.println("Sum is : " + jeff);
System.out.println("Average is : " + ave);
System.out.println("\n\nDo you want to get another average?\nY - Yes\nN - No\n");
rep = input.next().charAt(0);
}while(rep == 'Y' || rep == 'y');
}
}
Pwede pa to maimprove kailangan ng error checking eh. Haha!hmm, pa try nito paps
Java:import java.util.Scanner; public class test2 { static Scanner sc = new Scanner (System.in); public static void main(String []args){ int number,sum = 0,counter = 0; while (true){ System.out.print("Input any Integer: "); number = sc.nextInt(); if(number != 0){ sum += number; counter += 1; } else break; } System.out.println("Sum: "+sum); System.out.printf("Average : %.2f" , (float)(sum / counter)); } }
import java.util.Scanner;
public class test2 {
static Scanner sc = new Scanner (System.in);
public static void main(String []args){
int number,sum = 0,counter = 0;
do{
try
{
System.out.print("Input any Integer: ");
number = sc.nextInt();
sum += number;
counter += 1;
}
catch(Exception e)
{
System.out.println("Input Error: You need to put number. Try again...");
s.nextLine();
}
} while(number != 0);
System.out.println("Sum: "+sum);
System.out.printf("Average : %.2f" , (float)(sum / counter));
}
}