Zi F
Zi F

Reputation: 21

How to stop a program if answer to question is false

I had a prompt to write a program in java that asks users for the base and height of a triangle. Then the program is supposed to find the area. I started the program asking the user if they are willing to help me. If they say "Yes" then the program continues. If they say anything other than "Yes" I want the program to stop. I don't know how to get it to reply to the user, then stop running. My code is below. If you have time to review the full thing that would be awesome! I'm brand new at this.

  public static void main(String[] args) {
    System.out.println("Hey can you help me practice finding the area of a triangle?");
     String answerFirstQuestion = "Yes";
    //if (answerFirstQuestion = Yes){
    //}else{
     // System.exit(0);
    //}
    //^^This part isn't apart of the prompt. I wanted to ask the user a question, then have them answer "Yes". If they entered anything other than that, I wanted the program to print "Oh ok, that's fine. Thanks anyways." then stop. Could I get some feedback on how to accomplish this.
    switch (answerFirstQuestion) {
      case "Yes":
      System.out.println("Thanks kid!\nJust give me a random base and height for our imaginary triangle.\nI'll then use the equation b*h/2 to see if my program can find the area.");
      break;
      default: 
      System.out.println("Oh ok, that's fine.\nThanks anyways.");
    }
    int base;
    base = 6;
    int height;
    height = 12;
    int area = base*height/2;
    area = base*height/2;    
    System.out.println("The base of the triangle is? " + base);
    System.out.println("The height of the triangle is? " + height);
    System.out.println("Area of Triangle is: " + area);
    System.out.println("Cool I guess it works, thanks again!");
  }
}

Upvotes: 1

Views: 201

Answers (2)

Niloofar Adelkhani
Niloofar Adelkhani

Reputation: 227

The java.lang.System.exit() method exits the current program by terminating running Java virtual machine.
The following example shows the usage of java.lang.System.exit() method.

  // A Java program to demonstrate working of exit()
import java.util.*;
import java.lang.*;
  
class GfG
{
    public static void main(String[] args)
    {
        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
  
        for (int i = 0; i < arr.length; i++)
        {
            if (arr[i] >= 5)
            {
                System.out.println("exit...");
  
                // Terminate JVM
                System.exit(0);
            }
            else
                System.out.println("arr["+i+"] = " +
                                  arr[i]);
        }
        System.out.println("End of Program");
    }

}

I hope that this helps you :).

Upvotes: 2

enzo
enzo

Reputation: 11496

Just return.

System.out.println("Hey can you help me practice finding the area of a triangle?");
String answerFirstQuestion = "Yes";
switch (answerFirstQuestion) {
    case "Yes":
        System.out.println("Thanks kid!\nJust give me a random base and height for our imaginary triangle.\nI'll then use the equation b*h/2 to see if my program can find the area.");
        break;
    default: 
        System.out.println("Oh ok, that's fine.\nThanks anyways.");
        return;   
        // Return from the main function. The code below this will NOT be executed
}

Upvotes: 1

Related Questions