给出答案后如何重置程序

我在指南的帮助下完成了三角形面积计算的基本代码。然后我添加了一些代码来让它更有趣,但我希望它在你得到结果后重置。我希望程序在使用有效值完成先前的计算后重置为“输入三角形的底数:”。如果它在 (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 {


      }       


    }

 }


qq_花开花谢_0
浏览 161回答 2
2回答

莫回无

您想使用退出的 while 循环,例如,通过让用户输入“0”作为退出代码。您可以使用boolean初始化为 true 并在用户输入退出代码时设置为 false 的 。import java.util.Scanner;public class Main {&nbsp; &nbsp; static Scanner sc = new Scanner(System.in);&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; boolean repeat = true;&nbsp; &nbsp; &nbsp; &nbsp; while (repeat) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // declare variables to hold the base and height&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double base;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double height;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Variables created. move on&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter the triangle's base (enter 0 to exit): ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; base = sc.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (base == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repeat = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Base has been declared and filled in&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter the triangle's height (enter 0 to exit): ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height = sc.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (height == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repeat = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Both variables are filled in&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double preArea = base * height;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // now we need to divide by 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double Area = preArea / 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // There we go. All variables are done, area has been calculated.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The area of your triangle is: " + Area);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Area <= 100) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Triangle's area is small");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Area <= 300) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Triangles size is medium");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Area >= 300) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Triangle's area is big");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Bye bye");}}

aluckdog

我认为你需要做while (true){&nbsp; &nbsp; your code&nbsp;&nbsp; &nbsp; if (exit condition){&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}这样你的程序就会循环。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java