×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

俺把code贴出来吧,大家给俺看看有错没有,否则俺就这样交了。A和B在一个目录下面。

本文发表在 rolia.net 枫下论坛A.

public class TelephoneFormat
{
private String telephone;
public TelephoneFormat(String t)
{
telephone = t;
}
public String getAreaCode()
{
return telephone.substring(0,3);
}
public String getExchangeNumber()
{
return telephone.substring(3,6);
}
public String getNumber()
{
return telephone.substring(6,10);
}
public void setTelephoneNumber(String t)
{
this.telephone = t;
}
}

B.
import java.io.*;

public class TestTelephoneFormat

{
public static void main (String[] args) throws IOException
{
TelephoneFormat myTelephone;
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter 10-digit telephone number:");
String input = console.readLine();
myTelephone = new TelephoneFormat(input);
myTelephone.setTelephoneNumber(input);
System.out.print("The area code is: = " + myTelephone.getAreaCode()+"\n");
System.out.print("The exchange is: = " + myTelephone.getExchangeNumber()+"\n");
System.out.print("The number is: = " + myTelephone.getNumber()+"\n");
System.out.print("The complete telephone number is: =" + "(" + myTelephone.getAreaCode() + ")"
+ myTelephone.getExchangeNumber() + "-" + myTelephone.getNumber()+"\n");
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 俺有一个java问题, 俺老师要求把2个类定义在不同的文件里。这样,如果俺要在b文件里调用a文件里的类怎么办?总有报错。朋友告诉俺要把classpath去掉才行,请问高手同学们,那个命令怎么写呀,格式呢?
    • 俺这个名字起的不好,换一个
      • 俺这两天也一只觉得你祖上是不是有谁蒙过怨, 咋还避讳了呢
        • 呵呵,哪里跟哪里呀,你祖上难道是红龙?^_^
          • 是, 只不过不才是恐龙 :(
            不是因为"清风不识字何故乱翻书"而兴了一场文字狱嘛.
            • 俺就是刻意把那个清字改了适合这个季节。呵呵,没想到...还是要这么想:(
      • 那俺叫这个名了.:P咱姐弟俩联回像.:X
        • 没搞定?
          • :-)
    • 是不是import?八年了,都忘了
    • 低手同学来了:在b的头写package rr.basic, 在a的头写package rr.app;(其实这句没啥关系) import rr.basic.*; 然后编译javac -d . *.java, 应该就可以了。大致如此,未经验证,试吧。
    • sigh ... 何苦呢,何必呢?
      File A and B (put them in same directory e.g. c:\XXX and compile them!)
      A:
      public A
      {
      public void a()
      {
      ...
      }
      }


      B:
      public B
      {
      public void b()
      {
      (new A()).a();
      }

      public static void main(String[] sdfj)
      {
      (new B()).b();
      }
      }

      After compile, java -cp c:\XXX B
      • 俺一直把它们放在一个目录下的。我昨天实在没办法,原封不动放到朋友那里去complie就成功了,在我自己这里还是有报错。我的版本是1.4.1,他们的是1.3.1.真是郁闷。实在不行就只能原封不动交上去了。
        • 保险的做法,到学校的LAB COMPILE.
          • 来不及了:(
        • 那就对了,sun的1.4.1是个头痛,1.3.1下的applet到1.4.1下很多都报错,得重写。
      • 俺把code贴出来吧,大家给俺看看有错没有,否则俺就这样交了。A和B在一个目录下面。
        本文发表在 rolia.net 枫下论坛A.

        public class TelephoneFormat
        {
        private String telephone;
        public TelephoneFormat(String t)
        {
        telephone = t;
        }
        public String getAreaCode()
        {
        return telephone.substring(0,3);
        }
        public String getExchangeNumber()
        {
        return telephone.substring(3,6);
        }
        public String getNumber()
        {
        return telephone.substring(6,10);
        }
        public void setTelephoneNumber(String t)
        {
        this.telephone = t;
        }
        }

        B.
        import java.io.*;

        public class TestTelephoneFormat

        {
        public static void main (String[] args) throws IOException
        {
        TelephoneFormat myTelephone;
        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter 10-digit telephone number:");
        String input = console.readLine();
        myTelephone = new TelephoneFormat(input);
        myTelephone.setTelephoneNumber(input);
        System.out.print("The area code is: = " + myTelephone.getAreaCode()+"\n");
        System.out.print("The exchange is: = " + myTelephone.getExchangeNumber()+"\n");
        System.out.print("The number is: = " + myTelephone.getNumber()+"\n");
        System.out.print("The complete telephone number is: =" + "(" + myTelephone.getAreaCode() + ")"
        + myTelephone.getExchangeNumber() + "-" + myTelephone.getNumber()+"\n");
        }
        }更多精彩文章及讨论,请光临枫下论坛 rolia.net
        • COMPILE时的错误是什么? 你把两个文件所在的目录加到CLASSPATH去了吗?
          • 俺问的就是这个问题呀,俺就是不会加。现在已经好了~~~
    • rainrain在学什么啊?怎么又是离散又是java的呢?@@
      • 还不止呢,还有政治,critical thinking~~~都是专业必修课真是麻烦:(
        • 不要告诉我你在学MBA!!@@
          • kidding me!俺这样的MBA?
            • 哇呀,你回贴太快啦:P 那我再继续猜,CS?
    • 问题解决了,答案如下:做了一个批处理文件把classpath加上,然后就一切OK了。主要是因为用的command line,所以特别麻烦:( 谢谢各位的解答,谢谢所有帮助俺的朋友!!!^_^