Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need help with a simple calculator (Yes I know i'm a complete and total novice)

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with a simple calculator (Yes I know i'm a complete and total novice)

    import java.util.Scanner;
    public class startingpoint {
    public static void main (String args[]){
    	Scanner sc = new Scanner (System.in);
    	double type,fnum, snum, answer;
     
    	System.out.println("Enter one of three calculator modes, 1 (Multiplication),2 (Division), 3 (Addition). or 4(Subtraction)");
     
    	type = sc.nextDouble();
     
    	System.out.println("Please enter your first number now");
     
    	fnum = sc.nextDouble();
     
    	System.out.println("Please enter your second number now");
     
    	snum = sc.nextDouble();
     
     
    	if (type == 1){
    		answer = fnum * snum;
    	} else if (type == 2){
    		answer = fnum / snum;
    	}else if (type == 3){
    		answer = fnum + snum;
    	}else if (type == 4){
    		answer = fnum + snum;
    	}
     
     
    	System.out.println("Your answer is"); 
    	System.out.println(answer);
    }
    }
    When I run it it says the local variable answer may not have been initialized but when I made the if-then-else statement I though that would have initialized it

    Never mind I found out the problem thanks to Cornix, I forgot to add the else
    Last edited by jmosher65; June 16th, 2014 at 04:31 PM. Reason: Found out the problem


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Need help with a simple calculator (Yes I know i'm a complete and total novice)

    Imagine what would happen when type equals 5.
    What is your program supposed to print?

  3. The Following User Says Thank You to Cornix For This Useful Post:

    jmosher65 (June 16th, 2014)

  4. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with a simple calculator (Yes I know i'm a complete and total novice)

    Ahh... Thanks I got it to work now by adding an else in there, can't believe I overlooked something so simple

Similar Threads

  1. Simple Calculator HELP
    By krowley in forum What's Wrong With My Code?
    Replies: 18
    Last Post: April 20th, 2014, 08:55 PM
  2. Balloon Applet Help [Simple for non-novice]
    By GMPoison in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 24th, 2013, 10:36 AM
  3. Help with simple calculator
    By GMPoison in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 22nd, 2013, 01:29 PM
  4. HI WORLD! Can anyone point a complete novice in the right direction??
    By Irish-novice in forum Member Introductions
    Replies: 4
    Last Post: June 26th, 2012, 05:16 PM
  5. [SOLVED] (Total begginner) simple GUI program nt displayin
    By moneyman021 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 10th, 2012, 06:26 PM