6 Replies - 502 Views - Last Post: 08 October 2012 - 01:56 PM Rate Topic: -----

#1 Biscuit Tickler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-October 12

Printing out numbers that were looped.

Posted 05 October 2012 - 08:21 PM

I am supposed to make a program that lets the user input a small number and a large number and lists the numbers in between and then finds the sum and average. For example you would input 5 and 8. It should print out 5,6,7,8 and then on a new line it should say the sum of 5,6,7,8, is 26. On another line it should say "The average of 5,6,7,8, is 6.5. I have no problem with the loop, that works fine but how would I go about printing the numbers that are looped like the 5-8.
import java.io.*;
import java.util.*;
public class SummingNumbers
{
    public static void main (String[] args)
    {
        int sum = 0;
        Scanner value = new Scanner(System.in);
        System.out.print("Enter starting value: ");
        int x = value.nextInt();
        System.out.print("Enter ending value: ");
        int y = value.nextInt();
       
        for (; x<=y; x++)  //explain how this is working with just ; and no variable/number before it.
        {
            
            System.out.println(x);
            sum += x;          
           
        }
 
        System.out.println();
        
    }  
}



I had something in the System.out.println(); but deleted the variable and forgot to delete the line. Disregard that.

Is This A Good Question/Topic? 0
  • +

Replies To: Printing out numbers that were looped.

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Printing out numbers that were looped.

Posted 05 October 2012 - 08:29 PM

You do not actually want to use x in your for loop.
Since x is your min value you don't want to change it. In your for loop you are increasing x.

Anytime you want to print the numbers from x to y just create a loop.
Create a new int (EX: int i = x) in the for loop declaration and set is to x. Loop until Y. Then print out that variable in the for loop.

Now for the reason your for loop is working is because the first thing you set in a for loop is if you want to create a variable specifically for the for loop. Since you are using a pre-existing variable you do not have to have that first declaration.
Was This Post Helpful? 0
  • +
  • -

#3 Biscuit Tickler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-October 12

Re: Printing out numbers that were looped.

Posted 05 October 2012 - 09:03 PM

View PostKinaces, on 05 October 2012 - 08:29 PM, said:

You do not actually want to use x in your for loop.
Since x is your min value you don't want to change it. In your for loop you are increasing x.

Anytime you want to print the numbers from x to y just create a loop.
Create a new int (EX: int i = x) in the for loop declaration and set is to x. Loop until Y. Then print out that variable in the for loop.

Now for the reason your for loop is working is because the first thing you set in a for loop is if you want to create a variable specifically for the for loop. Since you are using a pre-existing variable you do not have to have that first declaration.


Ok, I fixed the for loop and now I need to figure out how to print the numbers that were looped.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Printing out numbers that were looped.

Posted 06 October 2012 - 02:16 AM

Show your updated code.

What don't you understand about printing? A good start would to add a print statement to the loop that prints the number each time through the loop. From there, it's getting the format right.
Was This Post Helpful? 0
  • +
  • -

#5 Biscuit Tickler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-October 12

Re: Printing out numbers that were looped.

Posted 06 October 2012 - 08:43 AM

I had a friend help me. I had to use a string that held all of the numbers in the loop. Here is the code.
import java.io.*;
import java.util.*;
public class SummingNumbers
{
    public static void main (String[] args)
    {
        int totalValue = 0;
        double totalNum = 0;
        String temp = ""; //was forced to use strings because I couldn't think of any other way to do this program.
        Scanner value = new Scanner(System.in);
        System.out.print("Enter starting value: ");
        int x = value.nextInt();
        
        System.out.print("Enter ending value: ");
        int y = value.nextInt();
        System.out.println(" ");
        
        
        for (int i = x; i<=y; i++) //renames x because x is already defined.
        {
           temp +=i + ","; //formats the looped number to have a comma beside of it when calculating the sum below.
           System.out.println(i);
           
           totalValue += i;
           totalNum ++;
           
        }
           
           System.out.println("The sum of " + temp + " is " + totalValue);
           
           double answer = totalValue/totalNum;
           System.out.println("The average of " + temp + " is " + answer);
    }  
}



Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Printing out numbers that were looped.

Posted 08 October 2012 - 12:29 PM

Why not simply

  for (int i = x; i<=y; i++) {
     System.out.print(" " + i);
     totalValue += i;


as far as totalNum is concerned no need to compute it in the loop. It is simply y - x + 1
Was This Post Helpful? 1
  • +
  • -

#7 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Printing out numbers that were looped.

Posted 08 October 2012 - 01:56 PM

Since these are guaranteed to be sequential numbers:

  • Number of integers = max - min + 1
  • Average of integers = (max + min) / 2
  • Sum of all integers = Number of integers * Average


In your case
  • totalNum = y - x + 1;
  • answer = ((double)(y + x)/2);
  • totalValue = totalNum * answer


No need to calculate anything in the loop, just print.

This post has been edited by rfs02: 08 October 2012 - 01:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1