Results 1 to 8 of 8

Thread: Java. help

  1. #1
    Elite Member
    Join Date
    May 2011
    Gender
    Male
    Posts
    1,465

    Default Java. help


    Example.
    Input number of array: 3
    input array 0 = 5
    input array 1 = -5
    input array 2 = -5

    Output:
    3.00 //kai 5+4+3+2+1=15/5= 3.00
    -2.00 //kai -5 + -4 + -3 + -2 + -1 + 0 + 1 = 14/7 = -2.00
    -2.00 //kai -5 + -4 + -3 + -2 + -1 + 0 + 1 = 14/7 = -2.00

    ang pangitaon kai average nila from 1.
    heres my codes. help me. tnx.(sorry wrong spelling ang variable na divide sa toDevide wahahaha)
    Code:
     
    import java.util.*;
    public class TestProgram {
        
        private int input,minus,c=1;
        private int []array;
        private static Scanner in = new Scanner(System.in);
        private double averageFromOne,sum,toDevide=0;
        
        public TestProgram(){
            askNumberOfInput();
            Process();
            theOutput();
        }
        public void askNumberOfInput(){
            System.out.println("Numer of inputs:" );
            setInput(Integer.parseInt(in.nextLine()));
            setIntArray(new int [getInput()]);
        }
        public void Process(){
            for(int x=0; x<array.length;x++){
                System.out.println("Input a number in array "+x+": ");
                setInput(Integer.parseInt(in.nextLine()));
                array[x] = getInput();
            }
        }
        
        public void theOutput(){
            System.out.println("Output");
            for(int x=0; x<array.length; x++){
                if(array[x]<=100&array[x]>0){
                    this.sum = array[x];
                    for(int y=1; y<=array[x];y++){
                        this.toDevide++;
                        this.sum += array[x]-y; 
                        if(y==array[x]){
                            System.out.println(sum/toDevide);
                            this.sum =0;
                            this.toDevide=0;
                        }
                    }
                }else if(array[x]>=-100&array[x]<0){
                    this.sum = array[x];
                    for(int y=0; y>=array[x];y--){
                        //System.out.println("debug"+y);
                        this.toDevide++;
                       this.minus++;
                        
                        if(array[x]<=0){
                            
                            this.sum+=array[x]+c;
                            //System.out.println("asd"+sum);
                            c++;
                        }
                        if(y==array[x]){
                           
                           // System.out.println("devide "+toDevide);
                           // System.out.println("sum "+sum);
                            System.out.println(sum/(toDevide+1));
                            this.sum =0;         //<<<run the program w/ the example input above 
                            this.toDevide=0;    //and see what happens, di na mao ang 3rd output.
                        }                             //bsag ang sum kai back to zero na, kanang shaded red
                    }
                    
                }
                
            }
            
        }
        
        public void setInput(int input){
            this.input = input;
        }
        public int getInput(){
            return input;
        }
        public void setIntArray(int [] array){
            this.array = array;
        }
        private int [] getIntArray(){
            return array;
        }
        public static void main(String args[]){
            TestProgram tp = new TestProgram();
        }
        
    }
    Last edited by jairoh_; 02-18-2012 at 07:37 PM.

  2. #2
    Elite Member
    Join Date
    May 2011
    Gender
    Male
    Posts
    1,465

    Default Re: Java. help

    up for this day. tabang mo bro stealtghost, jinfreecs hahahaha

  3. #3

    Default Re: Java. help

    unsa to imu need bro? ang average sa gi-input nga numbers?

  4. #4
    Elite Member
    Join Date
    May 2011
    Gender
    Male
    Posts
    1,465

    Default Re: Java. help

    Quote Originally Posted by stealthghost View Post
    unsa to imu need bro? ang average sa gi-input nga numbers?
    oh. average from 1 to the number you input. example input is -5, then -5 + -4 + -3 + -2 + -1 + 0 + 1 =-14 / 7 = -2.00. sakto naman akong program pero kung mu input kog -5 and another -5, ang first -5 kai sakto ang answer pero ang 2nd -5 kai inaaccurate na. try the example input above bro. tnx

  5. #5

  6. #6

    Default Re: Java. help

    Sorry, wa gihapon ko ka-gets sa imu goal while comparing sa imu code.

    Na-busy pud ko wholeday..
    check pa nako imu codes.

  7. #7

    Default Re: Java. help

    Medyo kulangan pa ka'g practice sa pag code bro.. but keep it up..

    Code:
    import java.util.*;
    public class TestProgram {
        
        private int input,c=1;
        private int[] array;
        private static Scanner in = new Scanner(System.in);
        private double averageFromOne,sum,toDevide=0;
        
        public TestProgram(){
            askNumberOfInput();
            Process();
            theOutput();
        }
        public void askNumberOfInput(){
            System.out.println("Numer of inputs:" );
            setInput(Integer.parseInt(in.nextLine()));
            setIntArray(new int [getInput()]);
        }
        public void Process(){
            for(int x=0; x<array.length;x++){
                System.out.println("Input a number in array "+x+": ");
                setInput(Integer.parseInt(in.nextLine()));
                array[x] = getInput();
            }
        }
        
        public void theOutput(){
            System.out.println("Output");
            for(int x=0; x<array.length; x++){
                if(array[x]<=100 && array[x]>0){
                    sum = array[x];
                    toDevide=0;
                    for(int y=1; y<=array[x];y++){
                        toDevide++;
                        sum += array[x]-y; 
                        if(array[x]==y){
                            System.out.println(sum/toDevide);
                        }
                    }
                }else if(array[x]>=-100 && array[x]<0){
                    sum = array[x];
                    toDevide=0;
                    c=1;
                    for(int y=0; y>=array[x];y--){
                        toDevide++;
                        if(array[x]<=0){
                            sum+=array[x]+c;
                            c++;
                        }
                        if(array[x]==y){
                            toDevide++;
                            System.out.println(sum/(toDevide));
                        }
                    }
                    
                }
                
            }
            
        }
        public void setInput(int input){
            this.input = input;
        }
        public int getInput(){
            return input;
        }
        public void setIntArray(int[] array){
            this.array = array;
        }
        private int[] getIntArray(){
            return array;
        }
        public static void main(String args[]){
            TestProgram tp = new TestProgram();
        }
    }
    Gamay ra akong gi-usab ani.

    ang variable "c" with initial value "1" wala nimo gi-reset to 1.
    mao maguba ang counting.

  8. #8
    Elite Member
    Join Date
    May 2011
    Gender
    Male
    Posts
    1,465

    Default Re: Java. help

    sakto diay bro stealthghost. wahahaha wa ko kabantay. tnx diay. problem solved.

  9.    Advertisement

Similar Threads

 
  1. JAVA. help in data structures(algorithm)
    By jairoh_ in forum Programming
    Replies: 22
    Last Post: 03-21-2012, 10:31 PM
  2. loop in java. help
    By jairoh_ in forum Programming
    Replies: 15
    Last Post: 02-14-2012, 08:21 PM
  3. user defined class in java [help]
    By xianz87 in forum Programming
    Replies: 1
    Last Post: 07-06-2010, 07:53 PM
  4. Java programming help pls...
    By mr_gwen in forum Programming
    Replies: 6
    Last Post: 11-03-2006, 12:30 AM
  5. nid help..java programming
    By ronninalpha in forum Programming
    Replies: 3
    Last Post: 09-13-2006, 07:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top