Help With Loops

Thanks for any help provided

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1269 Views - Last Post: 20 March 2009 - 08:02 PM Rate Topic: -----

#1 pmpinaintez11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 20-March 09

Help With Loops

Posted 20 March 2009 - 06:45 PM

Ok So These are the directions for a program I am required to write followed by sample outputs of what it should look like

Write a Java program that will input integer values from the user. The user will enter a sequence of integer values in a loop that will go for maximum 10 iterations (declare a class constant for 10) but it will stop if the user is entering a negative value. The program computes the sum and the average of all positive numbers in the sequence.
HINT: You will need to implement a loop that is both count-controlled (has up to 10 iterations) and event-controlled (stops earlier if a negative value is entered).

OUTPUT (3 SAMPLES):
SAMPLE #1
Enter a number, negative to STOP: 1
Enter a number, negative to STOP: 2
Enter a number, negative to STOP: 3
Enter a number, negative to STOP: 4
Enter a number, negative to STOP: 5
Enter a number, negative to STOP: 6
Enter a number, negative to STOP: -8
===============================
You entered 6 positive numbers.
The sum = 21
The average = 3.50

SAMPLE #2
Enter a number, negative to STOP: -1
ERROR! Division by 0. No input.

SAMPLE #3
Enter a number, negative to STOP: 1
Enter a number, negative to STOP: 2
Enter a number, negative to STOP: 3
Enter a number, negative to STOP: 4
Enter a number, negative to STOP: 5
Enter a number, negative to STOP: 6
Enter a number, negative to STOP: 7
Enter a number, negative to STOP: 8
Enter a number, negative to STOP: 9
Enter a number, negative to STOP: 10
================================
You entered 10 positive numbers.
The sum = 55
The average = 5.50

my code is taking input an 11th time but not calculating it when i run it all the way through without entering a negative number. I want it to take input only 10 times and calculate sum for the 10 integers. Any help is greatly appreciated.


My Code:

import java.util.Scanner;

public class Assignment2_3 {
  public static final int DONE = 10;
   public static void main(String[] args) {
	Scanner input = new Scanner(System.in);
	int n;
	int x;
	int i = 0;
	int sum = 0;
	double avg;
	System.out.print("Enter number, negative to STOP:");
	n = input.nextInt();
	if(n<0){
	  System.out.println("ERROR! Division by 0. No input.");
	}
	else{
	for(x =1; x <DONE; x++){
	  if
		(n>=0){
		sum= sum + n;
		System.out.print("Enter number, negative to STOP:");
		n = input.nextInt();
	  }
	}
	avg = (double)sum/x;
	System.out.println("===================================");
	System.out.println("You entered " + x + " Positive integers");
	System.out.println("The sum = " + sum);
	System.out.printf("The average = %3.2f\n", avg);
	}
   }
}

This post has been edited by pmpinaintez11: 20 March 2009 - 07:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help With Loops

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Help With Loops

Posted 20 March 2009 - 07:09 PM

Well, I don't fully understand the question, but I can see an error.

In your while loop, take out the x=x+1; statement. The for loop will automatically do that.

Edit: I found it. Since you already input a number before your for loop, you need to start the loop at 1 using the following...
for (int x = 1; x < DONE; x++)

This post has been edited by Locke: 20 March 2009 - 07:12 PM

Was This Post Helpful? 0
  • +
  • -

#3 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: Help With Loops

Posted 20 March 2009 - 07:11 PM

Welcome at DIC
Please do not put "fancy" font in your post
you loop (in a for loop) DONE times or 10
So why do you prompt for a negative number ?
Your code should rather look:

int sum = 0;
int nbEntered = 0;
for(;;)  {			   // loop until break
   int number = input.getInt();
   if(number < 0)
	  break;		  // exit loop if number inputed < 0
   sum = sum + number;
   nbEntered++;
}

// avoid / 0
if(nbEntered > 0) {
   double avg = sum / nbEntered;
   System.out.println("Numbers entered: " + nbEntered + "Total: " + sum + " Average: " + avg);
}


Was This Post Helpful? 0
  • +
  • -

#4 pmpinaintez11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 20-March 09

Re: Help With Loops

Posted 20 March 2009 - 07:12 PM

yeah i just saw that. i took that out
Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Help With Loops

Posted 20 March 2009 - 07:16 PM

I found out why it's going to 11 inputs...see my above post.

If you want it to go infinitely (until a negative number is detected), use pbl's method.

If you just want 10 inputs, use mine.

If you want to go to 10 maximum, but let them input as many as they want before that...use a combination of both of mine and pbl's solutions.
Was This Post Helpful? 0
  • +
  • -

#6 pmpinaintez11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 20-March 09

Re: Help With Loops

Posted 20 March 2009 - 07:17 PM

nvm

This post has been edited by pmpinaintez11: 20 March 2009 - 07:17 PM

Was This Post Helpful? 0
  • +
  • -

#7 pmpinaintez11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 20-March 09

Re: Help With Loops

Posted 20 March 2009 - 07:25 PM

View PostLocke, on 20 Mar, 2009 - 06:09 PM, said:

Well, I don't fully understand the question, but I can see an error.

In your while loop, take out the x=x+1; statement. The for loop will automatically do that.

Edit: I found it. Since you already input a number before your for loop, you need to start the loop at 1 using the following...
for (int x = 1; x < DONE; x++)



thanks that helped. Only problem I have now is that the sum is saying that it is 45 not 55 as it should be with 10 integers

i edited my code to be what i currently have
Was This Post Helpful? 0
  • +
  • -

#8 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: Help With Loops

Posted 20 March 2009 - 07:28 PM

ok try this bit. Instead of sum = sum + n, try sum += n. That will set sum equal to the sum of every value entered for n. Let me know if that helped.

Edit: also to make more programmatic sense, I would move your sum statement to below the request for input. i.e.

System.out.println("Please enter the next number, or a negative number to stop");
n = input.nextInt();
sum +=n;

This post has been edited by webmin: 20 March 2009 - 07:33 PM

Was This Post Helpful? 0
  • +
  • -

#9 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Help With Loops

Posted 20 March 2009 - 07:29 PM

View Postwebmin, on 20 Mar, 2009 - 08:28 PM, said:

ok try this bit. Instead of sum = sum + n, try sum += n. That will set sum equal to the sum of every value entered for n. Let me know if that helped


No, the way pmpinaintez11 does it is the same thing, yours is just a shortcut notation.

-------------

Aha, I got it.

You need to add the number right after you input the first time.

import java.util.Scanner;

public class Assignment2_3 {
  public static final int DONE = 10;
   public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int n;
    int x;
    int i = 1;
    int sum = 0;
    double avg;
    System.out.print("Enter number, negative to STOP:");
    n = input.nextInt();
    sum +=n; // ***************** ADD THIS **********************
    if(n<0){
      System.out.println("ERROR! Division by 0. No input.");
    }
    else{
    for(x =1; x <DONE; x++){
      if(n>=0){
        sum= sum + n;
        System.out.print("Enter number, negative to STOP:");
        n = input.nextInt();
        i = i+1;
       
      }
    }
    avg = (double)sum/i;
    System.out.println("===================================");
    System.out.println("You entered " + i + " Positive integers");
    System.out.println("The sum = " + sum);
    System.out.printf("The average = %3.2f\n", avg);
    }
   }
}


It still won't add correctly unless you use all 10 numbers. Edit it to look like this.

import java.util.Scanner;

public class Assignment2_3
{
    public static final int DONE = 10;

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        
        int n = 0, i = 0, sum = 0;
        double avg = 0;

        for (int x = 0; x < DONE; x++)
        {
            System.out.print("Enter a number, negative to stop: ");

            n = input.nextInt();

            if (n < 0)
                break;

            else
            {
                sum += n;
                i++;
            }
        }

        if (i == 0)
        {
            System.out.println("ERROR!  Division by 0.  No input.");
            System.exit(0);
        }

        avg = (double) sum /  (double) i;

        System.out.println("===================================");
        System.out.println("You entered " + i + " Positive integers");
        System.out.println("The sum = " + sum);
        System.out.printf("The average = %3.2f\n", avg);
    }
}

This post has been edited by Locke: 20 March 2009 - 07:46 PM

Was This Post Helpful? 0
  • +
  • -

#10 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: Help With Loops

Posted 20 March 2009 - 07:33 PM

[quote name='pmpinaintez11' date='20 Mar, 2009 - 06:25 PM' post='580189']
thanks that helped. Only problem I have now is that the sum is saying that it is 45 not 55 as it should be with 10 integers
/quote]
If you want 10 integers why do you prompt "enter negative number to stop" ?
Was This Post Helpful? 0
  • +
  • -

#11 pmpinaintez11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 20-March 09

Re: Help With Loops

Posted 20 March 2009 - 07:39 PM

View PostLocke, on 20 Mar, 2009 - 06:29 PM, said:

View Postwebmin, on 20 Mar, 2009 - 08:28 PM, said:

ok try this bit. Instead of sum = sum + n, try sum += n. That will set sum equal to the sum of every value entered for n. Let me know if that helped


No, the way pmpinaintez11 does it is the same thing, yours is just a shortcut notation.

-------------

Aha, I got it.

You need to add the number right after you input the first time.

import java.util.Scanner;

public class Assignment2_3 {
  public static final int DONE = 10;
   public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int n;
    int x;
    int i = 1;
    int sum = 0;
    double avg;
    System.out.print("Enter number, negative to STOP:");
    n = input.nextInt();
    sum +=n; // ***************** ADD THIS **********************
    if(n<0){
      System.out.println("ERROR! Division by 0. No input.");
    }
    else{
    for(x =1; x <DONE; x++){
      if(n>=0){
        sum= sum + n;
        System.out.print("Enter number, negative to STOP:");
        n = input.nextInt();
        i = i+1;
       
      }
    }
    avg = (double)sum/i;
    System.out.println("===================================");
    System.out.println("You entered " + i + " Positive integers");
    System.out.println("The sum = " + sum);
    System.out.printf("The average = %3.2f\n", avg);
    }
   }
}


That gave me 46 not 55

[quote name='pbl' date='20 Mar, 2009 - 06:33 PM' post='580199']

View Postpmpinaintez11, on 20 Mar, 2009 - 06:25 PM, said:

thanks that helped. Only problem I have now is that the sum is saying that it is 45 not 55 as it should be with 10 integers
/quote]
If you want 10 integers why do you prompt "enter negative number to stop" ?

The professor want's it to be so that the user can stop before 10 integers but if they keep going it stops at 10 integers
Was This Post Helpful? 0
  • +
  • -

#12 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Help With Loops

Posted 20 March 2009 - 07:47 PM

I edited my previous post...but I'll repost the code that will work.

import java.util.Scanner;

public class Assignment2_3
{
    public static final int DONE = 10;

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        
        int n = 0, i = 0, sum = 0;
        double avg = 0;

        for (int x = 0; x < DONE; x++)
        {
            System.out.print("Enter a number, negative to stop: ");

            n = input.nextInt();

            if (n < 0)
                break;

            else
            {
                sum += n;
                i++;
            }
        }

        if (i == 0)
        {
            System.out.println("ERROR!  Division by 0.  No input.");
            System.exit(0);
        }

        avg = (double) sum /  (double) i;

        System.out.println("===================================");
        System.out.println("You entered " + i + " Positive integers");
        System.out.println("The sum = " + sum);
        System.out.printf("The average = %3.2f\n", avg);
    }
}


That works just the way you want it to. :) I styled it a bit differently, since I typed it up using your code just as a template.

This post has been edited by Locke: 20 March 2009 - 07:49 PM

Was This Post Helpful? 0
  • +
  • -

#13 pmpinaintez11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 20-March 09

Re: Help With Loops

Posted 20 March 2009 - 07:55 PM

thank you very much. I was just wondering. What does the break statement do? and is there any substitute for that
Was This Post Helpful? 0
  • +
  • -

#14 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Help With Loops

Posted 20 March 2009 - 07:58 PM

You would have to play around a bit and reorder a few things but, yeah, you can take out the break.

What the break does is it breaks the loop that it's enclosed in. It can be used to exit many different structures, but in this case, it breaks the for loop if the number entered is less than zero.

This post has been edited by Locke: 20 March 2009 - 07:58 PM

Was This Post Helpful? 0
  • +
  • -

#15 pmpinaintez11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 20-March 09

Re: Help With Loops

Posted 20 March 2009 - 08:01 PM

thanks. do you guys have like a props or rep thing. You helped a lot
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2