Results 1 to 4 of 4
  1. #1
    ericj5555 is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Problem with program not outputting properly

    Hey guys, sorry if I submitted this improperly. I want to output only 1 of the selected answers( sum OR diff OR product OR ratio) yet the program is just ending or not working properly when I compile and run it. Beginner java-user here, so bare with me.

    Java Code:
    import java.util.Scanner;
    public class TwoNumberCalculator
    {
        public static void main(String []a)
        {
            System.out.println("Please input two numbers: ");
            Scanner kbd = new Scanner(System.in);
        	double num1 = kbd.nextDouble();
      	    double num2 = kbd.nextDouble();
    	    System.out.println("Please input the operation. Valid choices are:");
            System.out.println("plus/add/sum, subtract/minus/difference, ratio/divide, product/multiply: ");
            String ans;
            ans = kbd.next();
            if (ans.equalsIgnoreCase("plus" +"add" +"sum")) 
            {
               System.out.print("The summation of " +num1+ " and " +num2+ " is ");
               System.out.println(num1+num2);
            }
            else if (ans.equalsIgnoreCase("subtract" +"minus" +"difference"))
            {
               System.out.println("The absolute difference of " +num1+ " and " +num2+ " is " +Math.abs(num1-num2));
            }
            else if (ans.equalsIgnoreCase("product" +"multiply"))
            {
               System.out.println("The product of " +num1+ " and " +num2+ " is " +(num1*num2));
            }
            else if (ans.equalsIgnoreCase("ratio" +"divide"))
            {
                System.out.println("The ratio of " +num1+ " and " +num2+ " is " +(num1/num2));
            }
            else 
            {
                System.out.println("Erroneous answer. Program will now exit.");
            }
        }
    }

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default Re: Problem with program not outputting properly

    You are not declaring your IF statements correctly. I put "plusaddsum" into the dialog box and it worked because your if statement is adding the "plus" "add" and "sum" strings together to say IF it equals "plusaddsum". Hope that helps a little.
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    ericj5555 is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: Problem with program not outputting properly

    Thanks a ton Josh, I just had to make separate else if statements for the respective operations. Ya saved me a ton of grief. One last question, I need to make it so when the user chooses ratio or divide, it divides the larger number by the smaller number each time. I've been trying different statements but can't seem to get any to work, any suggestions?

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default Re: Problem with program not outputting properly

    Have you tried an if statement that says something like:

    Java Code:
    if(num1 >= num2)
    	System.out.print(num1/num2);
    else
    	System.out.print(num2/num1);
    It's just a simple comparison since there should only be two different outcomes for your code, either num1 is bigger else it isn't. Should work for you!
    Sincerely, Joshua Green
    Please REP if I help :)

Similar Threads

  1. My Checkbox program is not working properly
    By newbie106 in forum New To Java
    Replies: 8
    Last Post: 08-17-2012, 09:44 PM
  2. Help With Outputting Data To A File
    By Prodigial Mouse in forum New To Java
    Replies: 14
    Last Post: 01-24-2011, 08:01 PM
  3. This program compiles but doesnt run properly!
    By ErikD99 in forum New To Java
    Replies: 5
    Last Post: 12-03-2010, 08:44 PM
  4. Outputting a * triangle
    By blackbeltsas in forum New To Java
    Replies: 1
    Last Post: 10-17-2010, 11:39 AM
  5. Simple Addition Program Outputting Wrong Value
    By carlodelmundo in forum New To Java
    Replies: 4
    Last Post: 08-05-2008, 03:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •