| | |
simple java program
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I want first to input the first number then the second number. After that input sub or add. If sub, 1st num-2nd num. If add, 1st num+second num. Then print the sum/difference. I started a code that will input 2 numbers then add. Please revise this one i can't revise it. Thank you!
Java Syntax (Toggle Plain Text)
import java.io.*; public class reviewExer1 { public static int addNum(int xVar, int yVar){ int result = 0; result = xVar + yVar; return result; } public static void main (String[]args){ int xVal = 0; int yVal = 0; BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); String xinput = "0"; String yinput = "0"; System.out.print("Input x"); try { xinput = dataIn.readLine(); } catch (IOException e) { System.out.println("Error!"); } xVal = Integer.parseInt(xinput); System.out.print("Input y:"); try { yinput = dataIn.readLine(); } catch (IOException e) { System.out.println("Error!"); } yVal = Integer.parseInt(yinput); int resultOutput = 0; resultOutput = addNum(xVal,yVal); System.out.print("The sum is " + resultOutput); }}
Last edited by John A; Oct 3rd, 2009 at 5:42 pm. Reason: added code tags
You need to take a third input, right? So start with that, then maybe you can figure out how to continue.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
An if statement? Using the equals method of String?
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
No. I am not going to do your homework for you. You already read lines from the user in that code and I can guarantee you've already used an if statement at somepoint in your course. You can at least make an attempt at doing your own hoemwork. Then again, I can pretty much guarantee that this isn't your code either, and so you are lost in the woods. Well, you've been given enough hints to at least be able to find the edges of those woods on your own, and you are going to have to do that before I give you anything more.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
- exe java program by html link? (Java)
- simple coin program (Java)
- Help, I don't think I'm asking for much. This is a simple java program. (Java)
- Need help with a very simple Java program (Java)
- Running Java Program outside an IDE (Java)
- java program to calculate simple interest (Java)
- Help with simple Java IO program running under cygwin (Java)
Other Threads in the Java Forum
- Previous Thread: Encrypting a flv video
- Next Thread: Date formatting
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream






