Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Re-writing simple calculator to remove errors.

    Hi guys i'm new to the world of programming and have been watching some videos on youtube by thenewboston to get to know Java. In one of his tutorials he shows you how to make a basic calculator but i wanted to improve it by adding the option to choose which operation you want to perform. However not being familiar with code i cannot think of a way to put it so that it is error free.I do also have a seperate class file which coresponds with the second paragraph.The errors appear in the if statements because of the characters * / - +. I am currently using eclipse to write my code as it is very useful for showing you your mistakes. Thanks in advance. Code:

    Scanner tut7 = new Scanner(System.in);
    double fnum, snum, answer;
    System.out.println("Enter First Number Please:");
    fnum = tut7.nextDouble();
    System.out.println("Enter Second Number Please:");
    snum = tut7.nextDouble();

    Scanner opinput = new Scanner(System.in);
    Operation OperationObject = new Operation();
    System.out.println("Enter What Operation You Want to Perform: ");
    String oper = opinput.nextLine();
    OperationObject.calc(oper);

    if (oper = * ){
    answer = fnum * snum;
    System.out.print("The Answer is: ");
    System.out.println(answer);
    }else{
    if (oper = / ){
    answer = fnum / snum;
    System.out.print("The Answer is: ");
    System.out.println(answer);
    }else{
    if (oper = + ){
    answer = fnum + snum;
    System.out.print("The Answer is: ");
    System.out.println(answer);
    }else{
    if (oper = - ){

    answer = fnum - snum;
    System.out.print("The Answer is: ");
    System.out.println(answer);
    }else{
    System.out.println("Invalid Operation!!!");
    }
    }
    }
    Last edited by najd1103; 08-12-2011 at 02:42 PM. Reason: forgot to add something

  • #2
    New Coder
    Join Date
    Jul 2011
    Location
    London, UK
    Posts
    13
    Thanks
    2
    Thanked 1 Time in 1 Post
    Try posting your question in a Java forum.

  • Users who have thanked acomber for this post:

    najd1103 (08-13-2011)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    najd1103 (08-13-2011)

  • #4
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by najd1103 View Post
    ...i wanted to improve it by adding the option to choose which operation you want to perform....
    You might be able to use JPanel to create a container to store buttons or whatever for the user options.

    But in any case, you'll probably get better help in the Java forum. This is a javascript forum.

    basically, java is to javascript as ham is to hamster.

  • Users who have thanked bullant for this post:

    najd1103 (08-13-2011)

  • #5
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Cheers guys! My mistake.


  •  

    Posting Permissions

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