gpt4 book ai didi

java - 无法打印退出语句和打印其他与菜单选项不匹配的输入

转载 作者:行者123 更新时间:2023-11-30 09:53:30 25 4
gpt4 key购买 nike

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

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

最佳答案

从以下条件中删除 !(非)运算符

 if (!(menuOption.equals("Exit")))

关于java - 无法打印退出语句和打印其他与菜单选项不匹配的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40074388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com