刚看了一个程序,并写了一下,然后用Java 再写了一下
2010-09-15 10:43
357 查看
C# 的源码是这样的:
using System;
namespace Date
{
class Date
{
private int month;
private int day;
private int year;
public Date(int themonth,int theday,int theyear)
{
if (themonth > 0 && themonth <= 12)
month = themonth;
else
{
month = 1;
Console.Write("Month {0} invaild. Set to month 1", themonth);
}
year = theyear;
day = CheckDay (theday);
}
private int CheckDay(int testday)
{
int[] days = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if (testday > 0 && testday <= days[month])
{
return testday;
}
if (month == 2 && month == 29 && (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)))
return testday;
Console.Write("Day {0} invaild. Set to day 1.", testday);
return 1;
}
public string ToDateString()
{
return month + "/" + day + "/" + year;
}
}
}
using System;
namespace Date
{
class Employee
{
private string firstname;
private string lastname;
private Date birthday;
private Date hireday;
public Employee(string first,string last,int birthMonth,
int birthDay,int birthYear,int hireMonth,int hireDay,
int hireYear)
{
firstname = first;
lastname = last;
birthday = new Date(birthMonth, birthDay, birthYear);
hireday = new Date(hireMonth, hireDay, hireYear);
}
public string ToEmployeeString()
{
return lastname + " " + firstname + " /n" +
"Hired : " + hireday.ToDateString() +
"/nBirthday : " + birthday.ToDateString();
}
}
}
using System;
namespace Date
{
class CompositionTest
{
static void Main(string[] args)
{
Employee e = new Employee("Bob", "chen", 12, 11, 1990, 11, 21, 2010);
string output = e.ToEmployeeString();
Console.Write(output);
Console.ReadKey();
}
}
}
Java 的源码是这样的:
import java.applet.*;
public class Date {
private int month;
private int day;
private int year;
public Date(int themonth,int theday,int theyear)
{
if(themonth > 0 && themonth <= 12)
month = themonth;
year = theyear;
day = CheckDay(theday);
}
private int CheckDay(int testDay)
{
int[] DaysofMonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if(testDay > 0 && testDay <= DaysofMonth[month])
return testDay;
if(month==2 && testDay==29 && ((year%400==0)||(year%4==0 && year%100!=0)))
return testDay;
System.out.println("Day" + testDay + "invaild . Set to day 1.");
return 1;
}
public String ToDateString()
{
return month + "/" + day + "/" + year;
}
}
import java.applet.*;
public class Employee {
private String firstname;
private String lastname;
private Date birthDate;
private Date hireDate;
public Employee(String first,String last,int BMonth,int BDay,int BYear,
int HMonth,int HDay,int HYear)
{
firstname = first;
lastname = last;
birthDate = new Date(BMonth,BDay,BYear);
hireDate = new Date(HMonth,HDay,HYear);
}
public String ToEmployeeString()
{
return lastname + " " + firstname + "/n" +
"Hired : " + hireDate.ToDateString() +
"/nBirthDay : " + birthDate.ToDateString();
}
}
import java.applet.*;
public class CompositionTest {
public static void main(String[] args) {
Employee e = new Employee("Bob","Chen",12,21,1899,11,5,2007);
System.out.print(e.ToEmployeeString());
}
}
最后我发现,其实两者没啥大的区别。不过中间在写Java 的时候还是遇到了麻烦、
在C# 中,String 和 string 这两个单词,是不一样的。表达不同的意思, 要分开用。
但是在Java 中,就只有 String 这一个。哪都通用!!
using System;
namespace Date
{
class Date
{
private int month;
private int day;
private int year;
public Date(int themonth,int theday,int theyear)
{
if (themonth > 0 && themonth <= 12)
month = themonth;
else
{
month = 1;
Console.Write("Month {0} invaild. Set to month 1", themonth);
}
year = theyear;
day = CheckDay (theday);
}
private int CheckDay(int testday)
{
int[] days = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if (testday > 0 && testday <= days[month])
{
return testday;
}
if (month == 2 && month == 29 && (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)))
return testday;
Console.Write("Day {0} invaild. Set to day 1.", testday);
return 1;
}
public string ToDateString()
{
return month + "/" + day + "/" + year;
}
}
}
using System;
namespace Date
{
class Employee
{
private string firstname;
private string lastname;
private Date birthday;
private Date hireday;
public Employee(string first,string last,int birthMonth,
int birthDay,int birthYear,int hireMonth,int hireDay,
int hireYear)
{
firstname = first;
lastname = last;
birthday = new Date(birthMonth, birthDay, birthYear);
hireday = new Date(hireMonth, hireDay, hireYear);
}
public string ToEmployeeString()
{
return lastname + " " + firstname + " /n" +
"Hired : " + hireday.ToDateString() +
"/nBirthday : " + birthday.ToDateString();
}
}
}
using System;
namespace Date
{
class CompositionTest
{
static void Main(string[] args)
{
Employee e = new Employee("Bob", "chen", 12, 11, 1990, 11, 21, 2010);
string output = e.ToEmployeeString();
Console.Write(output);
Console.ReadKey();
}
}
}
Java 的源码是这样的:
import java.applet.*;
public class Date {
private int month;
private int day;
private int year;
public Date(int themonth,int theday,int theyear)
{
if(themonth > 0 && themonth <= 12)
month = themonth;
year = theyear;
day = CheckDay(theday);
}
private int CheckDay(int testDay)
{
int[] DaysofMonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if(testDay > 0 && testDay <= DaysofMonth[month])
return testDay;
if(month==2 && testDay==29 && ((year%400==0)||(year%4==0 && year%100!=0)))
return testDay;
System.out.println("Day" + testDay + "invaild . Set to day 1.");
return 1;
}
public String ToDateString()
{
return month + "/" + day + "/" + year;
}
}
import java.applet.*;
public class Employee {
private String firstname;
private String lastname;
private Date birthDate;
private Date hireDate;
public Employee(String first,String last,int BMonth,int BDay,int BYear,
int HMonth,int HDay,int HYear)
{
firstname = first;
lastname = last;
birthDate = new Date(BMonth,BDay,BYear);
hireDate = new Date(HMonth,HDay,HYear);
}
public String ToEmployeeString()
{
return lastname + " " + firstname + "/n" +
"Hired : " + hireDate.ToDateString() +
"/nBirthDay : " + birthDate.ToDateString();
}
}
import java.applet.*;
public class CompositionTest {
public static void main(String[] args) {
Employee e = new Employee("Bob","Chen",12,21,1899,11,5,2007);
System.out.print(e.ToEmployeeString());
}
}
最后我发现,其实两者没啥大的区别。不过中间在写Java 的时候还是遇到了麻烦、
在C# 中,String 和 string 这两个单词,是不一样的。表达不同的意思, 要分开用。
但是在Java 中,就只有 String 这一个。哪都通用!!
相关文章推荐
- 编写一个程序读入一行输入,然后反向打印该行,您可以把输入存储在一个char数组中: 假定该行不超过255个字符。回忆一下,您可以使用具有%c说明符的scanf()从输入中一次 读入一个字符,而且当您按
- 编写一个程序读入一行输入,然后反向打印该行,您可以把输入存储在一个char数组中: 假定该行不超过255个字符。回忆一下,您可以使用具有%c说明符的scanf()从输入中一次 读入一个字符,而且当您按下回车键时会产生换行符(/n)
- java程序: 输入数量不确定的正数和负数(一次输入一个), 然后打印正数和负数的个数,当输入0时,程序停止
- JAVA调用系统命令或可执行程序--返回一个Runtime运行时对象,然后启动另外一个进程来执行命令
- 8、 java 先写一个程序,打印从1到100的值。之后修改程序,通过使用break关键词,使得程序在打印到98时退出。然后尝试使用return来达到相同的目的。
- 学习JAVA有2天了,写了一个计算Pi的程序锻炼一下
- 编写一个JAVA程序,创建指定长度的 int 型数组,并生成 100 以内随机数为数组中的每个元素赋值,然后输出数组
- 编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。 程序允许用户提早结束输入,并在一行上显示所有成绩,然后报告平均成绩。 请使用3个数组处理函数来分别进行输入、显示和计算
- 一个自己编写的象棋程序,可实现走棋谱(如炮二平五),工程完成度%96,跟大家分享一下,下一步要做残局,欢迎交流
- java 编写一个没有main方法的程序
- Java写一个简单的死锁程序
- 一个读取MIB库的java程序全
- 一个java程序是怎样运行起来的(2)
- java嵌套类的一个小程序
- 一个在windows和基于SWT的java程序间拖拽的例子
- JAVA 程序中如何拷贝一个目录下的文件及子目录到另一个目录,如何获取系统环境变量等...
- java小练习(.一个求矩阵元素换位的程序)
- 将文本文件中的数据读取到一个二维数组中,求出这些数据的中位数,然后求所有数据与中位数的差值的绝对值和,并求出和的最小值,最后把程序运行的结果写到一个txt文件中
- 一个简单的java小程序
- Linux中设定让Firefox可以运行Java程序,然后老爸就可以在家与“小白”决象棋了!