[英]changing an already established double
I have a Java class in school and am rather new to programming. 我在学校里有一门Java课,对编程还是很陌生。 I need to write a short code.
我需要写一个简短的代码。 So far, I just have a little bit.
到目前为止,我只有一点点。 I have a player class.
我有一个球员课。 I'd like to be able to do something where the player's stamina decreases as they walk.
我希望能够做一些球员走路时耐力降低的事情。 But I don't know how to change the health without playing a new player.
但是我不知道如何在不玩新玩家的情况下改变健康状况。
Here is my code. 这是我的代码。 There's probably a lot wrong with it.
可能有很多错误。 This is my third day of coding.
这是我编码的第三天。
import java.io.*;
import java.util.Scanner;
public class Controller {
public static void main(String[] args){
System.out.println("Type your name here:");
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
Player playerOne = new Player(input);
playerOne.setStrength(78);
playerOne.setHealth(99);
playerOne.setStamina(67);
playerOne.printplayer();
}
}
import java.util.Scanner;
public class Player {
public String name;
private String input;
private double health;
public double strength;
public double stamina;
public Player (String input){
name = input;
}
public void setHealth(double playHp){
health = playHp;
}
public void setStrength(double playStrn){
strength = playStrn;
}
public void setStamina(double playStam){
stamina = playStam;
}
public void printplayer(){
System.out.println("name : " + name );
System.out.println("Health :" + health);
System.out.println("Strength :" + strength);
System.out.println("Stamina :" + stamina);
}
}
You just need to set the new value using setter method of stamina in Player class. 您只需要使用Player类中的耐力的setter方法来设置新值。
Create getter method in your player class, like below:- 在播放器类中创建getter方法,如下所示:-
public double getStamina() {
return stamina;
}
Now see the Controller class, how I am reducing the stamina of playerOne by 1 for every 10 steps:- 现在查看Controller类,我如何每10个步骤将playerOne的耐力降低1:-
public static void main(String[] args){
System.out.println("Type your name here:");
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
//Created the Player object
Player playerOne = new Player(input);
playerOne.setStrength(78);
playerOne.setHealth(99);
playerOne.setStamina(67);
playerOne.printplayer();
//Now moving the player 100 steps/ rule is for every 10 step he loses 1 point in stamina
for(int step=0; step<100;step++){
if(step%10==0){
playerOne.setStamina(playerOne.getStamina()-1);
}
}
playerOne.printplayer();
scan.close();
}
Take this as an hint. 以此为提示。
If I understand what you are trying to do. 如果我了解您要做什么。 You need to create a Timer that will constantly check the status of your player and can give a constant decrease to your player's stamina.
您需要创建一个计时器,该计时器将不断检查播放器的状态,并可以不断减小播放器的耐力。
You will need a way to know if your player is walking or not 您将需要一种方法来了解您的玩家是否在走路
And a way to change that status. 以及一种更改状态的方法。 Maybe a KeyListener or a MouseListener
可能是KeyListener或MouseListener
For this kind of Java coding I recommend zetcode 对于这种Java编码,我建议使用zetcode
I would suggest you to write a method in Player class as 我建议您在Player类中编写一个方法
private void reduceStamina() {
setStamina(stamina-1);
}
Then you can call this method from printPlayer before the codes that display output. 然后,您可以在显示输出的代码之前从printPlayer调用此方法。
reduceStamina()
This will reduce the value of stamina for player each time you print a player. 每次打印播放器时,这将降低播放器的耐力值。 Hope that helps
希望能有所帮助
General Tips: Always keep your attributes/fields in a class private, and only access them using by writing getters and setters for each field/attribute. 一般提示:始终将您的属性/字段放在一个私有类中,并且只能通过为每个字段/属性编写getter和setter来使用它们来访问它们。 Your health field is private, good;
您的健康领域是私人的,良好的; your stamina field is public, bad.
您的耐力场是公开的,不好。 Also you don't need the "input" as a field, since it is only used inside the constructor and is already defined in the parameter.
同样,您也不需要“输入”作为字段,因为它仅在构造函数内部使用,并且已经在参数中定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.