2 Replies - 2701 Views - Last Post: 26 October 2009 - 03:14 PM

#1 sweetD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-October 09

Simple Calculator Code in Java - need help please!

Post icon  Posted 18 October 2009 - 09:53 AM

Hello - I'm new to Java and hoping someone can help me with the code I have started. I need to create a simple calculator in Java. Below is what I have so far; it is not working currently and I'm not sure why. I also needed to add the parseInt function somewhere in the code but I'm not sure where. Thanks!

import java.util.Scanner;
public class CalcDemo {

public static void main(String[] args) {
}

private int valueA;
private int valueB;
private String operator;
private char operatorA;

public int getvalueA() {
return valueA;
}

public int getvalueB() {
return valueB;
}

{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter problem.");

{

valueA = keyboard.nextInt();
valueB = keyboard.nextInt();
operator = keyboard.next();
operator1 = operator.charAt(0);

int add = valueA + valueB;
int minus = valueA - valueB;
int multiply = valueA * valueB;
int divide = valueA / valueB;
String clearScreen = null;


switch (operatorA) {
case '+':
System.out.println(add);
break;

case '-':
System.out.println(minus);
break;

case '*':
System.out.println(multiply);
break;

case '/':
System.out.println(divide);

case 'C':
System.out.println(clearScreen);
break;

default:
System.out.println("unknown operator '" + operator + "'. Please try again.");
break;
}

}
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: Simple Calculator Code in Java - need help please!

#2 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: Simple Calculator Code in Java - need help please!

Posted 18 October 2009 - 10:25 AM

There 'is' a Java forum :P
Was This Post Helpful? 0
  • +
  • -

#3 rdutchman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-October 09

Re: Simple Calculator Code in Java - need help please!

Posted 26 October 2009 - 03:14 PM

View PostsweetD, on 18 Oct, 2009 - 08:53 AM, said:

Hello - I'm new to Java and hoping someone can help me with the code I have started. I need to create a simple calculator in Java. Below is what I have so far; it is not working currently and I'm not sure why. I also needed to add the parseInt function somewhere in the code but I'm not sure where. Thanks!

import java.util.Scanner;
public class CalcDemo {

public static void main(String[] args) {
}

private int valueA;
private int valueB;
private String operator;
private char operatorA;

public int getvalueA() {
return valueA;
}

public int getvalueB() {
return valueB;
}

{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter problem.");

{

valueA = keyboard.nextInt();
valueB = keyboard.nextInt();
operator = keyboard.next();
operator1 = operator.charAt(0);

int add = valueA + valueB;
int minus = valueA - valueB;
int multiply = valueA * valueB;
int divide = valueA / valueB;
String clearScreen = null;


switch (operatorA) {
case '+':
System.out.println(add);
break;

case '-':
System.out.println(minus);
break;

case '*':
System.out.println(multiply);
break;

case '/':
System.out.println(divide);

case 'C':
System.out.println(clearScreen);
break;

default:
System.out.println("unknown operator '" + operator + "'. Please try again.");
break;
}

}
}
}



try the Tutorial: A Basic Calculator
http://sumtotalz.com...icCalc_Tut.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1