英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论