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

Java 使用字符串继续执行if/else语句(是或否),java,if-statement,Java,If Statement,您好,我刚开始用Java编程,即使在做了很多研究之后,我似乎也不知道如何以我理解的方式来完成。目前,当有人回答“你想要其他圆圈的面积吗?”时,有三种选择:A)是B)否C)任何东西。当你回答“不”时,它工作得很好。当你输入随机字符时,它会要求你说是或否,在这一点上回答“否”很好,但类似于A。当我回答“是”时,它会上升到找到圆的面积并询问你是否想要其他圆的面积。这是我想知道我是否可以使程序永远继续,直到用户输入“否”。如果有人能检查一下并告诉我我能做什么,那将非常有帮助,请记住我对Java的了解很少

您好,我刚开始用Java编程,即使在做了很多研究之后,我似乎也不知道如何以我理解的方式来完成。目前,当有人回答“你想要其他圆圈的面积吗?”时,有三种选择:A)是B)否C)任何东西。当你回答“不”时,它工作得很好。当你输入随机字符时,它会要求你说是或否,在这一点上回答“否”很好,但类似于A。当我回答“是”时,它会上升到找到圆的面积并询问你是否想要其他圆的面积。这是我想知道我是否可以使程序永远继续,直到用户输入“否”。如果有人能检查一下并告诉我我能做什么,那将非常有帮助,请记住我对Java的了解很少。谢谢


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

你们可以使用
do{}while
来简化你们的代码-

// 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中,我们有三种类型的循环,do-while循环、while循环和for循环

在您的情况下,您应该使用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);

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

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

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();                
    }
}

好的,如果您希望某些事情继续(而不是结束)直到某些特定条件发生,您应该使用循环<代码>while,
do while
for
是java中的三种方法

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();
        }
    }
}

阅读do while循环。或者尝试在这里搜索。类似的问题已经被问过很多次了。你需要在这里使用循环,在你的情况下使用do while loops更好既然你在做方法,为什么不把
name
作为一个实例变量,这样你就不会把它传给你的方法了
getRadius(字符串名)
对我来说没有多大意义。@J.Lucky你说得对,但name实际上不是一个实例变量。在本例中,程序只获得一次名称,但它可能会在每次循环迭代中更改以获得名称…递归代替迭代的有趣用法。嗨,这是一群人中唯一一个我实际上能够理解的响应代码。你能解释一下while(!(yesno.equalsIgnoreCase(“yes”)| | yesno.equalsIgnoreCase(“no”){System.out.println(“对不起,你能说是还是否?”);yesno=sc.next();部分吗?| |和感叹号是什么意思?它有什么作用(代码块)while yesno=yes只要他们回答,就说继续循环那个部分?更新:我刚打电话给一个朋友,让他解释你写的代码,现在我明白了!谢谢!@gmitra,嘿,很抱歉昨天没能回复。我没有上网。总之,我很高兴这对你有用。谢谢你接受了答案。
        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);