gpt4 book ai didi

java - 方法: Identifier expected

转载 作者:行者123 更新时间:2023-12-01 19:46:49 24 4
gpt4 key购买 nike

我尝试在其他线程中寻找答案,但我似乎仍然无法弄清楚。我收到错误:预期在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com