What's new

# HelpJava Pahelp about loops java

#### RikkuRikku

Established
, Last edited:
paano mag divide after looping ??

Code:

#### Arcturus

##### Forum Guru
lagay ka ng int count, tas nag iincrement sya kada input ng user ng number maliban sa 0

#### Maryrose143

anong silbi ng true nong sa while mo

#### HikariNoMika

try mo palitan ung jeff += jeff2 mo ng jeff = jeff / jeff2;

#### RikkuRikku

try mo palitan ung jeff += jeff2 mo ng jeff = jeff / jeff2;

#### Maryrose143

ganito gusto mo

pag 10 maximum ayy sampo ang ma.print na name.ng user??

#### RikkuRikku

ganito gusto mo

pag 10 maximum ayy sampo ang ma.print na name.ng user??
nope

like
1
2
5
3

sum is 11
average is 2.75

#### hockers

##### Honorary Poster
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 );``````

#### djdoolky76

##### PHC Master
gagana bayan kahit walang switch variable?

#### hockers

##### Honorary Poster
Try 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');``````

#### RikkuRikku

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 );``````
salamat sir cge try ko ...

#### twojuantwo

##### Leecher
, Last edited:
**code na walang error checking**
Code:
``````        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);``````
**code na may divide by 0 error checking**
Code:
``````        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);
}``````
**code na may input error checker at saka divide by zero checker
Code:
``````        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);
}``````

#### djdoolky76

##### PHC Master
Try 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');``````
daming error paps.. pero konting ayos nalang

#### Maryrose143

#include <iostream>
#include <math.h>

using namespace std;

int main() {

system("cls");

cout<<"[a] Square Root \n Power(x,y) \n[c] Sin \n[e] Cosine \n[x] Exit \nEnter Option :";

case 'a':

{
system("cls");
cout<<" Square Root \n \n";
int num;

cout<<"Enter a Number :";
cin>>num;

cout<<"The Square Root Of " <<num<<" "<<"is "<<sqrt(num)<<"\n";

cout<<"Do You Want To Try Again Press [y,Y] \nany key for not :";
break;
case 'b' :
system("cls");
cout<<" Power \n \n";
int x;
int y;

cout<<"Enter Number of x :";
cin>>x;
cout<<"Enter Number of y :";
cin>>y;

cout<<"The Power Of X and Y is "<<pow(x,y)<< "\n";

cout<<"Do You Want To Try Again Press [y,Y] \nany key for not :";
}
break;
case 'c':
system("cls");
cout<<" Sin \n \n";
int sin_num;
double res;

cout<<"Enter Number :";
cin>>sin_num;

res=sin(sin_num);

cout<<"The Sin Of "<<sin_num<<" "<<"is "<<res<<"\n";

cout<<"Do You Want To Try Again Press [y,Y] \nany key for not :";

}
break;
case 'e':

system("cls");
cout<<" Cosine \n \n";
int cos_num;
double res1;

cout<<"Enter A Number :";
cin>>cos_num;

res1 = cos(cos_num);

cout<<"The Cosine of "<<cos_num<<" "<<"is "<<res1 <<"\n";

cout<<"Do You Want To Try Again Press [y,Y] \nany key for not :";

}
break;

case 'x':

cout<<"Do You Want To Exit?? \npress [X,x] :";

system("pause");

break;
}

}
cout<<"Do You Want To Back To Main Menu Press [y,Y] to go \n[x,X] to Exit :";
}
}

#### Maryrose143

dev++ yan kuha ka lang idea

#### RikkuRikku

salamat po mga sir

#### Arcturus

##### Forum Guru
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));
}
}``````

#### hockers

##### Honorary Poster
, Last edited:
daming error paps.. pero konting ayos nalang
Baka hindi mo nailagay si Scanner, nagbase lang kasi ako sa sinend mo eh
Pero eto boss sure nato triny ko na para sayo.
Code:
``````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');
}
}``````

#### mrHazan

##### Eternal Poster
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));
}
}``````
Pwede pa to maimprove kailangan ng error checking eh. Haha!

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

Members online
1,665
Guests online
5,054
Total visitors
6,719