给出答案后如何重置程序

How to reset program after it has given the answer

您好,我已经在指南的帮助下完成了三角形面积计算的基本代码。然后我添加了一些代码以使其更有趣,但我希望它在您获得结果后重置。我希望程序在完成先前的有效值计算后重置为 "Enter the triangle's base: "。如果它能在 (x) 时间后重置就好了。

我只是想让它在任务完成后按假想计算器中的C按钮。

代码:

import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class TriangleArea {

static Scanner sc = new Scanner(System.in);
public static void main(String[] args) 
{

    //declare variables to hold the base and height
    double base;
    double height;
    //Variables created. move on
    System.out.print("Enter the triangle's base: ");
    base = sc.nextDouble();
    //Base has been declared and filled in
    System.out.print("Enter the triangle's height: ");
    height = sc.nextDouble();
    //Both variables are filled in
    double preArea = base * height;
    //now we need to divide by 2
    double Area = preArea / 2;
    //There we go. All variables are done, area has been calculated.
    System.out.println("The area of your triangle is: " + Area);

    int Outcome;

      if (Area <= 100) {
          System.out.println("Triangle's area is small");
      }

      if (Area <= 300) {
          System.out.println("Triangles size is medium");

      }
      if (Area >= 300) {
          System.out.println("Triangle's area is big");
      }

      else {

      }       

    }
 }

我认为你需要做

while (true){

    your code 

    if (exit condition){
        break;
    }
}

这样你的程序就会循环。

您想使用退出的 while 循环,例如让用户输入“0”作为退出代码。您可以使用初始化为 true 并在用户输入退出代码时设置为 false 的 boolean

import java.util.Scanner;

public class Main {
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {

        boolean repeat = true;
        while (repeat) {
            // declare variables to hold the base and height
            double base;
            double height;
            // Variables created. move on
            System.out.print("Enter the triangle's base (enter 0 to exit): ");
            base = sc.nextDouble();
            if (base == 0) {
                repeat = false;
                break;
            }
            // Base has been declared and filled in
            System.out.print("Enter the triangle's height (enter 0 to exit): ");
            height = sc.nextDouble();
            if (height == 0) {
                repeat = false;
                break;
            }
            // Both variables are filled in
            double preArea = base * height;
            // now we need to divide by 2
            double Area = preArea / 2;
            // There we go. All variables are done, area has been calculated.
            System.out.println("The area of your triangle is: " + Area);

            if (Area <= 100) {
                System.out.println("Triangle's area is small");
            }

            if (Area <= 300) {
                System.out.println("Triangles size is medium");

            }
            if (Area >= 300) {
                System.out.println("Triangle's area is big");
            }

            else {

            }

        }
        System.out.println("Bye bye");
}
}