I am trying to create a simple calculator program where a user enters two values, by adding method () Operation is called the class and values are added and the result is displayed. And then I am using the loop while doing the do, in which the user records the values that are added to the previous total and the result is displayed. It must continue to run until the user enters some input which is not double type
import java.util.Scanner; Public class app {public static zero main (string [] args) {scanner input = new scanner (System.in); System.out.println ("Enter first number:"); Double number = input.nextDouble (); System.out.println ("Enter second number:"); Double No. 2 = Input NXD double (); Double total = operation.ed (number, number 2); Println (total); System.out.println ("Enter number again :); number 2 = input. New double (); do {total = operations.ed (total, number 2); system.out.printLN (total); system.out .printLN ("re-enter number:"); number2 = input .nxtDouble ();} while (input.hasNextDouble ()); System.out.println ("exit.");}}
and there is operating class
public class operations {public static double addition (double N 1, double N2) {return N1 + N2;}} < / Code>
pair this first two values It displays the results, and displays the results again. Then this value asks again, the user inputs, and it displays the result. But there is a problem here, which I tried but could not do it, so please Look at the code and tell me where the problem is. While doing so, something that is doing me wrong.
Output:
Enter the first number
5
Enter the second number:
2
7.0
Enter the number again:
1
8.0
Please enter the number again:
2
The program is running but it does, for example, I press 6 again, it still adds 2 (which I had previously entered) and demonstrates that
6
10.0
Please enter the number again:
< Div class = "post-text" itemprop = "text">
Your problem is in order that you are indicating and doing things Want to input at the beginning of time: Enter
to {system.out.println ( "Re-number"); Number2 = input.nextDouble (); Total = operation.ed (total, number 2); Println (total); } While (input.hasNextDouble ());
It redefines the signal and re-input before, so your last category looks like this:
public static zero main (string [] Args) {scanner input = new scanner (System.in); System.out.println ("Enter first number:"); Double number = input.nextDouble (); System.out.println ("Enter second number:"); Double No. 2 = Input NXD double (); Double total = operation.ed (number, number 2); Println (total); Do {System.out.println ("Enter the number again:"); Number2 = input.nextDouble (); Total = operation.ed (total, number 2); Println (total); } While (input.hasNextDouble ()); Println ("Exit."); }
No comments:
Post a Comment