You can use “input.nextInt()” to get an integer input and return it in a method in Java.

huangapple go评论149阅读模式
英文:

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(&quot;Age:&quot; + 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 &quot;Name:&quot; + this.name + &quot;. Age:&quot; + this.age; 
    }
}

And then:

int age = input.nextInt();
String name = input.next();
Person person1 = new Person(age, name);
System.out.println(person1.toString());   

huangapple
  • 本文由 发表于 2020年8月14日 19:42:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63412105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定