Jump to content

Java calculator

- - - - -

  • Please log in to reply
7 replies to this topic

#1
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts
Hello guys I'm learning Java bymyself and here I am trying to make a calculator but for some reason I'm getting weird values can someone revise me.
By the way I'm using my C++ knowledge as reference I don't know if it helps with java since some things are different.

import java.util.Scanner;

public class apples {

	public static void main (String args[]){

		

		Scanner input = new Scanner (System.in);

		int select = 0;

		double num1=0, num2=0 , result=0;

		num1 = input.nextDouble();

		num2 = input.nextDouble();

		result = input.nextDouble();

		System.out.print("Num1 :"); System.out.print(num1); System.out.print(" Num2: "); System.out.println(num2);

			

		

		System.out.print("1. Sum, 2. Subtract, 3. Multiply , 4. Divide :");

		select = input.nextInt();

		

		System.out.print(select);

		

		if ( select ==1)

		{	result = num1 + num2;	

				System.out.println(result);}

		

		else if ( select ==2)

		{	result = num1 - num2;

				System.out.println(result);	}

		

		else if (select == 3)

		{result = num1 * num2;

			System.out.println(result);}

		

		else if (select == 4)

		{	result = num1 / num2;	

				System.out.println(result);}

		

					else {System.out.println("Invalid");}

		

	}


}



#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
System.out.print(select); // possible problem

System.out.print[COLOR="#008000"]ln[/COLOR](select); // possible solution

To make things easier next time, you could post the input/output also. :D

#3
common_man

common_man

    Newbie

  • Members
  • Pip
  • 8 posts
I agree.. replacing "print" with "println" will solve the display problem (that is the reason you are seeing wierd values - the actual values calculated are correct).
One more thing - I am not sure why you are taking three inputs?? You can probably comment out the result input as it is calculated and displayed later on.

num1 = input.nextDouble();

num2 = input.nextDouble();

//result = input.nextDouble();



#4
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
System.out.print("Num1 :"); System.out.print(num1); System.out.print(" Num2: "); System.out.println(num2);

this can be written:

System.out.println("Num1 :"+num1+" Num2: "+num2);


#5
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Quote

it is too hard for me
It shouldn't be. If you feel you need help, be sure to start a new thread stating your problem/question.

#6
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

lethalwire said:

It shouldn't be. If you feel you need help, be sure to start a new thread stating your problem/question.

I just started IB CS today and I'm gonna need all of the help I can. ( I know this is not a HW help forum but if I need help with problems that are complex to me can I ask?)
I know my c++ but they are using java so I gotta learn allot in a short time

#7
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Quote

I just started IB CS today and I'm gonna need all of the help I can. ( I know this is not a HW help forum but if I need help with problems that are complex to me can I ask?)
I know my c++ but they are using java so I gotta learn allot in a short time
The two languages, c++ and java, have a lot of similar syntax. You shouldn't have too much trouble.

#8
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

lethalwire said:

The two languages, c++ and java, have a lot of similar syntax. You shouldn't have too much trouble.

All right, I will do my best to catch with the class and keep the forum updated with my progress




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users