GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-May-2009, 08:23
BaBy GiRl BaBy GiRl is offline
New Member
 
Join Date: May 2009
Posts: 1
BaBy GiRl is on a distinguished road
Exclamation

Simple calculator using loops


Hello Everybody,


I am a beginner in JAVA and I need help from you all..


How can I write a simple calculator (Sum,Subtract, multiply, and divide)



using either



while loop (much better)


or


switch loop



Thank You
Last edited by LuciWiz : 08-May-2009 at 08:50. Reason: Removed annoying formatting
  #2  
Old 09-May-2009, 10:43
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 347
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Simple calculator using loops


What specifically do you need help with? Do you just need to know how to make the program run inside a loop until the user decides to quit? Do you need help with implementing the operations (addition etc)? Show us the start you have and point out the problem.
  #3  
Old 29-May-2009, 10:44
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Regular Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 342
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Simple calculator using loops


Quote:
Originally Posted by BaBy GiRl
Hello Everybody,


I am a beginner in JAVA and I need help from you all..


How can I write a simple calculator (Sum,Subtract, multiply, and divide)



using either



while loop (much better)


or


switch loop



Thank You

Maybe this start will help get you going?

JAVA Code:
import java.lang.*;
import java.io.*;

public final class SimpleCalculator {
  public static void main(String[] args) {
    BufferedReader bufread = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Welcome to the world's most simple (also cheap) electronic calculator");
    System.out.print("\n\nEnter an integer: ");
    int a = 0;
    try {
      String s = bufread.readLine();
      Integer intVal = new Integer(s);
      a = intVal.intValue();
    } 
    catch(Exception ex) {
      ex.printStackTrace();
      return;
    }
    System.out.print("\n\nEnter the operation (+ - * /): ");
    int operation = 0;
    try {
      operation = System.in.read();
      System.in.skip(System.in.available());
    }
    catch(IOException ioex) {
      ioex.printStackTrace();
    }
    System.out.print("\n\nEnter another integer: ");
    int b = 0;
    try {
      String s = bufread.readLine();
      Integer intVal = new Integer(s);
      b = intVal.intValue();
    }
    catch(Exception ex) {
      ex.printStackTrace();
      return;
    }
    int result = 0;
    switch(operation) {
      case '+':
	  result = a + b;
	break;
      case '-':
	  result = a - b;
	break;
      case '*':
	  result = a * b;
	break;
      case '/':
	  if(b != 0) {
	    result = a/b;
	  }
	break;
      default:
	System.out.println("Not a valid operation...exiting!");
	return;
    }
    System.out.println("The result of " + a + " " + (char)operation + " " + b + " = " + result);
  }
}

	


Output:

Code:
bash-3.2$ java SimpleCalculator Welcome to the world's most simple (also cheap) electronic calculator Enter an integer: 1234 Enter the operation (+ - * /): - Enter another integer: 34 The result of 1234 - 34 = 1200 bash-3.2$ java SimpleCalculator Welcome to the world's most simple (also cheap) electronic calculator Enter an integer: -345 Enter the operation (+ - * /): * Enter another integer: 2 The result of -345 * 2 = -690 bash-3.2$ java SimpleCalculator Welcome to the world's most simple (also cheap) electronic calculator Enter an integer: 45 Enter the operation (+ - * /): / Enter another integer: 9 The result of 45 / 9 = 5 bash-3.2$ java SimpleCalculator Welcome to the world's most simple (also cheap) electronic calculator Enter an integer: 3333 Enter the operation (+ - * /): + Enter another integer: 2222 The result of 3333 + 2222 = 5555


MxB
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple login reveals extra menu/buttons... skuliaxe MySQL / PHP Forum 1 15-Feb-2008 12:44
Please help me to convert a simple C++ program into an object-oriented one. zekesteer C++ Forum 5 05-Jan-2008 10:05
nested loops.. need some help.. PeterPan Java Forum 1 21-Mar-2006 21:40
Help with nested for loops...please. sorry newbie keperry C++ Forum 14 16-Oct-2004 11:25
[PROGRAM] Simple 8-Puzzle Game Max Payne MS Visual C++ / MFC Forum 0 09-May-2004 21:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:13.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.