6025 Shares

I am writing a java program. How can I call the same method twice with 2 different outcomes? (two println's)

I am writing a java program. How can I call the same method twice with 2 different outcomes? (two println's) Topic: How to write a main method java
June 16, 2019 / By Jochebed
Question: I am writing a java program which consists of a main method, and also consists of some public static methods. I named one of my methods getMass() and called it in the main method as getMass();. For my program I must call this method twice but each time it prints out a different statement. The first statement asks the user to enter the first mass and the second asks the user to enter the second mass. Any idea how to do this?
Best Answer

Best Answers: I am writing a java program. How can I call the same method twice with 2 different outcomes? (two println's)

Gae Gae | 10 days ago
ok what about this: ... getMass(0); ... getMass(1); ... private function getMass( int nMode ) { ... switch ( nMode ) { case 0: //Enter first mass break; case 1: //Enter second mass break; } }
👍 268 | 👎 10
Did you like the answer? I am writing a java program. How can I call the same method twice with 2 different outcomes? (two println's) Share with your friends

We found more questions related to the topic: How to write a main method java


Gae Originally Answered: I am writing a java program. How can I call the same method twice with 2 different outcomes? (two println's)
ok what about this: ... getMass(0); ... getMass(1); ... private function getMass( int nMode ) { ... switch ( nMode ) { case 0: //Enter first mass break; case 1: //Enter second mass break; } }
Gae Originally Answered: I am writing a java program. How can I call the same method twice with 2 different outcomes? (two println's)
use a class field (or static variable) to keep track of the calls: class A { int calls = 0; static int staticCalls = 0; static int main(...) { A = new A(); A.x(); A.x(); y(); y(); } void x() { if (calls == 0) { System.out.println("1st call"); calls = 1; } else { System.out.println("after 1st call"); } } // end of method x static void y() { if (staticCalls == 0) { System.out.println("1st call"); staticCalls = 1; } else { System.out.println("after 1st call"); } } // end of static method y

Dayna Dayna
use a class field (or static variable) to keep track of the calls: class A { int calls = 0; static int staticCalls = 0; static int main(...) { A = new A(); A.x(); A.x(); y(); y(); } void x() { if (calls == 0) { System.out.println("1st call"); calls = 1; } else { System.out.println("after 1st call"); } } // end of method x static void y() { if (staticCalls == 0) { System.out.println("1st call"); staticCalls = 1; } else { System.out.println("after 1st call"); } } // end of static method y
👍 110 | 👎 9

Briar Briar
System.out.print( "Enter Mass 1: " ); double mass1 = getMass(); System.out.print( "Enter Mass 2: " ); double mass2 = getMass(); /************ or **************/ double mass1 = getMass( "Enter Mass 1: " ); double mass2 = getMass( "Enter Mass 2: " ); .......... public static double getMass( String str ) { Scanner input = new Scanner( ); try { System.out.print( str ); return input.nextDouble( ); } catch(Exception e) { e.printStackTrace(); return -1; } }
👍 101 | 👎 8

Briar Originally Answered: Polygon point java program?
Okay, you're going to write a class called Polygon with the following methods: add(Point2D.Double aPoint) add(Point2D.Double[] points) perimeter() area() It will also have a field to store the Polygon's points, I would suggest using a Vector or ArrayList. The constructor will basically initialize (i.e. create a "new" object) the field without adding any Point2D.Double objects to it. For the second add method, the one that takes an array as a parameter, I would just use a for loop to iterate over the elements of the array and pass those elements to the first add method. That is, for(int i=0; i

If you have your own answer to the question how to write a main method java, then you can write your own version, using the form below for an extended answer.