Java 返回最大值的方法无效

Java 返回最大值的方法无效,java,methods,return,Java,Methods,Return,我不知道我做错了什么。有人想解释一下吗?它编译并运行,但我在第50行(返回行)不断得到一个错误 另外,如果我将下面的代码更改为“int max=(number1,number2)”,则会出现找不到符号错误。任何帮助都将不胜感激 使用int max=Math.max(num1,num2)这将返回最大值。使用int max=Math.max(num1,num2)这将返回最大值。使用int max=Math.max(num1,num2)这将返回最大值。使用int max=Math.max(num1,n

我不知道我做错了什么。有人想解释一下吗?它编译并运行,但我在第50行(返回行)不断得到一个错误

另外,如果我将下面的代码更改为“int max=(number1,number2)”,则会出现找不到符号错误。任何帮助都将不胜感激


使用
int max=Math.max(num1,num2)
这将返回最大值。

使用
int max=Math.max(num1,num2)
这将返回最大值。

使用
int max=Math.max(num1,num2)
这将返回最大值。

使用
int max=Math.max(num1,num2)
这将返回最大值。

在您的最大值(int num1,int num2)中没有逻辑,它只是在调用自身,因此永远不会结束:

使用方法如下:

import java.io.*;
import java.util.Scanner;

public class MethodLab {

   public static void main(String[] args) {
     // variable declarations for part 1
     String title;
     String firstName;
     String lastName;
     Scanner in = new Scanner(System.in);
     // prompt for input for part 1
     System.out.print("Enter a title:");
     title = in.next();
     System.out.print("Enter your first name:");
     firstName = in.next();     
     System.out.print("Enter a your last name:");
     lastName = in.next();
     // call the method for part 1
     greeting(title, firstName, lastName);

     // variable declarations for part 2
     int number1;
     int number2;
     // user prompts for part 2
     System.out.print("Enter first number:");
     number1 = in.nextInt();
     System.out.print("Enter second number:");
     number2 = in.nextInt();
     // call the method for part 2 inside the println statement
     System.out.println("The largest number is " + max(number1, number2));
 }

 /******************** greeting method goes here*********************/
   public static void greeting(String proper, String fname, String lname){
     System.out.println();
     System.out.printf("Dear " + proper +" "+ fname + " "+ lname);
     System.out.println();
   }






  /***********************end of method*************************/

  /******************** max method goes here*********************/
   public static int max(int num1,int num2){
     return num1 > num2 ? num1 : num2;
   }
在你的max(intnum1,intnum2)中没有逻辑,它只是调用自己,所以永远不会结束:

使用方法如下:

import java.io.*;
import java.util.Scanner;

public class MethodLab {

   public static void main(String[] args) {
     // variable declarations for part 1
     String title;
     String firstName;
     String lastName;
     Scanner in = new Scanner(System.in);
     // prompt for input for part 1
     System.out.print("Enter a title:");
     title = in.next();
     System.out.print("Enter your first name:");
     firstName = in.next();     
     System.out.print("Enter a your last name:");
     lastName = in.next();
     // call the method for part 1
     greeting(title, firstName, lastName);

     // variable declarations for part 2
     int number1;
     int number2;
     // user prompts for part 2
     System.out.print("Enter first number:");
     number1 = in.nextInt();
     System.out.print("Enter second number:");
     number2 = in.nextInt();
     // call the method for part 2 inside the println statement
     System.out.println("The largest number is " + max(number1, number2));
 }

 /******************** greeting method goes here*********************/
   public static void greeting(String proper, String fname, String lname){
     System.out.println();
     System.out.printf("Dear " + proper +" "+ fname + " "+ lname);
     System.out.println();
   }






  /***********************end of method*************************/

  /******************** max method goes here*********************/
   public static int max(int num1,int num2){
     return num1 > num2 ? num1 : num2;
   }
在你的max(intnum1,intnum2)中没有逻辑,它只是调用自己,所以永远不会结束:

使用方法如下:

import java.io.*;
import java.util.Scanner;

public class MethodLab {

   public static void main(String[] args) {
     // variable declarations for part 1
     String title;
     String firstName;
     String lastName;
     Scanner in = new Scanner(System.in);
     // prompt for input for part 1
     System.out.print("Enter a title:");
     title = in.next();
     System.out.print("Enter your first name:");
     firstName = in.next();     
     System.out.print("Enter a your last name:");
     lastName = in.next();
     // call the method for part 1
     greeting(title, firstName, lastName);

     // variable declarations for part 2
     int number1;
     int number2;
     // user prompts for part 2
     System.out.print("Enter first number:");
     number1 = in.nextInt();
     System.out.print("Enter second number:");
     number2 = in.nextInt();
     // call the method for part 2 inside the println statement
     System.out.println("The largest number is " + max(number1, number2));
 }

 /******************** greeting method goes here*********************/
   public static void greeting(String proper, String fname, String lname){
     System.out.println();
     System.out.printf("Dear " + proper +" "+ fname + " "+ lname);
     System.out.println();
   }






  /***********************end of method*************************/

  /******************** max method goes here*********************/
   public static int max(int num1,int num2){
     return num1 > num2 ? num1 : num2;
   }
在你的max(intnum1,intnum2)中没有逻辑,它只是调用自己,所以永远不会结束:

使用方法如下:

import java.io.*;
import java.util.Scanner;

public class MethodLab {

   public static void main(String[] args) {
     // variable declarations for part 1
     String title;
     String firstName;
     String lastName;
     Scanner in = new Scanner(System.in);
     // prompt for input for part 1
     System.out.print("Enter a title:");
     title = in.next();
     System.out.print("Enter your first name:");
     firstName = in.next();     
     System.out.print("Enter a your last name:");
     lastName = in.next();
     // call the method for part 1
     greeting(title, firstName, lastName);

     // variable declarations for part 2
     int number1;
     int number2;
     // user prompts for part 2
     System.out.print("Enter first number:");
     number1 = in.nextInt();
     System.out.print("Enter second number:");
     number2 = in.nextInt();
     // call the method for part 2 inside the println statement
     System.out.println("The largest number is " + max(number1, number2));
 }

 /******************** greeting method goes here*********************/
   public static void greeting(String proper, String fname, String lname){
     System.out.println();
     System.out.printf("Dear " + proper +" "+ fname + " "+ lname);
     System.out.println();
   }






  /***********************end of method*************************/

  /******************** max method goes here*********************/
   public static int max(int num1,int num2){
     return num1 > num2 ? num1 : num2;
   }
这种方法永远不会完成。这将一次又一次地调用max,直到堆栈空间用完为止。当您这样做时,它会抛出堆栈溢出错误。你应该把它改成

public static int max(int num1,int num2){
     int max = num1>num2?num1:numb2; // return the highest number.
     return max;
   }
编辑:既然你提到你是编程新手,让我再补充一些细节。如果在方法a中调用方法B,堆栈段中会保留一定的空间,以便控件知道方法a中的行位置,以便在完成方法B后继续执行。在您的情况下,max方法会一次又一次地调用max方法。这直接意味着堆栈段中为每个方法调用保留了越来越多的空间。在某种程度上,它耗尽了堆栈内存中的可用空间,最终会出现这样的堆栈溢出问题

通常,在大多数情况下,任何不修改输入而调用自身的方法都是一个危险信号。你的max方法就是这样

这种方法永远不会完成。这将一次又一次地调用max,直到堆栈空间用完为止。当您这样做时,它会抛出堆栈溢出错误。你应该把它改成

public static int max(int num1,int num2){
     int max = num1>num2?num1:numb2; // return the highest number.
     return max;
   }
编辑:既然你提到你是编程新手,让我再补充一些细节。如果在方法a中调用方法B,堆栈段中会保留一定的空间,以便控件知道方法a中的行位置,以便在完成方法B后继续执行。在您的情况下,max方法会一次又一次地调用max方法。这直接意味着堆栈段中为每个方法调用保留了越来越多的空间。在某种程度上,它耗尽了堆栈内存中的可用空间,最终会出现这样的堆栈溢出问题

通常,在大多数情况下,任何不修改输入而调用自身的方法都是一个危险信号。你的max方法就是这样

这种方法永远不会完成。这将一次又一次地调用max,直到堆栈空间用完为止。当您这样做时,它会抛出堆栈溢出错误。你应该把它改成

public static int max(int num1,int num2){
     int max = num1>num2?num1:numb2; // return the highest number.
     return max;
   }
编辑:既然你提到你是编程新手,让我再补充一些细节。如果在方法a中调用方法B,堆栈段中会保留一定的空间,以便控件知道方法a中的行位置,以便在完成方法B后继续执行。在您的情况下,max方法会一次又一次地调用max方法。这直接意味着堆栈段中为每个方法调用保留了越来越多的空间。在某种程度上,它耗尽了堆栈内存中的可用空间,最终会出现这样的堆栈溢出问题

通常,在大多数情况下,任何不修改输入而调用自身的方法都是一个危险信号。你的max方法就是这样

这种方法永远不会完成。这将一次又一次地调用max,直到堆栈空间用完为止。当您这样做时,它会抛出堆栈溢出错误。你应该把它改成

public static int max(int num1,int num2){
     int max = num1>num2?num1:numb2; // return the highest number.
     return max;
   }
编辑:既然你提到你是编程新手,让我再补充一些细节。如果在方法a中调用方法B,堆栈段中会保留一定的空间,以便控件知道方法a中的行位置,以便在完成方法B后继续执行。在您的情况下,max方法会一次又一次地调用max方法。这直接意味着堆栈段中为每个方法调用保留了越来越多的空间。在某种程度上,它耗尽了堆栈内存中的可用空间,最终会出现这样的堆栈溢出问题


通常,在大多数情况下,任何不修改输入而调用自身的方法都是一个危险信号。max方法就是这样。

int max=max(num1,num2)int max=max(num1,num2)int max=max(num1,num2)int max=max(num1,num2)