Saturday 15 May 2010

java - Method Miscalculation -


I have trouble understanding a calculation within my code I have made an additional method in this mini calculator that I I am doing It is completely to practice methods. I have created the first method for this reason, and for some reason, when I choose to add two numbers together and the program goes to the method and gives an answer, in addition there is a multiplication instead.

The code is down for the whole program:

  import java.util.Scanner; Public Class Test {/ ** * @ Ultimate Argix * / Public Stable Zero Main (String [] Args) {Scanner userReader = New scanner (System.in); Int number 1; Int number 2; Difference decision; Int results = 0; System.out.println ("Please input your first number"); Number1 = user reader.xit (); System.out.println ("Please input your second number"); Number2 = userReader.nextInt (); System.out.println ("Please choose what you want to do"); Println ("1.Addition"); Println ("2.Subtraction"); Println ("3.Division"); Println ("4.Multiplication"); Decision = userReader.nextInt (); Switch (decision) {case 1: result = sum (number 1, number 2); Case 2: Results = Number 1 - Number 2; Case 3: Results = Number 1 / Number 2; Case 4: Results = Number 1 * Number 2; } System.out.println ("Your answer is" + result); } Public static integration (int number 1, int no 2) {int Additionresult; Fusion number = number 1 + number 2; Return Additionresult; }}   

Best regards

In addition, this is good Practice adding a default so that the user can enter a number other than 1, 2, 3, or 4, then you can tell the user that he has entered an invalid option.

Example:

Switch (Decision) {Case 1: ....; break; Case 2: ....; break; Case 3: ....; break; Case 4: ....; break; Default: system.out.println ("invalid option"); break; }

No comments:

Post a Comment