Sum of numbers


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Sum of numbers

  1. #1
    Join Date
    Dec 2004
    Posts
    46

    Sum of numbers

    Hi,

    First off I am new to java and I am doing some self-study.
    I have wrote a program that asks the user to enter two numbers and displays the sum of these two numbers, it then gives the user a opportunity to have another go or to quit the program:

    public class Numbers
    {
    public static void main(String[] args)
    {
    int num1, num2;
    char choice;
    do
    {
    System.out.print("Enter a number: ");
    num1 = EasyIn.getInt();
    System.out.print("Enter another number: ");
    num2 = EasyIn.getInt();
    System.out.println("The sum of the two numbers is "
    + (num1 + num2));
    System.out.print ("Do you want another go (y/n): ");
    choice = EasyIn.getChar();
    }while(choice == 'y');
    }
    }

    I have a couple of questions:
    • How could I adapt the above program so the user would be given another go irrespective of whether or not an upper case or lower case ‘y’ is entered?
    • How could I make it so instead of displaying the sum of the numbers, it asks the user to enter the sum. Then if it is correct a congratulations message is displayed and if incorrect then the correct answer is displayed.

    Any feedback would be much appreciated.

  2. #2
    Join Date
    Sep 2004
    Posts
    223
    ok for the first question:

    when the user enters in their selection into your "choice" variable, you can use the String method:
    equalsIgnoreCase(String anotherString) this will do what you asked.

    so your while statement would become:

    while(choice.equalsIgnoreCase("y"));


    for your second question, just make another 3 variables called "sum", "guessString" and "guess", then after the second number is inputted, add the 2 numbers together into the "sum" variable.
    Then ask the user for the appropriate input like so:

    System.out.print ("Guess the sum of the 2 numbers: ");
    guessString = EasyIn.getChar();
    guess = Integer.parseInt(guessString);

    then do and if statement on the guess variable to see if it matches the sum variable

    easy huh?
    A kram a day keeps the doctor......guessing

  3. #3
    Join Date
    Dec 2004
    Posts
    46
    Thanks for your help

  4. #4
    Join Date
    Dec 2004
    Posts
    46

    Upper Case, Lower Case

    Hi i have had a couple of problems trying to compile the code that you gave me:

    For the adapting the above program so the user would be given another go irrespective of whether or not an upper case or lower case ‘y’ is entered?:

    public class ArithmeticCase3_1a
    {
    public static void main(String[] agrs)
    {
    int num1, num2;
    char choice;
    do
    {
    System.out.print("Enter a number: ");
    num1 = EasyIn.getInt();
    System.out.print("Enter another number: ");
    num2 = EasyIn.getInt();
    System.out.println("The sum of the two numbers is "
    + (num1 + num2));
    System.out.print ("Do you want another go (y/n): ");
    choice = EasyIn.getChar();
    }while(choice.equalsIgnoreCase("y"));
    }
    }

    when i compile it , its says char cannot be dereferenced.

    Where have i gone wrong?

  5. #5
    Join Date
    Feb 2004
    Posts
    541
    There are two types of variables in java, primitives and objects. Primitives simply represent a structureless value, whereas objects represent something with structure.

    Dereferencing is what you do to objects, to refer to a particular method or variable within them. You can only do this to objects though, as it is referencing the internal structure of the object. It makes no sense to do this to a primitive, becuase it has no internal structure.

    In your code the problem is the variable "choice" is a primitive (char), yet you are attempting to access the method "equalsIgnoreCase()". A char has no methods, so you can't do this.

    When you are comparing primitives you use the == operator. In this case you should either compare the char you have to the char 'y' and the char 'Y' to see if it equals either of them using if(choice == 'y' || choice == 'Y'), or you should create a String object from it, and use the equalsIgnoreCase() method.

  6. #6
    Join Date
    Dec 2004
    Posts
    46
    Thanks for your explanation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center