Java:实例访问和输入系统.in

Java:实例访问和输入系统.in,java,input,instance-variables,Java,Input,Instance Variables,我必须根据实例提问,并使用System.in输入 首先是实例: 我创建了一个名为woodenSword的实例变量,其中包含: Sword woodenSword=new Sword("Wooden Sword", 2); public Sword(String nameSword, int damageSword){ this.nameSword=nameSword; this.damageSword=damageSword; numberOfS

我必须根据实例提问,并使用System.in输入

首先是实例:

我创建了一个名为
woodenSword
的实例变量,其中包含:

Sword woodenSword=new Sword("Wooden Sword", 2);

public Sword(String nameSword, int damageSword){
        this.nameSword=nameSword;
        this.damageSword=damageSword;
        numberOfSwords++;
}
现在,我想使用这把剑,但是我该怎么做呢?我试过woodenSword.Damage剑,但显然不行。。。我认为这是因为我将变量设置为私有的,但我不想改变这一点,因为我在某个地方读到,最好将变量设置为私有的。(还有一个附带的问题:为什么保持变量的私有性更好?)

还有另一个问题:如何使用
System.in
获取输入?是否必须使用
System.in.toString()

我应该为此使用函数吗?从类中获取私有变量,并将该函数放入类中? 我想到了这个函数:

public static int getSwordStats(String nameSword){
    damageSword=nameSword.damageSword;

}
但是,我在
nameSword.damageSwarm
上遇到一个错误,我想它不明白这是一个变量。。。我怎样才能解决这个问题


希望你能帮助我

如果您需要从任何地方访问剑的伤害,那么您应该有一个公共方法返回此信息:

public int getDamage() {
    return this.damageSword;
}
(请注意,我将该方法命名为
getDamage剑()
,而不是
getDamage剑()
。该方法位于剑类中。将剑放在任何地方都是无用的,只会增加噪音,并使代码可读性降低)

关于你的第二个问题。是的,System.in是标准输入流。toString()不会返回用户输入的内容。阅读该类的javadoc,了解其工作原理。另请阅读,其中有一节关于命令行


关于最后一部分,您的代码尝试获得字符串的损坏。弦没有损坏。剑有伤害。

如果您需要从任何地方访问剑的伤害,那么您应该有一个公共方法返回此信息:

public int getDamage() {
    return this.damageSword;
}
(请注意,我将该方法命名为
getDamage剑()
,而不是
getDamage剑()
。该方法位于剑类中。将剑放在任何地方都是无用的,只会增加噪音,并使代码可读性降低)

关于你的第二个问题。是的,System.in是标准输入流。toString()不会返回用户输入的内容。阅读该类的javadoc,了解其工作原理。另请阅读,其中有一节关于命令行


关于最后一部分,您的代码尝试获得字符串的损坏。弦没有损坏。剑有伤害。

看来你的剑类负责跟踪3件事:

  • 每把剑的名字
  • 每把剑的伤害
  • 剑的总数
  • 前两个必须是成员变量,而最后一个必须是静态变量

    public class Sword {
        // private (so no one can access it but Sword)
        // static (so it belongs to the class Sword and not any specific Sword)
        private static int numberOfSwords = 0; // initialize to 0
        // public accessor method
        public static int getNumberOfSwords() {
            return numberOfSwords;
        }
        // notice there's no "setNumberOfSwords" - no one can come along and change
        // our data - it's 'encapsulated' in the class
    
        private String name; // private
        private int damage; // private
    
        public Sword(String name, int damage) {
            this.name = name;
            this.damage = damage;
            numberOfSwords++; // the only place we change number of swords
        }
    
        // this is how people outside Sword access the name
        // note that we could add a "setName(String name)" if we want
        public String getName() {
            return name;
        }
    
        // same with name - protect and provide an accessor
        public int getDamage() {
            return damage;
        }
    }
    
    在以后的课程中,您现在可以执行以下操作:

    Sword wood = new Sword("Wooden Sword", 2);
    System.out.println("wood's name is " + wood.getName());
    System.out.println("wood's damage is " + wood.getDamage());
    System.out.println("swords crafted so far: " + Sword.getNumberOfSwords());
    
    Sword mithril = new Sword ("Mithril Sword", 10);
    System.out.println("mithril 's name is " + mithril .getName());
    System.out.println("mithril 's damage is " + mithril .getDamage());
    System.out.println("swords crafted so far: " + Sword.getNumberOfSwords());
    
    哪个会打印

    Wooden Sword
    2
    1
    Mithril Sword
    10
    2
    
    关于你的第二个问题,我相信谷歌可以帮助你找到一些很棒的资源。作为一个简单的例子,以下是我所做的:

    // assumes you "import java.util.Scanner"
    Scanner sc = new Scanner(System.in);
    while(sc.hasNextLine()) {
        String line = sc.nextLine();
        System.out.println("You typed: " + line);
    }
    

    看来你的剑类负责跟踪3件事:

  • 每把剑的名字
  • 每把剑的伤害
  • 剑的总数
  • 前两个必须是成员变量,而最后一个必须是静态变量

    public class Sword {
        // private (so no one can access it but Sword)
        // static (so it belongs to the class Sword and not any specific Sword)
        private static int numberOfSwords = 0; // initialize to 0
        // public accessor method
        public static int getNumberOfSwords() {
            return numberOfSwords;
        }
        // notice there's no "setNumberOfSwords" - no one can come along and change
        // our data - it's 'encapsulated' in the class
    
        private String name; // private
        private int damage; // private
    
        public Sword(String name, int damage) {
            this.name = name;
            this.damage = damage;
            numberOfSwords++; // the only place we change number of swords
        }
    
        // this is how people outside Sword access the name
        // note that we could add a "setName(String name)" if we want
        public String getName() {
            return name;
        }
    
        // same with name - protect and provide an accessor
        public int getDamage() {
            return damage;
        }
    }
    
    在以后的课程中,您现在可以执行以下操作:

    Sword wood = new Sword("Wooden Sword", 2);
    System.out.println("wood's name is " + wood.getName());
    System.out.println("wood's damage is " + wood.getDamage());
    System.out.println("swords crafted so far: " + Sword.getNumberOfSwords());
    
    Sword mithril = new Sword ("Mithril Sword", 10);
    System.out.println("mithril 's name is " + mithril .getName());
    System.out.println("mithril 's damage is " + mithril .getDamage());
    System.out.println("swords crafted so far: " + Sword.getNumberOfSwords());
    
    哪个会打印

    Wooden Sword
    2
    1
    Mithril Sword
    10
    2
    
    关于你的第二个问题,我相信谷歌可以帮助你找到一些很棒的资源。作为一个简单的例子,以下是我所做的:

    // assumes you "import java.util.Scanner"
    Scanner sc = new Scanner(System.in);
    while(sc.hasNextLine()) {
        String line = sc.nextLine();
        System.out.println("You typed: " + line);
    }
    

    对于从系统控制台阅读,请参阅此;对于从系统控制台阅读,请参阅此。我添加了一些注释,用于解释我在剑类课程中所做的决定。我添加了一些注释,用于解释我在剑类课程中所做的决定