我已经尝试在其他线程中在这里寻找答案,但是我似乎仍然找不到答案。我收到一个错误:应该在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,但似乎没有任何效果。感谢您的帮助。
答案 0 :(得分:2)
您的方法getUserMHR具有单个参数age。但是在Java中,您必须指定参数的类型。由于age很可能是整数,因此您只能使用基本类型int。
private static int getUserMHR( int age ) { //... }
答案 1 :(得分:1)
您需要指定age
是什么类型,这里是int
:
private static int getUserMHR(int age){
int mhrcalc;
mhrcalc = (age - 220); //calculate the user's maximum heart private
return mhrcalc;
}
当您要求一个参数时,在调用该方法时需要给它
mhr = getUserMHR(age);
此外,您不需要拆分声明和赋值的每一句(这里似乎您希望对评论进行规范,这样就可以了) int t; t=5; ==> int t=5;