较大的方法程序。请阅读内容,以便理解我需要编写基本Java代码。

huangapple go评论160阅读模式
英文:

Larger Method Program. Please read the body so you understand what it is I need to write a basic java code for

问题

  1. 写一个名为"larger"的方法接受两个浮点参数类型为double),如果第一个参数大于第二个参数则返回true否则返回false
  2. 我需要在Java中编写代码非常基础的Java不复杂),以实现这个任务
英文:

Write a method called larger that accepts two floating-point parameters (of type double) and returns true if the first parameter is greater than the second, and false otherwise.

I need help writing code in java (very basic java, not complicated) that accomplishes this task.

答案1

得分: 0

  1. boolean larger(double a, double b) {
  2. return a > b;
  3. }
  4. Demo:
  5. ===
  6. public class Main {
  7. public static void main(String[] args) {
  8. // Tests
  9. System.out.println(larger(15, 10));
  10. System.out.println(larger(5, 10));
  11. }
  12. static boolean larger(double a, double b) {
  13. return a > b;
  14. }
  15. }
  16. Output:
  17. true
  18. false
  19. Demo using user inputs:
  20. ======
  21. import java.util.Scanner;
  22. public class Main {
  23. public static void main(String[] args) {
  24. Scanner in = new Scanner(System.in);
  25. double d1, d2;
  26. System.out.print("Enter the first floating point number: ");
  27. d1 = in.nextDouble();
  28. System.out.print("Enter the second floating point number: ");
  29. d2 = in.nextDouble();
  30. System.out.println(larger(d1, d2));
  31. // An example of further processing based on the returned value
  32. System.out.println(larger(d1, d2) ? d1 : d2 + " is greater");
  33. }
  34. static boolean larger(double a, double b) {
  35. return a > b;
  36. }
  37. }
  38. A sample run:
  39. Enter the first floating point number: 4.5
  40. Enter the second floating point number: 9.2
  41. false
  42. 9.2 is greater
  43. Another sample run:
  44. Enter the first floating point number: 50.5
  45. Enter the second floating point number: 2.5
  46. true
  47. 50.5 is greater
英文:
  1. boolean larger(double a, double b) {
  2. return a > b;
  3. }

Demo:

  1. public class Main {
  2. public static void main(String[] args) {
  3. // Tests
  4. System.out.println(larger(15, 10));
  5. System.out.println(larger(5, 10));
  6. }
  7. static boolean larger(double a, double b) {
  8. return a > b;
  9. }
  10. }

Output:

  1. true
  2. false

Demo using user inputs:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner in = new Scanner(System.in);
  5. double d1, d2;
  6. System.out.print("Enter the first floating point number: ");
  7. d1 = in.nextDouble();
  8. System.out.print("Enter the second floating point number: ");
  9. d2 = in.nextDouble();
  10. System.out.println(larger(d1, d2));
  11. // An example of further processing based on the returned value
  12. System.out.println(larger(d1, d2) ? d1 : d2 + " is greater");
  13. }
  14. static boolean larger(double a, double b) {
  15. return a > b;
  16. }
  17. }

A sample run:

  1. Enter the first floating point number: 4.5
  2. Enter the second floating point number: 9.2
  3. false
  4. 9.2 is greater

Another sample run:

  1. Enter the first floating point number: 50.5
  2. Enter the second floating point number: 2.5
  3. true
  4. 50.5 is greater

huangapple
  • 本文由 发表于 2020年4月8日 07:49:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/61091221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定