如何编写一个JAVA方法来找到用户输入的最大值?
我是个十足的初学者。 我知道如何用“if”找到最大值,但我想用一种方法。问题是我如何保持上次最大值的记录如何编写一个JAVA方法来找到用户输入的最大值?,java,methods,max,Java,Methods,Max,我是个十足的初学者。 我知道如何用“if”找到最大值,但我想用一种方法。问题是我如何保持上次最大值的记录 public class Max { public static double Max (double score){ double max = 0; if (score > max) max = score; return max; } public static void main
public class Max
{
public static double Max (double score){
double max = 0;
if (score > max)
max = score;
return max;
}
public static void main (String [] arg){
Scanner scan = new Scanner (System.in);
double score = 0;
do{
System.out.print("Enter the scores (-1 to end input) > ");
score = scan.nextDouble();
}while (score >= 0);
System.out.println("Highest score: " + Max(score));
}
}
您可以执行以下操作,但可以稍微修改您的方法:
public static double Max (double score, double max){
if (score > max)
max = score;
return max;
}
public static void main (String [] arg){
Scanner scan = new Scanner (System.in);
double score = 0;
double max = 0;
do{
System.out.print("Enter the scores (-1 to end input) > ");
score = scan.nextDouble();
max = Max(score, max);
}while (score >= 0);
System.out.println("Highest score: " + Max(score));
}
基本上,函数返回两个参数的最大值。它将持续将当前最大值与输入值进行比较,并将较大的数值设置为变量
max
。您将需要一个holder变量(在我的示例中,它是在double max=0;
中生成的,无论您使用何种方法。您可以执行以下操作,但需要稍微修改您的方法:
public static double Max (double score, double max){
if (score > max)
max = score;
return max;
}
public static void main (String [] arg){
Scanner scan = new Scanner (System.in);
double score = 0;
double max = 0;
do{
System.out.print("Enter the scores (-1 to end input) > ");
score = scan.nextDouble();
max = Max(score, max);
}while (score >= 0);
System.out.println("Highest score: " + Max(score));
}
基本上,您的函数返回两个参数中的最大值。它会将当前最大值与输入值进行比较,并将较大的数值设置为变量Max
。您将需要一个holder变量(在我的示例中,它是在double Max=0;
中生成的,无论您使用何种方法。您可以尝试此方法
public class Max {
public static double max(List<Double> score) {
double max = 0;
for (int i = 0; i < score.size(); i++) {
if (score.get(i) > max) {
max = score.get(i);
}
}
return max;
}
public static void main(String[] arg) {
Scanner scan = new Scanner(System.in);
List<Double> scores = new ArrayList<>();
Double score;
do {
System.out.print("Enter the scores (-1 to end input) > ");
score = scan.nextDouble();
scores.add(score);
} while (score >= 0);
System.out.println("Highest score: " + max(scores));
}
}
公共类最大值{
公共静态双倍最大值(列表分数){
双最大值=0;
对于(int i=0;i最大值){
max=得分。get(i);
}
}
返回最大值;
}
公共静态void main(字符串[]arg){
扫描仪扫描=新扫描仪(System.in);
列表分数=新的ArrayList();
双倍得分;
做{
系统输出打印(“输入分数(-1到结束输入)>”;
分数=scan.nextDouble();
分数。添加(分数);
}而(得分>=0);
System.out.println(“最高分数:+max(分数));
}
}
您可以试试这个
public class Max {
public static double max(List<Double> score) {
double max = 0;
for (int i = 0; i < score.size(); i++) {
if (score.get(i) > max) {
max = score.get(i);
}
}
return max;
}
public static void main(String[] arg) {
Scanner scan = new Scanner(System.in);
List<Double> scores = new ArrayList<>();
Double score;
do {
System.out.print("Enter the scores (-1 to end input) > ");
score = scan.nextDouble();
scores.add(score);
} while (score >= 0);
System.out.println("Highest score: " + max(scores));
}
}
公共类最大值{
公共静态双倍最大值(列表分数){
双最大值=0;
对于(int i=0;i最大值){
max=得分。get(i);
}
}
返回最大值;
}
公共静态void main(字符串[]arg){
扫描仪扫描=新扫描仪(System.in);
列表分数=新的ArrayList();
双倍得分;
做{
系统输出打印(“输入分数(-1到结束输入)>”;
分数=scan.nextDouble();
分数。添加(分数);
}而(得分>=0);
System.out.println(“最高分数:+max(分数));
}
}
您必须将输入值保存在列表中
。然后将列表
对象作为MAX
方法的输入参数传递
public static double max(List<Double> score) {
double max = 0;
for (int i = 0; i < score.size(); i++) {
if (score.get(i) > max) {
max = score.get(i);
}
}
return max;
}
public static void main(String[] arg) {
Scanner scan = new Scanner(System.in);
List<Double> scores = new ArrayList<>();
Double score;
do {
System.out.print("Enter the scores (-1 to end input) > ");
score = scan.nextDouble();
scores.add(score);
} while (score >= 0);
System.out.println("Highest score: " + max(scores));
}
公共静态双倍最大值(列表分数){
双最大值=0;
对于(int i=0;i最大值){
max=得分。get(i);
}
}
返回最大值;
}
公共静态void main(字符串[]arg){
扫描仪扫描=新扫描仪(System.in);
列表分数=新的ArrayList();
双倍得分;
做{
系统输出打印(“输入分数(-1到结束输入)>”;
分数=scan.nextDouble();
分数。添加(分数);
}而(得分>=0);
System.out.println(“最高分数:+max(分数));
}
您必须将输入值保存在列表中
。然后将列表
对象作为MAX
方法的输入参数传递
public static double max(List<Double> score) {
double max = 0;
for (int i = 0; i < score.size(); i++) {
if (score.get(i) > max) {
max = score.get(i);
}
}
return max;
}
public static void main(String[] arg) {
Scanner scan = new Scanner(System.in);
List<Double> scores = new ArrayList<>();
Double score;
do {
System.out.print("Enter the scores (-1 to end input) > ");
score = scan.nextDouble();
scores.add(score);
} while (score >= 0);
System.out.println("Highest score: " + max(scores));
}
公共静态双倍最大值(列表分数){
双最大值=0;
对于(int i=0;i最大值){
max=得分。get(i);
}
}
返回最大值;
}
公共静态void main(字符串[]arg){
扫描仪扫描=新扫描仪(System.in);
列表分数=新的ArrayList();
双倍得分;
做{
系统输出打印(“输入分数(-1到结束输入)>”;
分数=scan.nextDouble();
分数。添加(分数);
}而(得分>=0);
System.out.println(“最高分数:+max(分数));
}
除了提出的想法外,请注意,如果值小于0,则会将其进行比较,或者在不应该的情况下将其添加到列表中,例如,如果希望找到子对象,则会失败。
除了找到较大或较小的一个可以求助于溪流。
公共静态双倍最大值(列表分数){
返回scores.stream().mapToDouble(i->i.max().getAsDouble();
}
公共静态void main(字符串[]args){
扫描器t=新扫描器(System.in);
列表分数=新的ArrayList();
双值=0;
while(true){
值=t.nextDouble();
如果(value与提出的想法不同,请小心,如果该值小于0,则会将其进行比较或在不应该的情况下将其添加到列表中,例如,如果希望找到子对象,则会失败。
除了找到较大或较小的一个可以求助于溪流。
公共静态双倍最大值(列表分数){
返回scores.stream().mapToDouble(i->i.max().getAsDouble();
}
公共静态void main(字符串[]args){
扫描器t=新扫描器(System.in);
列表分数=新的ArrayList();
双值=0;
while(true){
值=t.nextDouble();
如果(值)不要忘记将我的答案标记为已接受不要忘记将我的答案标记为已接受