有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法获取要打印的exit语句和用于任何与菜单选项不匹配的输入的else语句

我根本无法打印最后两条语句

import java.util.Scanner;
import java.lang.Math;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Practice {
public static void main(String[] args){
Scanner input = new Scanner(System.in);

我在这里登录了这台机器,它可以工作,所以我删除了那个部分。除此之外,这似乎是我创建的循环的一个错误。我只是需要一些帮助来理解我哪里出了错

                //Body of Shape Machine

                String menuOption = "";


        while(!(menuOption.equals("Exit"))){
                String circles = ("Circles");
                String rectangles = ("Rectangles");
                String triangles = ("Triangles");
                String exit = ("Exit");

                System.out.println("---Welcome to the Shape Machine---");
                System.out.println("Available Options:");
                System.out.println(circles);
                System.out.println(rectangles);
                System.out.println(triangles);
                System.out.println(exit);
                System.out.println("");

                menuOption = input.next();      

    if (menuOption.equals("Circles")){  

                //CIRCLES
                System.out.println("Circles selected. Please enter the radius:");
                double userRadius = input.nextDouble();

            if (userRadius < 0) {
                while (userRadius < 0){
                System.out.println("#ERROR Negative input. Please input the radius again:");
                userRadius = input.nextDouble();
                }
            }

                input.nextLine();

                if (userRadius > 0){
                    double circumference = (2 * userRadius * Math.PI);
                    double circleArea = Math.PI * userRadius * userRadius;
                    System.out.println("The circumference is: " + circumference);
                    System.out.println("The area is: " + circleArea);

                String digitsCircumference = Double.toString(circumference);
                String digitsCircleArea = Double.toString(circleArea);

                    System.out.println("Total number of digits in the circumference is: " + (digitsCircumference.length()-1));
                    System.out.println("Total number of digits in the area is: " + (digitsCircleArea.length()-1));
            }
            continue;
            }   

    if (menuOption.equals(rectangles)){

                    //RECTANGLES

                    System.out.print("Rectangles selected. Please enter the 2 sides: ");
                    double rectangleSide1 = input.nextDouble();
                    double rectangleSide2 = input.nextDouble();

            while (rectangleSide1 < 0 || rectangleSide2 < 0){
                    System.out.print("#ERROR Negative input. Please input the 2 sides again: ");
                    rectangleSide1 = input.nextDouble();
                    rectangleSide2 = input.nextDouble();

                }           
                    double rectangleArea = (rectangleSide1 * rectangleSide2);
                    double rectanglePerimeter = ((rectangleSide1 * 2) + (rectangleSide2 * 2));
                    System.out.println("The area is: " + rectangleArea);
                    System.out.println("The perimeter is: " + rectanglePerimeter);
                    String digitsRectangleArea = Double.toString(rectangleArea);
                    String digitsRectanglePerimeter = Double.toString(rectanglePerimeter);
                    System.out.println("Total number of digits in the area is: " + (digitsRectangleArea.length()-1));
                    System.out.println("Total number of digits in the perimeter is: " + (digitsRectanglePerimeter.length()-1));
                    continue;
                }


    if (menuOption.equals("Triangles")){

                    //TRIANGLES

                    System.out.print("Triangles selected. Please enter the 3 sides: ");
                    double triangleSide1 = input.nextDouble();
                    double triangleSide2 = input.nextDouble();
                    double triangleSide3 = input.nextDouble();
            while (triangleSide1 < 0 || triangleSide2 < 0 || triangleSide3 < 0){
                    System.out.print("#ERROR Negative input. Please input the 3 sides again: ");

                    triangleSide1 = input.nextDouble();
                    triangleSide2 = input.nextDouble();
                    triangleSide3 = input.nextDouble();
            }
            if ((triangleSide1 + triangleSide2) <= triangleSide3 || (triangleSide1 + triangleSide3) <= triangleSide3 || (triangleSide2 + triangleSide3) <= triangleSide1){
                    System.out.println("#ERROR Triangle is not valid. Returning to menu.");
                    continue;
            }
            if ((triangleSide1 + triangleSide2) > triangleSide3 && (triangleSide1 + triangleSide3) > triangleSide3 && (triangleSide2 + triangleSide3) > triangleSide1){

                double trianglePreliminary = (triangleSide1 + triangleSide2 + triangleSide3)/2;
                double triangleArea = Math.sqrt(trianglePreliminary * ((trianglePreliminary - triangleSide1) * (trianglePreliminary - triangleSide2) * (trianglePreliminary - triangleSide3)));
                double trianglePerimeter = (triangleSide1 + triangleSide2 + triangleSide3);

                if ((triangleSide1 == triangleSide2) && (triangleSide2 == triangleSide3)){
                    System.out.println("The triangle is: Equilateral");
                }               
                else if ((triangleSide1 == triangleSide2) || (triangleSide2 == triangleSide3) || (triangleSide1 == triangleSide3)){
                    System.out.println("The triangle is: Isosceles");
                }
                else if ((triangleSide1 != triangleSide2) && (triangleSide2 != triangleSide3) && (triangleSide1 != triangleSide3)){
                    System.out.println("The triangle is: Scalene");
                }

                System.out.println("The perimeter is: " + trianglePerimeter);
                System.out.println("The area is: " + triangleArea);
                String digitsTrianglePerimeter = Double.toString(trianglePerimeter);
                String digitsTriangleArea = Double.toString(triangleArea);
                System.out.println("Total number of digits in the area is: " + (digitsTrianglePerimeter.length()-1));
                System.out.println("Total number of digits in the perimeter is: " + (digitsTriangleArea.length()-1));
                continue;
            }

 here I have trouble and I can not get these menuOption statements to print. I am not sure why either.

        if (!(menuOption.equals("Exit")))
            {
            System.out.print("Terminating the program. Have a nice day!\n");
            System.exit(0);
            }


        else 
            {   
            System.out.print("#ERROR Invalid option. Please try again.\n");
            continue;
            }
        }                       
    }   
}

共 (2) 个答案

  1. # 1 楼答案

    走开!(非)操作员出现以下情况

     if (!(menuOption.equals("Exit")))
    
  2. # 2 楼答案

    Java是面向对象的

    Java是为面向对象编程而设计的,所以您可能希望使用不同的类和类型层次结构。。。但对于这样的事情来说,这可能太过分了

    So use Scriptlanguages for this like Python, Powershell ...

    如果你想坚持这种Java风格

    至少使用不同的功能。一个用于圆,一个用于矩形等等

    public static double circleArea(double radius){
      return Math.PI*radius*radius;
    }
    //for circumference, rectangle area...
    public static void processCircle(){
      System.out.println("You selected Circle");
      double radius = inputDouble("Radius:",x->{return x>=0});
      System.out.println("Area: "+circleArea(radius));
    }
    

    你可能想知道inputDouble("Radius: ",x->{return x>=0})。这是我的下一篇文章。不要重复代码!你经常接受输入并测试它是否有效。inputDouble函数的定义如下:

    public static Scanner scanner= new Scanner(System.in);
    public static double inputDouble(String popup, Function<boolean> condition){
      double out;
      while(!condition.applyAsDouble(out=scanner.nextDouble())){
      }
      return out;
    }
    

    在main函数中有变量exitrectanglestriangles,但在if语句中不使用它们。还可以使用不区分大小写的比较

    if(menuoption.equalsIgnoreCase(circles)){
      processCircle();
    }
    

    现在谈谈你的实际问题。我想我可能做得有点太多了。:) 你有

    if(!menuoption.equals("Exit")) {
    

    但是你想要

    if(menuoption.equalsIgnoreCase(exit)) {