添加' While'循环到我的计划

时间:2017-02-19 00:17:01

标签: java while-loop

我正在开发一个计算圆(C),方(S)或矩形(R)面积的程序,具体取决于用户输入的字母。我测试了它,它工作正常;代码如下:

import java.util.Scanner;

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

    System.out.println("What is your shape? Enter C for circle, S for " + 
            "square, R for rectangle, or X to exit: ");
    String Shape = input.nextLine();

    if (Shape.equals("C")) {
        System.out.println("What is your circle's radius?: ");
        double Radius = input.nextDouble();
        double cFormula = (3.14 * Radius * Radius);
        System.out.println("Your circle's area = " + cFormula); 
    }
    else if (Shape.equals("S")) {
        System.out.println("What is the length of your shape's sides?: ");
        double Side = input.nextDouble();
        double sFormula = (Side * Side);
        System.out.println("Your square's area = " + sFormula);           
    }
    else if (Shape.equals("R")) {
        System.out.println("What is your rectangle's height?: ");
        double Height = input.nextDouble();
        System.out.println("What is your rectangle's width?: ");
        double Width = input.nextDouble();
        double rFormula = (Height * Width);
        System.out.println("Your rectangle's area = " + rFormula);
    }

   }

  }

现在,我想要做的是为程序添加一个循环。例如,如果用户输入C代表圆圈并输入数字22作为半径,他们会得到答案,但我希望程序再次循环回到开头,以便它询问用户&#34你的形状是什么?..."。此外,如果用户键入X而不是C,S或R,我希望程序退出,但我也不确定如何添加该程序。

我知道我需要添加' while'循环,但我希望有人可以指出我正确的方向,因为我不知道在哪里插入代码的那部分。我是否添加了' while'在代码开头的某个地方循环,在最后一个" if else"声明,或者......而且,我实际上并不确定要键入什么。应该是这样吗,

while (Shape == C, S, R) {
   ....?

编码社区中的任何人都会感谢任何帮助或指示!我将继续自己研究这些代码。

1 个答案:

答案 0 :(得分:1)

我会选择do, while

因此,程序将始终在完成设置的条件时执行某些操作,因此您希望程序看起来像:

public class TestLoops {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        boolean thisB = false; /*this is the guy who will tell the loop to stop the execution when the user inserts X*/
        String shape;
        do{
            System.out.println("What is your shape? Enter C for circle, S for " +
                    "square, R for rectangle, or X to exit: ");
            shape = input.next();

            if(shape.equalsIgnoreCase("C") || shape.equalsIgnoreCase("S") || shape.equalsIgnoreCase("R")) {
                if (shape.equals("C")) {
                    System.out.println("What is your circle's radius?: ");
                    double Radius = input.nextDouble();
                    double cFormula = (3.14 * Radius * Radius);
                    System.out.println("Your circle's area = " + cFormula);
                } else if (shape.equals("S")) {
                    System.out.println("What is the length of your shape's sides?: ");
                    double Side = input.nextDouble();
                    double sFormula = (Side * Side);
                    System.out.println("Your square's area = " + sFormula);
                } else if (shape.equals("R")) {
                    System.out.println("What is your rectangle's height?: ");
                    double Height = input.nextDouble();
                    System.out.println("What is your rectangle's width?: ");
                    double Width = input.nextDouble();
                    double rFormula = (Height * Width);
                    System.out.println("Your rectangle's area = " + rFormula);
                }
            }
            else if (shape.equalsIgnoreCase("X")) thisB = true;/*or in other words: stop*/
        }
        while(!thisB);
    }

}

需要考虑的事项:

1)命名约定,总是使用camelCase使用大写字母启动变量名称,在您的示例shape中以UpperCase开头

2)在while循环中,仅使用next(),而不是nextLine()来获取值,因为后者会在System.out.Println中复制问题。< / p>

3)执行此操作的最佳方法是将所有if子句放在方法中,并使用Scanner输入中的参数调用它。更好的方法是每个形状都有一个方法,因为根据要求,事情会变得毛茸茸