Results 1 to 3 of 3
Thread: Calculator help.
- 10-25-2008, 04:35 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 3
- Rep Power
- 0
Calculator help.
ok im supposed to do a calculator and ive red the book over 20-30 times and this just doesnt make sense to me . my calculator isnt supposed to have graphics or nothing just a plain old dos calc. asks you wat operation you want to do. you type it in. the operation comes out nd u input the numbers and it solves it for you. i have the division code and the start of the calc. but idk how to put them together. can someone help. heres my code. for the calc and division.
and heres the division which i got from the bookJava Code:import java.util.Scanner; public class Calc { public static void main(String []args) { //creat a scanner object to read input Scanner keyboard = new Scanner(System.in); int multiply, add, subtract, divide, number ; System.out.println("What operation would you like to do?:"); System.out.println ("multiply"); System.out.println ("add"); System.out.println ("subtract"); System.out.println ("divide"); System.out.println(); multiply = keyboard.nextInt(); add = keyboard.nextInt(); subtract = keyboard.nextInt(); divide = keyboard.nextInt(); } }
Java Code:import java.util.Scanner; //needed for scanner class /** *this program demonstrates the if-else statement */ public class Division { public static void main(String []args) { double number1, number2; // division operands double quotient; // result of division // creat a scanner objet for keyboard input Scanner keyboard = new Scanner(System.in); //get the first number System.out.print("enter a number: "); number1 = keyboard.nextDouble(); //get the 2nd number System.out.print("divided by: "); number2 = keyboard.nextDouble(); if (2 == 0) { System.out.println("division by zero is not possible."); System.out.println("please run the program again and "); System.out.println("enter a number other than zero"); } else { quotient = number1 / number2; System.out.println(number1); System.out.println("divided by " + number2); System.out.println("is " + quotient); } } }
-
Do you know how to create and use methods? and how is your Calc class getting input? how can it tell what operation to perform?
Last edited by Fubarable; 10-25-2008 at 04:46 AM.
- 10-25-2008, 07:42 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, first of all learn about methods in Java, how to define how to handle to have some process how to call methods in different locations and so on.
When you get fair knowledge about methods, you can easily implement all your classes as a method. Actually you can find out what your classes doing, which you have found from the book.
Similar Threads
-
HELP FAST!!----Interest Calculator
By Coop33 in forum New To JavaReplies: 8Last Post: 10-15-2008, 01:44 PM -
Calculator Problem. Thanks for helping! ^^
By clark_sandy in forum New To JavaReplies: 3Last Post: 07-06-2008, 04:01 PM -
Java Calculator
By aapanju in forum New To JavaReplies: 3Last Post: 04-17-2008, 05:33 AM -
calculator not working
By Renegade85 in forum New To JavaReplies: 5Last Post: 03-10-2008, 03:27 PM -
Swing Calculator
By nemo in forum AWT / SwingReplies: 1Last Post: 05-28-2007, 11:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks