【问题标题】:Java method to format String fields格式化字符串字段的Java方法
【发布时间】:2011-02-25 04:54:42
【问题描述】:

类 Person 持有个人数据

它的构造函数接收3个参数,两个代表名字和姓氏的字符串和一个代表年龄的int

public Person(String firstName, String lastName, int age) 

它的方法getName 没有参数,返回一个String,格式为“姓,名”
它的方法getAge 不接受任何参数并返回一个表示当前年龄的int
它的方法birthday将年龄值加1并返回新的年龄值

创建类 Person 并将整个类粘贴到下面的文本框中

    public class Person
{   
       private String Firstname;
        private String Lastname;
        private String Name1;
        private int getAge;
        private int birthday;
        private int newAge;
        private int age1;


public Person(String first, String last, int age)
    {
      Firstname = first;
      Lastname = last;
        age1 = age;


    }
       public String getName()
           {
       String getName = "Lastname , Firstname";
       Name1 = Lastname + ", "  + Firstname;
       return Name1;

    }
        public int getAge()
        {
       return age1;

    }

        public int birthday()
        {

       age1++;
       return age1;


    }

}

我已经解决了,谢谢你们的帮助!

【问题讨论】:

  • 你误解了Java。
  • 你现在能给自己的最好帮助不是来自stackoverflow。拿起你的教科书,阅读、学习、练习。
  • 代码中只有3行是语法正确的。程序本身是完全错误的。正如@polygenelubricants 所说,读一本教科书。 Bruce Eckel 的“Thinking in Java”应该是一篇不错的文章,可通过bruceeckel.com在线免费获得
  • @Chinmay:这是基本功课。他应该更好地阅读课程材料,更好地听导师的话。现在不是推荐一本厚书的合适时机。
  • 编写更具描述性的主题行将有助于您的信息从 49795 个其他 Java 问题中脱颖而出。 “编程帮助”不会让那些正在寻找并可能获得您需要帮助的人知道种类的帮助。 在这里发帖的每个人都需要“编程帮助”。

标签: java


【解决方案1】:

你似乎不明白什么是方法。因为听起来你是在为一堂课做这件事,所以你可能应该复习你的教科书或与你的老师讨论。

你也可以试试official Java tutorial,尤其是第一部分,关于类。

【讨论】:

    【解决方案2】:

    您使用getName 作为变量,但它应该是一个方法

    public String getName() {
        // Implement accordingly.
    }
    

    另请参阅有关该主题的 Sun 教程:Defining Methods。然而,我想知道您是否在课程期间还没有学习/获得这些信息。我会再次咨询你的导师。

    【讨论】:

      【解决方案3】:

      您发布的代码并没有像您想象的那样定义方法。查看基本的 Java 教科书,了解如何定义方法以及如何定义和分配变量。

      【讨论】:

        【解决方案4】:

        我会向你展示一点,只是为了让你开始。

        您需要在类中声明变量以保存传递给构造函数的值。然后你需要一个单独的方法来返回格式化的名字。

        public class Person
        {
            // all of the methods inside this class will have access to these variables
            private String first;
            private String last;
            private int age;
        
            public Person(String first, String last, int age)
            {
                this.first = first;
                // this.first refers to the "private String first" declared in the class
                // first refers to the local variable passed in as a parameter.
                // now you write the rest of the constructor
            }
        
            public String getName() {
                // what can you do with first and last here to return the formatted name?
            }
        
            // TODO: Add other methods to return age, etc.
        }
        

        【讨论】:

        • 很好的例子。研究这个应该会有所帮助。
        • 获取它并使其编译并工作-然后一次添加一行并测试为您添加的每一行编译它-这样您就知道何时添加不不工作。 Eclipse 还会立即(在您键入时)告诉您出现了问题。
        • @Bill K:很好的建议。这提醒了我,当编译失败时,BlueJ 会使用 Windows“坏哔”声。我曾经不得不告诉每个学生不要害怕那种声音。您可以尝试编译任意多次。
        • blueJ 很棒。实际上我有时会在工作中使用它(原型设计)——如果它在 UML 中有更好的编辑器和更多的细节,它可能是最好的环境。
        【解决方案5】:

        Java 不是 javascript,方法不是变量。 getName 看起来像

        public String getName(){
          return this.lastName + ", " + this.firstName;
        }
        

        阅读Java tutorial

        【讨论】:

          【解决方案6】:

          您可以修改您的代码以使其能够编译,然后您就可以开始了解如何改进它。

          所以,这可能是一种私有方法:

             getAge = age + 1; 
             return getAge; 
          

          这是另一个私有方法:

             birthday = age + 1; 
             newAge = birthday; 
             return newAge; 
          

          所以你会有类似的东西:

             getName = "Lastname, Firstname";  
             System.out.print(last + first);  
          
             System.out.print(myageinc(age));  
          
             mydobinc(birthday);
          

          一旦你把所有东西都分解成小函数,你就可以更容易地弄清楚你在做什么。

          因为这是一个家庭作业,所以我试图尽可能少地展示新代码,但希望这会对你有所帮助。

          此外,您需要定义变量,告诉 Java 它是什么类型的变量,然后才能分配给它。

          【讨论】:

            【解决方案7】:

            这是另一个与您最初想做的有点相似的选项。可以使用 String.format 方法指定格式字符串,然后传入要替换的值。

            public String getName(){
              return String.format("%s, %s", lastName, firstName);
            }
            

            【讨论】:

              【解决方案8】:

              在 BlueJay 中,您使用的是 Windows 版还是 Mac 版? 无论哪种方式,我都会通过 Mac 版本并希望它能正常工作。

              创建一个新类并输入 Person 作为类。

              通过右键单击类并单击打开编辑器来调出编辑器窗口。

              您的课程应具有以下格式。

              /*** Write a description of class Person here.
               * 
               * @author (your name) 
               * @version (a version number or a date)
               */
               public class Person
               {
              // instance variables - replace the example below with your own
              private int x;
              
              /**
               * Constructor for objects of class Person
               */
              public Person()
              {
                  // initialise instance variables
                  x = 0;
              }
              
              /**
               * An example of a method - replace this comment with your own
               * 
               * @param  y   a sample parameter for a method
               * @return     the sum of x and y 
               */
              public int sampleMethod(int y)
              {
                  // put your code here
                  return x + y;
              }
              

              }

              你说的第一个要求是

              • 它的构造函数接收 3 个参数,两个 String 代表名字和姓氏,一个 int 代表年龄

              你在代码中做得好的地方

              public Person(String firstName, String lastName, int age)
              

              所以那里没有变化。 :D

              不是

              public Person(String first, String last, int age)
              

              [我们可以改用这个,但不要混淆,似乎对方希望你使用第一个]

              虽然我们希望这个类获取这 3 个参数所以我们需要 1)有私有变量来保存这些值 2) 在构造函数中赋值。

              回顾一下 BlueJ 用这些台词带来的东西

              // instance variables - replace the example below with your own
              private int x;
              
              /**
               * Constructor for objects of class Person
               */
              public Person()
              {
                  // initialise instance variables
                  x = 0;
              }
              

              我们希望在您的构造函数中进行更改以及附加更改 1) 和 2)

                  // instance variables - replace the example below with your own
              // private int x; replace blueJ sample variable
              private String firstName; //not the same as the one given in the constructor
              private String lastName; //not the same as the one given in the constructor
              private int age; //not the same as the one given in the constructor
              
              /**
               * Constructor for objects of class Person
               */
              public Person(String firstName, String lastName, int age)
              {
                  // initialise instance variables
                  //x = 0; replace BlueJ sample with
                  this.firstName = firstName;
                  this.lastName = lastName;
                  this.age = age;
              }
              

              好的,所以只要呼吸并观察它。我们有两组每个变量!一个是用于类的,另一个是提供给构造函数的。现在要解释所有内容太多了 :( ,所以你需要阅读一本 Java 书籍 [只是前几章......在观看 24 季第 1 季时阅读它],大多数书籍都带有这个 Person 类示例。

              好吧,所以取消了第一个要求。让我们看看第二个。

              -它的方法getName没有参数,返回一个格式为“Lastname, Firstname”的String

              所以它是一个方法,为简洁起见,跳转到本节

              http://java.sun.com/docs/books/tutorial/java/javaOO/methods.html

              如果我这样做,它会更好地解释它。好的,我给你一点时间。

              你读了吗?不 ?去读吧Grr! :( 这很重要

              好的,现在您已经完成了以下操作。

              /**
               * The getName Method - put in a little description here
               * 
               * @return     the String with the format "Lastname, Firstname"
               */
              public String getName()
              {
                  // put your code here
                  // I did and this is called string concatenation in java
                  // Google it:"string concatenation in java"
              
                  return this.lastName + ", " + this.firstName;
              }
              

              所以我制作了一种类似于 BlueJ 制作的方法以及它的外观。它获取名称(this.lastName 和 this.firstName NOT lastName 和 firstName 也可以,但不要让我们自己混淆 k?)

              BlueJ 中的完整代码

              /**
               * Write a description of class Person here.
               * 
               * @user208639 (Is that your real name ?) 
               * @version (a version number or a date)
               */
               public class Person
               {
               // instance variables - replace the example below with your own
               // private int x; replace blueJ sample variable
               private String firstName;
               private String lastName;
               private int age;
              
              /**
               * Constructor for objects of class Person
               */
              public Person(String firstName, String lastName, int age)
              {
                  // initialise instance variables
                  //x = 0; replace BlueJ sample with
                  this.firstName = firstName;
                  this.lastName = lastName;
                  this.age = age;
              }
              
              /**
               * The getName Method - put in a little description here
               * 
               * @return     the String with the format "Lastname, Firstname"
               */
              public String getName()
              {
                  // put your code here
                  // I did and this is called string concatenation in java
                  // Google it:"string concatenation in java"
                  return this.lastName + ", " + this.firstName;
              }
              
              /**
               * An example of a method - replace this comment with your own
               * 
               * @param  y   a sample parameter for a method
               * @return     the sum of x and y 
               */
              public int sampleMethod(int y)
              {
                  // put your code here
                  return x + y;
              }
              

              }

              你的方法正确吗? 有点 ?如果您对编码、变量、数据类型和方法一无所知,我会说这是一个合理的猜测 :) ...但您真的应该阅读一本 Java Intro 书。

              正确的程序?呐……这个 BlueJ 程序很奇怪。

              Google 的“NetBeans”是免费的。

              好的,西海岸的早餐时间已经过去了。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-11-08
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-02-20
                • 2010-12-26
                相关资源
                最近更新 更多