What's new
  • Welcome to PHCorner Forums. Take a moment to Sign up and gain unlimited access and extra privileges that guests are not entitled to, such as: All that and more! Registration is quick, simple and absolutely free. Join our community today!

Help Pahelp about loops java

RikkuRikku

Honorary Poster
Joined
Apr 19, 2017
Messages
514
Reaction
470
Age
18
, Last edited:
paano mag divide after looping ??

Code:
 

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

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');
 
Comment

RikkuRikku

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

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

Maryrose143

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

using namespace std;

int main() {

char menu, menu1 ='y', menu2='y',menu3= 'y',menu4='y', menu5 ='y',menu6 = 'y';

while(menu1 == 'y' || menu1 == 'Y'){


system("cls");

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



switch(menu) {
case 'a':

while(menu2 == 'y' || menu2 == 'Y')
{
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 :";
cin>>menu2; }
break;
case 'b' :
while(menu3 == 'y' || menu3 == 'Y') {
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 :";
cin>>menu3;
}
break;
case 'c':
while(menu4 == 'y' || menu4 == 'Y') {
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 :";
cin>>menu4;

}
break;
case 'e':

while(menu5 == 'y' || menu5 == 'Y') {
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 :";
cin>>menu5;


}
break;

case 'x':
while(menu6 == 'y' || menu6 == 'Y'){

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

system("pause");

break;
}

}
cout<<"Do You Want To Back To Main Menu Press [y,Y] to go \n[x,X] to Exit :";
cin>>menu1;
}
}
 
Comment
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));
     }
}
 
Comment

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

mrHazan

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

Online statistics

Members online
1,559
Guests online
900
Total visitors
2,459

Forum statistics

Threads
811,483
Messages
16,052,201
Members
1,435,868
Top