hi, i have to write a program that calculates and displays numbers btw 1 and 100 incl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • monkey1001
    New Member
    • Dec 2007
    • 10

    hi, i have to write a program that calculates and displays numbers btw 1 and 100 incl

    this is what i got and am stuck...please helpp..thanks

    [CODE=java]import java.util.Rando m; //Needed for Scanner class
    import java.util.*;

    /**
    This program demonstrates value between 1 and 100 inclusive
    */

    public class Value
    {
    public static int getValidValue()

    {
    Scanner keyboard = new Scanner(System. in);

    int inputValue=0;
    System.out.prin t("Enter an integer between" + "1 and 100;inclusive:" );

    inputValue = keyboard.nextln ();

    sum=n(n+1)/2;
    System.out.prin tln(sum);
    System.out.prin tln();
    {
    while (inputValue <1 ||inputValue > 100)
    {
    System.out.prin t("Invaalid value" + "enter a value between 1" + "and 100,inclusive:" );

    inputValue = keyboard.nextIn t();

    }
    return(inputVal ue);
    }
    }
    }[/CODE]
  • dav3
    New Member
    • Nov 2006
    • 94

    #2
    Originally posted by monkey1001
    this is what i got and am stuck...please helpp..thanks

    import java.util.Rando m; //Needed for Scanner class
    import java.util.*;

    /**
    This program demonstrates value between 1 and 100 inclusive
    */

    public class Value
    {
    public static int getValidValue()

    {
    Scanner keyboard = new Scanner(System. in);

    int inputValue=0;
    System.out.prin t("Enter an integer between" + "1 and 100;inclusive:" );

    inputValue = keyboard.nextln ();

    sum=n(n+1)/2;
    System.out.prin tln(sum);
    System.out.prin tln();
    {
    while (inputValue <1 ||inputValue > 100)
    {
    System.out.prin t("Invaalid value" + "enter a value between 1" + "and 100,inclusive:" );

    inputValue = keyboard.nextIn t();

    }
    return(inputVal ue);
    }
    }
    }
    First why is there a "+" in this line?
    System.out.prin t("Enter an integer between" + "1 and 100;inclusive:" );

    second, sum=n(n+1)/2; what is n?

    Comment

    • monkey1001
      New Member
      • Dec 2007
      • 10

      #3
      Originally posted by dav3
      First why is there a "+" in this line?
      System.out.prin t("Enter an integer between" + "1 and 100;inclusive:" );

      second, sum=n(n+1)/2; what is n?
      the "n" stands for user entered value and the "+" represntd "and"

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        The syntax n(n+1)/2 will not compile. You need to use the * operator:

        [CODE=java]sum = (n * (n + 1)) / 2;[/CODE]

        But apart from this, your code is a mess. You never make an n variable, you never get a value for it, sum is initialized, but then printed and not used again. If you get rid of this, you will have exactly what the method describes - you ask for input, get it in the form of an int, and then check if it is between 1 and 100. What is all the rest of that for anyway?

        Comment

        • MarshMallow
          New Member
          • Nov 2007
          • 52

          #5
          rename n with inputvalue;
          move the while loop before the computation(who y should computation start if the input is not correct?);
          change the comment in "computing the sum of first n integer,where n is the userinput"(this is what your formula 'n(n+1)/2' is supposed to do);
          Last edited by MarshMallow; Dec 3 '07, 09:13 AM. Reason: mistyping variable names

          Comment

          Working...