【问题标题】:Scanner + TesterClass? [closed]Scanner + TesterClass? [关闭]
【发布时间】:2017-03-13 03:22:46
【问题描述】:

为此,我必须在 Java 中创建一个程序,要求用户输入三角形的所有三个点,然后我必须找到边和面积。所有的数学运算都必须与测试类分开完成,我会在测试类中提示用户问题...
- 我如何要求用户在测试程序类中输入一些内容,但在原始程序中取回这些整数?

【问题讨论】:

  • 欢迎来到 SO。请阅读How to Askminimal reproducible example。谢谢。
  • @OldProgrammer 对不起,这是我的第一篇文章,我会在未来尝试更好地格式化所有内容! :) 谢谢

标签: java java.util.scanner area


【解决方案1】:
Scanner s = new Scanner();
double x1 = s.nextDouble();
double y1 = s.nextDouble();

等等……

并在您创建的函数中传递这些变量。 希望对你有帮助。

谢谢

【讨论】:

    【解决方案2】:

    在测试器类的main 方法中,您可以创建一个非测试器类的实例,其中包含您进行数学运算的函数,即:

    TriangleMath tMath = new TriangleMath();
    // where TriangleMath is the name of the other class, and "tMath" is
    // an instance of it. then:
    
    Scanner keyboard = new Scanner(System.in);
    int point1 = (int) keyboard.nextLine().charAt(0);
    int point2 = (int) keyboard.nextLine().charAt(0);
    int point3 = (int) keyboard.nextLine().charAt(0);
    int area = tMath.area(point1, point2, point3);
    

    在这里,您正在创建一个包含所有数学函数和东西的类的对象,然后在测试器类的 main 方法中获取输入,然后将输入传递给实例的 area 函数属于TriangleMath 类 (tMath)。 .charAt(0) 将其转换为 char(int) 将其转换为 int

    希望对我有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-03
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多