Sum of numbers

• 11-18-2004, 01:09 PM
WillisTi
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.
• 11-18-2004, 05:42 PM
Kram
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?
• 11-19-2004, 03:39 AM
WillisTi
Thanks for your help
• 11-20-2004, 07:45 AM
WillisTi
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?
• 11-22-2004, 12:02 PM
mikeBarr81
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.
• 11-22-2004, 12:47 PM
WillisTi
Thanks for your explanation