5 Replies - 1172 Views - Last Post: 20 January 2016 - 02:40 AM Rate Topic: -----

#1 39852063wang   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 17-January 16

How do I keep the program running repeatedly, but can stop?

Posted 17 January 2016 - 09:15 PM

I have wrote a program that solves some mathematical calculations, but I need the program to keep restart itself every-time after the calculation is over.

import java.util.Scanner;

class EvenOrOdd{
	public static void main(String[] args){
		
		System.out.println("     Enter parabolic equation in standard form");
		System.out.println("     ax^2+bx+c");
		Scanner henry = new Scanner(System.in);
		double a,b,c,xintercept1,delta,xintercept2,ml,mv,m;
		
		System.out.println("     Enter a - value: ");
		a = henry.nextDouble();
		System.out.println("	 Enter b - value: ");
		b = henry.nextDouble();
		System.out.println("	 Enter c - value: ");
		c = henry.nextDouble();
		
		delta = Math.sqrt(b * b - 4 * a * c);
		xintercept1 = (-b+delta)/(2*a);
		xintercept2 = (-b-delta)/(2*a);
		System.out.println("	x-intercepts of the line ");
		System.out.println("");
		System.out.println("				[("+a+")x^2+("+b+")x+("+c+")]");
		System.out.println("");
		System.out.println("	are "+xintercept1+" and "+xintercept2);
		ml = -b/(2*a);
		mv = (4*a*c-b*B)/>/>/(4*a);
		m = 2*a;
		if (a>0){
			System.out.println("	The minimum value is located on");
	    }else if (a<0){
		System.out.println("	The maximum value is located on");
        }
		System.out.println("	("+ml+","+mv+")");
		System.out.println("	The dervative of the function is ");
		System.out.println("	[("+m+")x+("+b+")]");

	}
	}


I don't know how to make the code repeat itself after I have read all the results.
And I need a place where java can tell if I still want the program to be continuous or not.
such as
System.out.println("Do you want to do another calculation? Y/N")
Y -------- Rerun the code(!!THE CODE ONLY, NOT THE WHOLE PROGRAM!!)
N -------- Terminate the program entirely.

Still new to JAVA, need extra help on coding still.
(Pretty easy code as many of you can tell XD)

This post has been edited by no2pencil: 17 January 2016 - 09:16 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: How do I keep the program running repeatedly, but can stop?

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: How do I keep the program running repeatedly, but can stop?

Posted 17 January 2016 - 09:21 PM

What you want here is a while loop. While (continue) { do the stuff, then ask if continue}.
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: How do I keep the program running repeatedly, but can stop?

Posted 17 January 2016 - 09:25 PM

You might want to look into the notion of a loop:

boolean playAgain = true;

//the loop executes while the boolean
//expression in the parentheses evaluates to true
//in this case, while playAgain == true
while(playAgain){
    //your code

    System.out.println("Do you want to play again?");
    String input = scanner.nextLine();

    //if the user types "Yes", then we mark playAgain = true
    //so the loop will execute again.
    //otherwise, playAgain is set to false and the loop
    //does not run again
    playAgain = (input.equalsIgnoreCase("Yes"));
  
}



Locke has a good tutorial on looping and NeoTifa's Basic Java for N00blets is another good resource.
Was This Post Helpful? 1
  • +
  • -

#4 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1064
  • View blog
  • Posts: 4,105
  • Joined: 13-June 14

Re: How do I keep the program running repeatedly, but can stop?

Posted 18 January 2016 - 01:06 AM

Please also use meaningful names for variables, classes, methods, etc. - names like henry (line 8) are not good because they don't give any information about what the thing they're naming is for. Good names help you read and understand code and are therefore extremely important.

This post has been edited by ndc85430: 18 January 2016 - 01:22 AM

Was This Post Helpful? 3
  • +
  • -

#5 39852063wang   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 17-January 16

Re: How do I keep the program running repeatedly, but can stop?

Posted 19 January 2016 - 08:58 PM

View Postndc85430, on 18 January 2016 - 01:06 AM, said:

Please also use meaningful names for variables, classes, methods, etc. - names like henry (line 8) are not good because they don't give any information about what the thing they're naming is for. Good names help you read and understand code and are therefore extremely important.


Thank you for the advice. SWEAR going to fix the problem in the future programming... :bananaman:
Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: How do I keep the program running repeatedly, but can stop?

Posted 20 January 2016 - 02:40 AM

*
POPULAR

Quote

SWEAR going to fix the problem in the future programming...

Fix it in THIS program!
Was This Post Helpful? 5
  • +
  • -

Page 1 of 1