java - 方法: Identifier expected

标签 java

我尝试在其他线程中寻找答案,但我似乎仍然无法弄清楚。我收到错误:预期在 private static int getUserMHR(age) 行。这是我的代码:

import java.util.Scanner;

public class Assignment2
{
//class variables
//a scanner object to read input from the keyboard - can be used by any method
static Scanner input = new Scanner(System.in);

public static void main(String[] args)
{
  String name; // the user's name
  int age; // the user's age
  String gender; // the user's gender
  float height; // the users height in inches
  float weight; // the users weight in pounds
  int mhr; //the user's maximum heart rate
  int thr; //the user's target heart rate
  float bmi; //the user's body mass index

  //display the purpose of the program
  displayPurpose();
  //get the user's name
  name = getUserName();
  //get the user's age
  age = getUserAge();
  //get the user's gender
  gender = getUserGender();
  //get the user's height
  height = getUserHeight();
  //get the user's weight
  weight = getUserWeight();
  //get the user's maximum heart private
  mhr = getUserMHR();

   System.out.println(mhr);

}// of main

private static void displayPurpose()
{
    System.out.println("****************************************************************************************************");
    System.out.println("            This application takes the user's name, age, gender, height and weight");
    System.out.println("It then display's the user's maximum heart rate, target heart rate, BMI (Body Mass Index), and their");
    System.out.println("                                              weight class");
    System.out.println("****************************************************************************************************");
}//displayPurpose

private static String getUserName()
{
  //local variable
  String username;

  //ask for user's name
  System.out.println("Please input your name: ");
  //input the user's name
  username = input.nextLine();

  //return user's name
  return username;

}//getUserName();

private static int getUserAge()
{
  //local variable
  int userage;

  //ask for user's age
  System.out.println("Please input your age: ");
  //input the user's age
  userage = input.nextByte();

  //return user's age
  return userage;
}//getUserAge();

private static String getUserGender()
{
  //local variable
  String gender;

  //ask for user's gender
  System.out.println("Please input your gender (M or F): ");
  //input the user's gender
  gender = input.nextLine();

  //return user's gender
  return gender;
}//getUserGender();

private static float getUserHeight()
{
  //local variable
  float height;

  //ask for user's height
  System.out.println("Please input your height (in inches): ");
  //input the user's height
  height = input.nextFloat();

  //return user's height
  return height;
}//getUserHeight();

private static float getUserWeight()
{
  //local variable
  float weight;

  //ask for user's weight
  System.out.println("Please input your weight (in pounds): ");
  //input the user's weights
  weight = input.nextFloat();

  //return user's weight
  return weight;
}//getUserWeight();

private static int getUserMHR(age)
{
  int mhrcalc;
  mhrcalc = (age - 220); //calculate the user's maximum heart private

  return mhrcalc;
}

}

我尝试在年龄之前放置一个 int 并从实际方法中删除 int,但似乎没有任何效果。我感谢所提供的任何帮助。

最佳答案

您的方法 getUserMHR 有单个参数age。但在Java中你必须指定参数的类型。由于年龄很可能是一个整数,因此您可以只使用原始类型 int。

private static int getUserMHR( int age ) { //... }

关于java - 方法: Identifier expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52920578/

相关文章:

java - 无法从 Object 转换为 char

java - 让 "Calling method"等待数据 "Return"

java - 在 Java 中调用返回类型不是 void 的函数作为单独的语句

java - "m"在java代码中代表什么

java - Android - 粗体标签似乎不会影响 Strings.xml 中的 URL 文本

java - 按 {} 和 [] 拆分字符串

Java 集合缩减为 Map

方法签名中的 Java "params"?

java - spring data mongorepository.save E11000 有时出现重复键错误索引

java - 如何解决API版本相关障碍