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

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

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

问题

写一个名为"larger"的方法接受两个浮点参数类型为double),如果第一个参数大于第二个参数则返回true否则返回false

我需要在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

boolean larger(double a, double b) {
    return a > b;
}

Demo:
===

public class Main {
    public static void main(String[] args) {
        // Tests
        System.out.println(larger(15, 10));
        System.out.println(larger(5, 10));
    }

    static boolean larger(double a, double b) {
        return a > b;
    }
}

Output:

true
false

Demo using user inputs:
======

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double d1, d2;
        System.out.print("Enter the first floating point number: ");
        d1 = in.nextDouble();
        System.out.print("Enter the second floating point number: ");
        d2 = in.nextDouble();
        System.out.println(larger(d1, d2));

        // An example of further processing based on the returned value
        System.out.println(larger(d1, d2) ? d1 : d2 + " is greater");
    }

    static boolean larger(double a, double b) {
        return a > b;
    }
}

A sample run:

Enter the first floating point number: 4.5
Enter the second floating point number: 9.2
false
9.2 is greater

Another sample run:

Enter the first floating point number: 50.5
Enter the second floating point number: 2.5
true
50.5 is greater
英文:
boolean larger(double a, double b) {
return a > b;
}

Demo:

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

Output:

true
false

Demo using user inputs:

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double d1, d2;
System.out.print("Enter the first floating point number: ");
d1 = in.nextDouble();
System.out.print("Enter the second floating point number: ");
d2 = in.nextDouble();
System.out.println(larger(d1, d2));
// An example of further processing based on the returned value
System.out.println(larger(d1, d2) ? d1 : d2 + " is greater");
}
static boolean larger(double a, double b) {
return a > b;
}
}

A sample run:

Enter the first floating point number: 4.5
Enter the second floating point number: 9.2
false
9.2 is greater

Another sample run:

Enter the first floating point number: 50.5
Enter the second floating point number: 2.5
true
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:

确定