英文:
How can i use input(int) in get and return method in Java?
问题
所以我刚开始学习Java,计划深入学习,然后我想问这个问题,因为我卡住了,想学得更多。
我正在尝试使用获取和返回方法。
我想以一种输入的方式来做这个,但是我不能使用
**"int age = person1.GetAge()
System.out.println("Age:" + age)**,因为它会变成两个变量(age)。
我希望你理解我的问题,我知道这听起来很愚蠢,但我想学习 xD。
CODE:
//未完成
//无法使用getAge,不知道如何;尽管我给出的公式不管用,yrsleft的值始终为65
package practice;
import java.util.Scanner;
class person{
String name;
int age;
void speak() {
System.out.print("Hello my name is:" + name);
}
int retire() {
int yrsleft = 65 - age;
return yrsleft;
}
int GetAge() {
return age;
}
}
public class curiosity1{
public static void main(String[]args) {
person person1 = new person();
Scanner input = new Scanner(System.in);
System.out.print("What is your name:");
String name = input.next();
System.out.print("What is your age:");
int age = input.nextInt();
//person1.name = "John";
//person1.age = 30;
System.out.println("Name: " + name);
int age = person1.GetAge();
System.out.println("Age:" + age);
int years = person1.retire();
System.out.println("Years till retirement:" + years);
}
}```
英文:
so im just starting to study java and planning to learn it in-depth and then i wanna ask this thing because im stuck and to learn more.
im trying to use the get and return method.
i wanted to do this in an input way but i cant use the
"int age = person1.GetAge()
System.out.println("Age:" + age) because it will become 2 variables (age)
i hope you understand my question and i know it sounds stupid but i wanna learn xD.
CODE:
//unfinished
//cant use the getAge, no idea how; the value in yrsleft is always 65 despite of the formula that i give
package practice;
import java.util.Scanner;
class person{
String name;
int age;
void speak() {
System.out.print("Hello my name is:" + name);
}
int retire() {
int yrsleft = 65 - age;
return yrsleft;
}
int GetAge() {
return age;
}
}
public class curiosity1{
public static void main(String[]args) {
person person1 = new person();
Scanner input = new Scanner(System.in);
System.out.print("What is your name:");
String name = input.next();
System.out.print("What is your age:");
int age = input.nextInt();
//person1.name = "John";
//person1.age = 30;
System.out.println("Name: " + name);
int age = person1.GetAge();
System.out.println("Age:" + age);
int years = person1.retire();
System.out.println("Years till retirement:" + years);
}
}```
</details>
# 答案1
**得分**: 0
我希望我正确理解了你的问题,你想要这样做?
```java
person1.age = input.nextInt();
person1.name = input.next();
System.out.println("Age:" + person1.getAge());
或者你可以在你的类中重写toString()
方法(因为所有的Java类都继承自Object
,该类拥有这个方法),以字符串形式表示你的对象。同时,你应该为你的Person
类创建一个构造函数。
class Person {
int age;
String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
// 你的其他方法等等
@Override
public String toString() {
return "Name:" + this.name + ". Age:" + this.age;
}
}
然后:
int age = input.nextInt();
String name = input.next();
Person person1 = new Person(age, name);
System.out.println(person1.toString());
英文:
I hope I understood your question correctly, you want to do this?
person1.age = input.nextInt();
person1.name = input.next();
System.out.println("Age:" + person1.getAge());
Or you can override toString()
method in your class (since all java classes are inherited from Object
, which has this method) to represent your object with a string. Also, you should create a constructor for your Person
class.
class Person { // always start class name with a capital letter
int age;
String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
// Your methods and etc.
@Override
public String toString() {
return "Name:" + this.name + ". Age:" + this.age;
}
}
And then:
int age = input.nextInt();
String name = input.next();
Person person1 = new Person(age, name);
System.out.println(person1.toString());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论