import java.util.Scanner;
import java.lang.Math;
public class ClassNine {
public static Scanner INPUT = new Scanner(System.in);
public static void nine() {
boolean loop = true;
while(loop) {
loop = areaMenu();
}
Seperator.addLine();
}
public static boolean areaMenu() {
boolean loop2 = true;
boolean validChoice = false;
while(loop2) {
System.out.print("\n1 - Triangle\n"
+ "2 - Square\n"
+ "3 - Rectangle\n"
+ "4 - Parallelogram\n"
+ "5 - Trapezium\n"
+ "6 - Circle\n"
+ "7 - Ellipse\n"
+ "8 - Sector\n"
+ "\t>>enter 'exit' to leave.\n"
+ "\nChoice: ");
String choice = INPUT.nextLine();
if(choice.matches("[0-8]")) {
validChoice = true;
}else if(choice.matches("exit")){
return false;
}else{
System.out.println("Invalid Input. Exiting 3."); //TODO
validChoice = false;
}
if(validChoice) {
try {
switch(choice){
case "1": triangle();
break;
case "2": square();
break;
case "3": rectangle();
break;
case "4": parallelogram();
break;
case "5": trapezium();
break;
case "6": circle();
break;
case "7": ellipse();
break;
case "8": sector();
break;
case "exit": loop2 = false;
break;
}
}catch(Exception e) {
System.out.println("Invalid Input. Exiting.");
break;
}
}
}
return true;
}
public static void triangle() {
System.out.print("\nTriangle Base: ");
double base = INPUT.nextDouble();
System.out.print("Triangle Height: ");
double height = INPUT.nextDouble();
System.out.println("\n\nArea of Traingle: " + 0.5*base*height + "\n");
}
public static void square() {
System.out.print("\nSquare Side Lenght: ");
double side = INPUT.nextDouble();
System.out.println("\n\nArea of Square: " + Math.pow(side, 2) + "\n");
}
public static void rectangle() {
System.out.print("\nRectangle Lenght: ");
double lenght = INPUT.nextDouble();
System.out.print("Rectangle Height: ");
double height = INPUT.nextDouble();
System.out.println("\n\nArea of Rectangle: " + lenght*height + "\n");
}
public static void parallelogram() {
System.out.print("\nParallelogram Lenght: ");
double lenght = INPUT.nextDouble();
System.out.print("Parallelogram Height: ");
double height = INPUT.nextDouble();
System.out.println("\n\nArea of Parallelogram: " + lenght*height + "\n");
}
public static void trapezium() {
System.out.print("\nTrapezium Narrow Width: ");
double narrow = INPUT.nextDouble();
System.out.print("Trapezium Wide Width: ");
double wide = INPUT.nextDouble();
System.out.print("Trapezium Height: ");
double height = INPUT.nextDouble();
System.out.println("\n\nArea of Trapezium: " + 0.5*(narrow + wide)*height + "\n");
}
public static void circle() {
System.out.print("\nCircle Radius: ");
double radius = INPUT.nextDouble();
System.out.println("\n\nArea of Circle: " + radius*Math.pow(Math.PI, 2) + "\n");
}
public static void ellipse() {
System.out.print("\nEllipse Narrow Radius: ");
double narrow = INPUT.nextDouble();
System.out.print("Ellipse Wide Radius: ");
double wide = INPUT.nextDouble();
System.out.println("\n\nArea of Ellipse: " + Math.PI*narrow*wide + "\n");
}
public static void sector() {
System.out.print("\nSector Radius: ");
double radius = INPUT.nextDouble();
System.out.print("Value of angle subtending the sector (in radians): ");
double angle = INPUT.nextDouble();
System.out.println("\n\nArea of sector: " + 0.5*Math.pow(radius, 2)*angle + "\n");
}
}
在选择一个选项并执行了相关的方法后,程序将循环并再次通过选项菜单显示,这是应该的,但是由于没有任何进一步的输入,INPUT.nextLine()
返回一个空字符串并直接转到'其他”语句中包含“退出3”,然后再循环一遍并实际上等待用户提示。
为什么Scanner.nextLine()
在scanner.nextDouble()
之后返回一个多余的空行?
另一个半问题是输入“ exit”会执行应做的操作并退出程序,但是查看代码不应结束程序,而只是结束程序 再次循环菜单,因为它中断了返回true的循环以再次开始循环。对? (反问,我显然缺少了一些东西)