Java 如何计算当年与出生年份的差异

Java 如何计算当年与出生年份的差异,java,Java,我在编程课上有一个项目,我必须通过计算当前年份并减去出生年份来计算某人的年龄。然而,当我编写代码时,它一直给我“50.0分钟50.0=0.0”的答案 我应该怎么做才能解决这个问题?对于int,您必须使用nextInt()。java string charAt()方法在给定的索引号处返回一个char值。这不适合您当前的场景 import java.util.Scanner; class Main { String firstName; static int currentYear;

我在编程课上有一个项目,我必须通过计算当前年份并减去出生年份来计算某人的年龄。然而,当我编写代码时,它一直给我“50.0分钟50.0=0.0”的答案


我应该怎么做才能解决这个问题?

对于int,您必须使用
nextInt()
java string charAt()
方法在给定的索引号处返回一个
char值。这不适合您当前的场景

 import java.util.Scanner;

class Main {




String firstName;

static int currentYear;

static int birthYear;

double mathEquation;

Scanner lookAtKeyboard = new Scanner(System.in);


public void getFirstName()
{
System.out.println("What is your first name");
firstName =lookAtKeyboard.nextLine();
}

public  void getCurrentYear() {
System.out.println("What is the current year");
currentYear =lookAtKeyboard.nextInt();
}

public  void getBirthYear() {
System.out.println("What is your birth year");
birthYear =lookAtKeyboard.nextInt();
}

public void doMath(double birthYear, double currentYear) {
mathEquation = currentYear - birthYear;
System.out.println(currentYear +"minus"+ birthYear + "=" +mathEquation);

}



public static void main (String[] args) {

Main calculator = new Main();
calculator.getFirstName();
calculator.getCurrentYear();
calculator.getBirthYear();
calculator.doMath( currentYear,birthYear);

}


}

工作(我刚刚更改了类名,以便它可以在fiddle上工作)

对于int,您必须使用
nextInt()
java string charAt()
方法在给定的索引号处返回
char值。这不适合您当前的场景

 import java.util.Scanner;

class Main {




String firstName;

static int currentYear;

static int birthYear;

double mathEquation;

Scanner lookAtKeyboard = new Scanner(System.in);


public void getFirstName()
{
System.out.println("What is your first name");
firstName =lookAtKeyboard.nextLine();
}

public  void getCurrentYear() {
System.out.println("What is the current year");
currentYear =lookAtKeyboard.nextInt();
}

public  void getBirthYear() {
System.out.println("What is your birth year");
birthYear =lookAtKeyboard.nextInt();
}

public void doMath(double birthYear, double currentYear) {
mathEquation = currentYear - birthYear;
System.out.println(currentYear +"minus"+ birthYear + "=" +mathEquation);

}



public static void main (String[] args) {

Main calculator = new Main();
calculator.getFirstName();
calculator.getCurrentYear();
calculator.getBirthYear();
calculator.doMath( currentYear,birthYear);

}


}
工作(我刚刚更改了类名,以便它可以在fiddle上工作)

char(0)
返回ascii值“2”,因为您可能已将2019年作为当前年份输入

如果需要相同的输入行为(用户在每次输入后按enter键),可以将输入读取为字符串并将其解析为int:

public void getCurrentYear() {
    System.out.println("What is the current year");
    currentYear = Integer.parseInt(lookAtKeyboard.nextLine());
}

public void getBirthYear() {
    System.out.println("What is your birth year");
    birthYear = Integer.parseInt(lookAtKeyboard.nextLine());
}
(在main方法中调用doMath时,请参阅我对参数顺序的评论。

char(0)
返回ascii值“2”,因为您可能已将2019
输入为当前年份

如果需要相同的输入行为(用户在每次输入后按enter键),可以将输入读取为字符串并将其解析为int:

public void getCurrentYear() {
    System.out.println("What is the current year");
    currentYear = Integer.parseInt(lookAtKeyboard.nextLine());
}

public void getBirthYear() {
    System.out.println("What is your birth year");
    birthYear = Integer.parseInt(lookAtKeyboard.nextLine());
}

(在main方法中调用doMath时,请参阅我关于参数顺序的评论。

您给代码的输入是什么导致了此输出?请具体说明问题的原因。
charAt(0)
给您该字符串中第一个字符的整数表示形式(字符
2
为50)。您想要
Integer.parseInt(lookAtKeyboard.nextLine());
lookAtKeyboard.nextLine();
我正在尝试将输入设置为用户定义的出生和当前年份。在调用
doMath
时切换参数,否则您将始终得到负年龄。它应该是
计算器.doMatch(出生年份,当前年份)
是的,但是您使用了什么输入来获得输出“50.0分钟50.0=0.0”?您给代码的是什么输入导致此输出?请具体说明问题的原因。
charAt(0)
为您提供该字符串中第一个字符的整数表示形式(50表示字符
2
)。您想要
integer.parseInt吗(lookAtKeyboard.nextLine());
lookAtKeyboard.nextLine();
我正在尝试将输入设置为用户定义的出生和当前年份。在调用
doMath
时切换参数,否则您将始终得到负年龄。它应该是
calculator.doMatch(出生年份,当前年份)
没错,但是您使用了哪些输入来获得输出“50.0分钟50.0=0.0”?