我如何要求用户输入字符串? (如果要输入中心和半径,或直径的端点?)(读取输入)

来源:爱站网时间:2021-09-27编辑:网友分享
//这是驱动程序,有一些资源类被调用/使用过。ignorethislineofwritingignorethislineofwritingignorethislineofwritingignorethislineofwritingimport java.util.Scanner; ...

问题描述


//这是驱动程序,有一些资源类称为/已使用

ignorethiswriteline忽略这条线忽略这条线忽略此文字线

    import java.util.Scanner;
    public class KI23CriclesDriver
    {
       public static void main (String[] args)
       {
          //Declare Variables
          int x1, y1, x2, y2;
          String print;
          double center;
          double center2;
          String radius;
          int validLength = 1;
          int validLength2 = 2;

          //Instantiate Objects
          KI23GetC gc = new KI23GetC();
          KI23GetCircles gs = new KI23GetCircles();
          KI23PrintC p = new KI23PrintC();
          System.out.println("Do you want to enter (Option 1) center and radius or (Option 2) end points of diameter?");
          x1 = 0;
          y1 = 0;
          x2 = 0;
          y2 = 0;
          Scanner scanner = new Scanner(System.in);
          radius = scanner.nextLine();
          if( radius.length() == validLength )
          {
             System.out.println("Please enter the center and radius");
             x2 = gc.getX();
             y2 = gc.getY();
             x1 = gc.getX();
             y1 = gc.getY();
             center = gs.getCenter(x1, y1, x2, y2);
             center2 = gs.getCenter2(x1, y1, x2, y2);  
             p.print(x1, y1, x2, y2, center, center2);
          }

          if( radius.length() == validLength2) // doesnt work since validlength2 is the same as validlength
          {
             System.out.println("Please enter the end points of diameter");
             x1 = gc.getX();
             y1 = gc.getY();
             x2 = gc.getX();
             y2 = gc.getY();
             center = gs.getCenter(x1, y1, x2, y2);
             center2 = gs.getCenter2(x1, y1, x2, y2);  
             p.print(x1, y1, x2, y2, center, center2);

          }
          else
          {
             System.out.println("Please enter 1 or 2");
             x1 = 0;
             y1 = 0;
             x2 = 0;
             y2 = 0;
             radius = scanner.nextLine();  
          }
    // 

//我想要一个更好的方法来接受输入并对此做出不同的决策和计算}}

思路:


您显然没有显示该类和其他类的所有代码,因此下面的示例代码基本上在一定程度上利用了您提供的内容。提示被分解,并在特定的类方法中提供。 main()方法仅调用另一个实际上开始滚球的方法(可以这么说)。这样做是为了避免需要静电。

startApp()方法调用mainMenu()方法,该方法又显示此控制台应用程序的主菜单。菜单包含在循环中,以确保用户正确输入。

从主菜单中选择的选项从mainMenu()方法返回为整数值,然后该整数值落入switch / case块的控制之下,该块又确定中心和半径(菜单项1)将被提供,或者圆弧终点(菜单项[[2)将被提供。

如果要提供中心和半径,则将调用

getCenterAndRadius()

方法,该方法提示用户提供中心值和半径值。如果要提供圆弧终点,则调用

getCircleEndPoints()

方法,该方法提示用户提供所有四个值(x1,y1,x2,y2)以组成两个所需的终点圈子。还提供了[[一种名为getCenterAndRadius_2()的附加方法,该方法演示了允许用户提供端点值的另一种方法。使用最适合您的方法,或者根据代码中给出的一些想法创建自己的方法。

Regular Expressions的使用在提供的代码中使用。 String#matches()String#split()String#replaceAll()方法利用这些正则表达式。

import java.util.Scanner; public class KI23CriclesDriver { // KI23GetC gc = new KI23GetC(); // KI23GetCircles gs = new KI23GetCircles(); // KI23PrintC p = new KI23PrintC(); private final Scanner userInput = new Scanner(System.in); private final String ls = System.lineSeparator(); private int x1 = 0, y1 = 0, x2 = 0, y2 = 0; public static void main(String[] args) { // Done this way to avoid statics new KI23CriclesDriver().startApp(args); } private void startApp(String[] args) { int menuOption = mainMenu(); switch (menuOption) { case 1: getCenterAndRadius(); break; case 2: getCircleEndPoints(); // getCircleEndPoints_2(); break; } } private int mainMenu() { int menuChoice = 0; while (menuChoice == 0) { System.out.println("Supply a circle creation option:"); System.out.println(" 1) Based on Center and Radius." + ls + " 2) Based on Diameter End Points."); System.out.print("Choice: --> "); String choice = userInput.nextLine().trim(); if (choice.toLowerCase().startsWith("q")) { // Quit appplication System.exit(0); } if (!choice.matches("[12]")) { System.err.println("Invalid menu choice! Try again..." + ls); continue; } menuChoice = Integer.parseInt(choice); } return menuChoice; } private void getCenterAndRadius() { System.out.println("Please enter the Center and Radius:"); String center = "", radius = ""; // Center while (center.equals("")) { System.out.print("Center Value: --> "); center = userInput.nextLine(); if (!center.matches("\\d+")) { System.err.println("Invalid entry for CENTER! Try again..."); center = ""; } } // Radius while (radius.equals("")) { System.out.print("Radius Value: --> "); radius = userInput.nextLine(); if (!radius.matches("\\d+")) { System.err.println("Invalid entry for RADIUS! Try again..."); radius = ""; } } System.out.println(new StringBuffer("").append("Center = ").append(center) .append(" | Radius = ").append(radius)); /* Do what you want here with the numerical values contained within the String variables center and radius. */ } private void getCircleEndPoints() { System.out.println("Please enter the Circle End Points:"); String xx1 = "", yy1 = "", xx2 = "", yy2 = ""; // x1 while (xx1.equals("")) { System.out.print("x1 Value: --> "); xx1 = userInput.nextLine(); if (!xx1.matches("\\d+")) { System.err.println("Invalid entry for x1! Try again..."); xx1 = ""; } } // y1 while (yy1.equals("")) { System.out.print("y1 Value: --> "); yy1 = userInput.nextLine(); if (!yy1.matches("\\d+")) { System.err.println("Invalid entry for y1! Try again..."); yy1 = ""; } } // x2 while (xx2.equals("")) { System.out.print("x2 Value: --> "); xx2 = userInput.nextLine(); if (!xx2.matches("\\d+")) { System.err.println("Invalid entry for x2! Try again..."); xx2 = ""; } } // y2 while (yy2.equals("")) { System.out.print("y2 Value: --> "); yy2 = userInput.nextLine(); if (!yy2.matches("\\d+")) { System.err.println("Invalid entry for y2! Try again..."); yy2 = ""; } } System.out.println(new StringBuffer("").append("Circle End Points Suplied: (") .append(xx1).append(",").append(yy1).append("), (") .append(xx2).append(",").append(yy2).append(")")); /* Do what you want here with the numerical values contained within the String variables xx1, yy1, xx2, and yy2. */ } private void getCircleEndPoints_2() { System.out.println("Please enter the Circle End Points:" + ls + "Example Entries: 50 50 65 72 or" + ls + " 50,50,65,72 or" + ls + " 50, 50, 65, 72"); int xx1, yy1, xx2, yy2; String endPoints = ""; while (endPoints.equals("")) { System.out.print("End Points: --> "); endPoints = userInput.nextLine(); if (!endPoints.replaceAll("[ ,]","").matches("\\d+") || endPoints.contains(",") ? endPoints.split("\\s{0,},\\s{0,}").length != 4 : endPoints.split("\\s+").length != 4) { System.err.println("Invalid End Points Entry! Try again..."); endPoints = ""; } } String[] points = endPoints.contains(",") ? endPoints.split("\\s{0,},\\s{0,}") : endPoints.split("\\s+"); xx1 = Integer.parseInt(points[0]); yy1 = Integer.parseInt(points[1]); xx2= Integer.parseInt(points[2]); yy2 = Integer.parseInt(points[3]); System.out.println(new StringBuffer("").append("Circle End Points Suplied: (") .append(xx1).append(",").append(yy1).append("), (") .append(xx2).append(",").append(yy2).append(")")); /* Do what you want here with the numerical values contained within the int type variables xx1, yy1, xx2, and yy2. */ } }

代码中使用的正则表达式:

if (!choice.matches("[12]")) { >条件的

matches()
方法中此处包含的"[12]"表达式基本上表示以下含义:如果

choice变量中包含的提供的字符串为not “ 1”或“ 2”,然后输入if代码块。if (!center.matches("\\d+")) {

>条件的
matches()方法中此处包含的"\\d+"表达式基本上表示以下含义:如果
center
变量中包含的提供的字符串为

not字符串表示一个或多个数字(0到9),然后输入if代码块。 您可以在多个地方看到此表达式。endPoints.replaceAll("[ ,]", "")

此处包含在

replaceAll()

方法中的"[ ,]"表达式意味着从
endPoints字符串变量中包含的字符串替换
all
空格(“”)和逗号(,)。

endPoints.split("\\s+") 此处[中包含的"\\s+"表达式表示:根据一个或多个空格(“”)分隔符,将endPoints变量中包含的字符串拆分为一个字符串数组


endPoints.split("\\s{0,},\\s{0,}")

此处包含在[[split()

方法中的"\\s{0,},\\s{0,}"表达式意味着:将基于逗号(“,”)分隔符或any]包含在endPoints
变量中的字符串拆分为字符串数组逗号/空格组合定界符(例如:“,”或“,”或“,”或“,”),而不考虑逗号两侧的空格数(如果有)。它基本上涵盖了使用逗号分隔符的所有基础。
根据需要修改代码。

上一篇:在复杂对象的结构中,通过变量从深处对列表进行排序

下一篇:发生错误java.lang.NoClassDefFoundError:org / mockito / MockitoAnnotations $ Mock

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载