Java 如何让用户使用字符串输入选择特定项?
这是我在java开发课上的一些家庭作业,我们应该创建一个形状计算器,由正方形、三角形和圆形组成,同时在用户输入他们想要看到的形状后显示所述形状的半径和周长 到目前为止,我有很多麻烦,作业的提示要求这样“询问用户他们想使用哪种形状” 我不知道如何实现用户键入形状的功能,java将识别键入的内容,并专门针对用户键入的形状并显示其属性,我假设它将使用else语句 我已经找到了如何显示字符串,提示用户他们想要使用什么形状,但我无法找到如何专门针对所述形状,并让它显示其半径和周长Java 如何让用户使用字符串输入选择特定项?,java,string,input,selection,Java,String,Input,Selection,这是我在java开发课上的一些家庭作业,我们应该创建一个形状计算器,由正方形、三角形和圆形组成,同时在用户输入他们想要看到的形状后显示所述形状的半径和周长 到目前为止,我有很多麻烦,作业的提示要求这样“询问用户他们想使用哪种形状” 我不知道如何实现用户键入形状的功能,java将识别键入的内容,并专门针对用户键入的形状并显示其属性,我假设它将使用else语句 我已经找到了如何显示字符串,提示用户他们想要使用什么形状,但我无法找到如何专门针对所述形状,并让它显示其半径和周长 package geom
package geometric;
import java.util.Scanner;
public class shapes {
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
String shapeSelect = "Which Shape: Square Circle or Triangle?";
System.out.println(shapeSelect);
String Circle;
double circle;
circle = scanner.nextDouble();
System.out.println("enter the radius: ");
}
}
您可以让用户输入几何形状的名称,然后计算扫描仪返回的字符串
String userInput = scanner.nextLine();
if(userInput.equals("circle")){ // be aware that equals is case sensitive. Use .equalsIgnoreCase otherwise
// print circle properties and ask for the radius
}else if(userInput.equals("square"){
// ask the user for square parameters
}
... and so on
您还可能希望向用户提供一个可能形状的完整列表,这样他就不会键入“hexagon”(六边形),尽管您只提供“Pentagan”(五角大楼)您可以让用户键入几何形状的名称,然后计算扫描仪返回的字符串
String userInput = scanner.nextLine();
if(userInput.equals("circle")){ // be aware that equals is case sensitive. Use .equalsIgnoreCase otherwise
// print circle properties and ask for the radius
}else if(userInput.equals("square"){
// ask the user for square parameters
}
... and so on
您可能还希望向用户提供一个可能形状的完整列表,这样他就不会键入“hexagon”,尽管您只提供“Pentagan”您希望在用户输入半径之前获得形状的类型,对吗
String input=scanner.nextLine()代码>
应在打印形状选择
消息后立即执行
存储拾取的任何形状后,您可以使用if else
或switch
语句为每个形状执行需要执行的操作,然后您只需将存储的变量传递给用于计算形状属性的方法作为参数
您的代码应该如下所示:
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
String shapeSelect = "Which Shape: Square Circle or Triangle?";
System.out.println(shapeSelect); // what shape?
String selectedShape = scanner.nextLine(); //waits for your input
System.out.println("enter the radius: "); // what radius?
double circleRadius = scanner.nextDouble(); //waits for your input
if(selectedShape.contentEquals("circle")){
//call circle methods
} else if(selectedShape.contentEquals("triangle")){
//call triangle methods
}
}
最好的做法是以小写字母开头变量名
对于条件,如果要使其不区分大小写,也可以使用equalsIgnoreCase
。要在形状输入半径之前获取形状类型,对吗
String input=scanner.nextLine()代码>
应在打印形状选择
消息后立即执行
存储拾取的任何形状后,您可以使用if else
或switch
语句为每个形状执行需要执行的操作,然后您只需将存储的变量传递给用于计算形状属性的方法作为参数
您的代码应该如下所示:
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
String shapeSelect = "Which Shape: Square Circle or Triangle?";
System.out.println(shapeSelect); // what shape?
String selectedShape = scanner.nextLine(); //waits for your input
System.out.println("enter the radius: "); // what radius?
double circleRadius = scanner.nextDouble(); //waits for your input
if(selectedShape.contentEquals("circle")){
//call circle methods
} else if(selectedShape.contentEquals("triangle")){
//call triangle methods
}
}
最好的做法是以小写字母开头变量名
对于条件句,如果要使其不区分大小写,也可以使用equalsIgnoreCase
。请改进格式并添加解释,因为不首选纯代码答案。请改进格式并添加解释,因为不首选纯代码答案