继续使用字符串的if / else语句(是或否)

时间:2014-09-06 04:38:49

标签: java if-statement

// SquaresIn program 
import java.util.Scanner;
import java.lang.Math;
public class AreaInv2{

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

  System.out.println ("Hi there! What's your name?"); 
  String name = sc.next();

  System.out.println("Alright , " + name + ", enter the radius of the circle:");
  float radius = sc.nextFloat();

  float area = (float)(Math.pow(radius, 2) * Math.PI);
  System.out.print("The area is ");
  System.out.printf("%.2f", area);
  System.out.println(". Would you like the area of any other circles?");

  String yesno;
  yesno = sc.next();
  {
      if (yesno.equalsIgnoreCase("yes"))
          {
              System.out.println("Alright , " + name + ", enter the radius of the circle:");
              radius = sc.nextFloat();

              area = (float)(Math.pow(radius, 2) * Math.PI);
              System.out.print("The area is ");
              System.out.printf("%.2f", area);
              System.out.print(" square units.");
              System.out.println(" Would you like the area of any other circles?");
          }
      else if (yesno.equalsIgnoreCase("no"))
          {
              System.out.print("Have a great day " + name + "!");
              sc.close();

          }
      else
      {
          System.out.println("Sorry, could you say yes or no?");
          yesno = sc.next();
          if (yesno.equalsIgnoreCase("yes"))
          {
              System.out.println("Alright , " + name + ", enter the radius of the circle:");
              radius = sc.nextFloat();

              area = (float)(Math.pow(radius, 2) * Math.PI);
              System.out.print("The area is ");
              System.out.printf("%.2f", area);
              System.out.print(" square units.");
              System.out.println(" Would you like the area of any other circles?");
          }
      else if (yesno.equalsIgnoreCase("no"))
          {
              System.out.print("Have a great day " + name + "!");
              sc.close();
      }
      }   
  }
 }
}

您好,我刚开始使用Java进行编程,即使经过大量研究,我似乎无法弄清楚如何以我理解的方式执行此操作。目前,有人回复"你想要任何其他圈子的区域"有三种选择:A)是B)没有C)任何东西。当你回答" no"它完美地运作。当你输入随机字符时,它会要求你说“是”或“否”,在这一点上回答“不正常”但当我回答“是”时类似于A,它会一直找到圆圈的区域并询问你是否愿意喜欢其他圈子的区域。这是我想知道我是否可以做到这一点,以便程序永远持续到用户输入" no"。如果有人能够检查出来并告诉我我能做什么,那将是非常有帮助的,请记住我对Java的了解很少。谢谢!

编辑:谢谢大家的帮助,我打电话给我的一个朋友,他向我解释了一切。我很感谢你们为我做的事情:)

4 个答案:

答案 0 :(得分:0)

您可以使用do {} while并使用 -

等简化代码
Scanner sc = new Scanner(System.in);

System.out.println("Hi there! What's your name?");
String name = sc.next();

String yesno = null;
do {
    System.out.printf("Alright %s, enter the radius of the circle: ",
            name);

    float radius = sc.nextFloat();
    float area = (float) (Math.pow(radius, 2) * Math.PI);
    System.out.printf("The area is %.2f. Would you like "
            + "the area of any other circles?", area);
    yesno = sc.next();
    while (!yesno.equalsIgnoreCase("yes")
            && !yesno.equalsIgnoreCase("no")) {
        System.out.println("Sorry, could you please say yes or no?");
        yesno = sc.next();
    }
} while (yesno.equalsIgnoreCase("yes"));
System.out.printf("Have a great day %s!%n", name);

答案 1 :(得分:0)

如果只要满足给定条件就想运行某些东西,那么你必须使用循环。在Java中,我们有三种类型的循环,do-while循环,while循环和for循环。

在你的情况下你应该使用do-while,因为你需要在检查条件之前至少运行一次。

请尝试以下代码,希望它能为您提供帮助。

import java.util.Scanner;

public class AreaInv2 {

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

        System.out.println("Hi there! What's your name?");
        String name = sc.next();
        String yesno;
        do {
            System.out.println("Alright , " + name + ", enter the radius of the circle:");
            float radius = sc.nextFloat();   
            float area = (float) (Math.pow(radius, 2) * Math.PI);

            System.out.print("The area is ");
            System.out.printf("%.2f", area);
            System.out.println(". Would you like the area of any other circles?");
            yesno = sc.next();

            while ((!(yesno.equalsIgnoreCase("yes") || yesno.equalsIgnoreCase("no")))) {
                System.out.println("Sorry, could you say yes or no?");
                yesno = sc.next();
            }
        } while (yesno.equalsIgnoreCase("yes"));

        System.out.print("Have a great day " + name + "!");
        sc.close();                
    }
}

它与您的代码几乎相同,我只是添加了循环,并将您的条件放在那里。

答案 2 :(得分:0)

最好将代码组织在可重用的函数中,以使其更具可读性:

public class AreaInv2 {

    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {

        String name = getName();
        while (true) {
            float radius = getRadius(name);
            printArea(radius);
            System.out.println("Would you like the area of any other circles? ");
            if (!needToDoAgain()) {
                System.out.print("Have a great day " + name + "!");
                break;
            }
        }
        sc.close();
    }

    private static String getName() {
        System.out.println("Hi there! What's your name?");
        return sc.next();
    }

    private static float getRadius(String name) {
        System.out.println("Alright , " + name + ", enter the radius of the circle: ");
        return sc.nextFloat();
    }

    private static void printArea(float radius) {
        float area = (float) (Math.pow(radius, 2) * Math.PI);
        System.out.print("The area is ");
        System.out.printf("%.2f", area);
        System.out.println(".");
    }

    private static boolean needToDoAgain() {
        String yesno = sc.next();
        if (yesno.equalsIgnoreCase("yes")) {
            return true;
        } else if (yesno.equalsIgnoreCase("no")) {
            return false;
        } else {
            System.out.println("Sorry, could you say yes or no? ");
            return needToDoAgain();
        }
    }
}

答案 3 :(得分:0)

好吧,如果你想在某些特定条件发生之前继续(不结束),你应该使用循环。 whiledo whilefor是java中的三种方法。

        Scanner sc = new Scanner(System.in);

        System.out.println("Hi there! What's your name?");
        String name = sc.next();

        System.out.println("Alright , " + name
                + ", enter the radius of the circle:");
        float radius = sc.nextFloat();

        float area = (float) (Math.pow(radius, 2) * Math.PI);
        System.out.print("The area is ");
        System.out.printf("%.2f", area);
        System.out.println(". Would you like the area of any other circles?");
        String yesno = null;

        yesno = sc.next();

        while (!yesno.equalsIgnoreCase("no")) {

            if (yesno.equalsIgnoreCase("yes")) {

                System.out.printf(
                        "Alright %s, enter the radius of the circle: ", name);

                radius = sc.nextFloat();
                area = (float) (Math.pow(radius, 2) * Math.PI);
                System.out.printf("The area is %.2f. Would you like "
                        + "the area of any other circles?", area);

            } else {

                System.out.println("Sorry, could you please say yes or no?");
            }
            yesno = sc.next();
        }

        sc.close();

        System.out.printf("Have a great day %s!%n", name);