First program made on my own

+1 Shiloh Cuevas · May 21, 2015
Hey guys so I watched a few of the videos here and have combined what i learned to make this program that finds the average of the numbers you input.  I didnt make it simple because i wanted to include methods and objects but i want your guys opinion!

p.s. i know theres a ton of things i need to do to make this better but this is what i made after one day! and i tried to refer to the videos and google as least as possible


package training;

import java.util.Scanner;

public class First{
public static void main(String args[]){

Scanner input = new Scanner(System.in);
First firstobj = new First();
System.out.println("how many numbers would you like to average");
int amount = input.nextInt();

firstobj.secondstep(amount);
System.out.println("if you have made it this far, the program has worked");

}

public void secondstep(int amount){
int totnum = 0;
int counter = 0;
if(amount == 0){
First firstobj = new First();
firstobj.laststep(totnum,amount);
}
while(counter != amount){
Scanner input = new Scanner(System.in);
System.out.println("please enter next number");
int entry = input.nextInt();
totnum = totnum + entry;
counter = counter + 1;
}
First firstobj = new First();
firstobj.laststep(totnum,amount);
}

public void laststep(int totnum, int amount){
if(totnum == 0){
System.out.println("the answer is simply 0...");
}else{

System.out.println("the average of all your numbers is");
int answer = totnum/amount;
String aString = Integer.toString(answer);
System.out.println(answer);

}
}
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Jonas Meise · May 21, 2015
import java.util.Scanner;

public class First{

public static void main(String args[]){

Scanner input = new Scanner(System.in);
First firstobj = new First();
System.out.println("how many numbers would you like to average");
int amount = input.nextInt();

firstobj.secondstep(amount);
System.out.println("if you have made it this far, the program has worked");

}

public void secondstep(int amount){
int totnum = 0;
int counter = 0;

if(amount == 0){
First firstobj = new First();
firstobj.laststep(totnum,amount); 
}

while(counter != amount){
Scanner input = new Scanner(System.in);
System.out.println("please enter next number");
int entry = input.nextInt();
totnum = totnum + entry;
counter = counter + 1;
}

First firstobj = new First();
firstobj.laststep(totnum,amount);
}

public void laststep(int totnum, int amount){

if(totnum == 0){
System.out.println("the answer is simply 0...");
}

else{
System.out.println("the average of all your numbers is");
int answer = totnum/amount;
String aString = Integer.toString(answer);
System.out.println(answer);
}
}
}

If you format your code more like this you won't get lost that easily when writing more complex programs. Try to format you code in a nice way from beginning so it gets a habit ;)
0 Jonas Meise · May 21, 2015
And when posting code in the forums here you can use the feature above called "code" to let you code show up as white text in that black box.
0 Shiloh Cuevas · May 21, 2015
ok thanks jonas, now i know.  But does anybody have anything to say on the way i did it? Could anybody rewrite it in a skilled or professional way?
+1 Neil Cannon · May 29, 2015
import java.util.Scanner;

public class Average {

    public static void main(String args[]) {
        
        Scanner in = new Scanner(System.in);

        System.out.println("How many numbers would you like to average");

        int n = in.nextInt();
        int[] x = new int[n];

        System.out.println("Please enter your numbers");
            
        int sum = 0;
        
        for (int i = 0; i < n; i++) {

            x[i] = in.nextInt();
        
            sum += x[i];
            
        }
        
        System.out.println("The average of your numbers is " + sum/n );
        
        System.out.println("\nIf you have made it this far, the program has worked!!");
        
        in.close();
    }
}


+1 Neil Cannon · May 29, 2015
Compare your code to what I have placed above. It does the same but is a lot shorter.
0 Ryan Cummings · May 30, 2015

import java.util.Scanner;
import static java.lang.System.out;


public class AveragingPprogram {
public static void main(String args[]){
Scanner MyScan = new Scanner(System.in);
int num , val ,count, total = 0;
double avg;

out.println("Enter the amount of number you want to finD the average of: ");
count = MyScan.nextInt();
out.print("Enter the number you want to find the average of: ");

for( num= 1 ;num
  • 1

Java / Android Development

106,991 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator