| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1166 人关注过本帖
标题:[求助]怎样编写这样的程序?
只看楼主 加入收藏
xuyijin
Rank: 1
等 级:新手上路
威 望:1
帖 子:90
专家分:0
注 册:2006-4-13
收藏
 问题点数:0 回复次数:14 
[求助]怎样编写这样的程序?
各位大虾:
偶是一个JAVA的初学者,最近看到这样的一个编程题目:
输出某年某月的日历页,通过main方法的参数将年份和月份时间传递到程序中.
请问各位应该怎样编写呀!望各们大虾们多多指教了!小北先谢过了!
搜索更多相关主题的帖子: 编写 
2006-04-20 17:28
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

用java.util.Calendar类实现


可惜不是你,陪我到最后
2006-04-20 18:08
★王者至尊★
Rank: 1
等 级:新手上路
帖 子:528
专家分:0
注 册:2006-3-28
收藏
得分:0 

方法getTime()


------Java 爱好者,论坛小混混,学习中------
2006-04-20 19:22
★王者至尊★
Rank: 1
等 级:新手上路
帖 子:528
专家分:0
注 册:2006-3-28
收藏
得分:0 

参考一下这个程序:
---------------------------------
import java.applet.Applet;
import java.awt.*;
import java.util.*;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class Clock extends Applet implements Runnable
{
Thread thisThread;
Color faceColor, borderColor, minuteColor, hourColor, secondColor;
Calendar d;
public void init()
{
//read in the colors for each of the hands and for the face/border
faceColor = readColor(getParameter("faceCol"));
borderColor = readColor(getParameter("borderCol"));
minuteColor = readColor(getParameter("minuteCol"));
hourColor = readColor(getParameter("hourCol"));
secondColor = readColor(getParameter("secondCol"));
}

// This method creates a color based on a string.
// The string is assumed to be "red, green, blue" where each
// of the colors is represented by its integer equivalent.
public Color readColor(String aColor)
{
if(aColor == null)
return Color.black;
int r, g, b;

//break the string apart into each number
StringTokenizer st = new StringTokenizer(aColor, ",");

try
{
r = Integer.valueOf(st.nextToken()).intValue();
g = Integer.valueOf(st.nextToken()).intValue();
b = Integer.valueOf(st.nextToken()).intValue();
return new Color(r,g,b);
}
catch(Exception e)
{
System.out.println("An exception occurred trying to convert a parameter to a color:" + e);
return Color.black;
}
}
public String getParameter(String name)
{
String ST;
if(inApplet)
return super.getParameter(name);
//if you are not in an applet you default all of the values
if(name == "hourcol")
return "255,00,00";
if(name == "minuteCol")
return "00,255,00";
if(name == "secondCol")
return "00,00,255";
if(name == "borderCol")
return "255,255,255";
if(name == "faceCol")
return "125,125,125";
return null;
}
public void start()
{
thisThread = new Thread(this);
thisThread.start();
}

public void run()
{
while(true)
{
repaint();
try
{
thisThread.sleep(1000);
}catch(Exception e){}
}
}

public void update(Graphics g)
{
paint(g);
if(inApplet)
{
int uhr = d.get(Calendar.HOUR_OF_DAY);
int minute = d.get(Calendar.MINUTE);
int sec = d.get(Calendar.SECOND);
String currentTime = new String(uhr + ":" + minute + ":" + sec);
showStatus(currentTime);
}
}

public void paint(Graphics g)
{
//fill clock face
g.setColor(faceColor);
g.fillOval(0,0,100,100);
g.setColor(borderColor);
g.drawOval(0,0,100,100);

//get the current time
d = Calendar.getInstance();
//draw the minute hand
g.setColor(minuteColor);
double angle = (((double)(90 - d.get(Calendar.MINUTE)))/60)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*50),50+(int)(Math.cos(angle)*50));
//draw the hour hand
g.setColor(hourColor);
angle = ((((double)18 - d.get(Calendar.HOUR_OF_DAY)+(double)d.get(Calendar.MINUTE)/60))/12)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*40), 50+(int)(Math.cos(angle)*40));
//draw the second hand
g.setColor(secondColor);
angle = (((double)(90 - d.get(Calendar.SECOND)))/60)*2*Math.PI;
g.drawLine(50,50,50+(int)(Math.sin(angle)*50), 50+(int)(Math.cos(angle)*50));
}
static boolean inApplet = true;
public static void main(String args[])
{
//set a boolean flag to show if you are in an applet or not
inApplet = false;

//create a frame to place our application in
//you can change the string value to show your desired label
//for the frame
Frame myFrame = new Frame("Clock as an Application");
myFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
});
//create a clock instance
Clock myApp = new Clock();

//add the current application to the frame
myFrame.add("Center", myApp);

//resize the frame to the desired size and make it visible
myFrame.setSize(100,130);
myFrame.show();

//run the methods the browser normally would
myApp.init();
myApp.start();
}
}

-------------------------------------------
楼主就知道怎么办了....


------Java 爱好者,论坛小混混,学习中------
2006-04-20 22:01
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
楼上的帖程序是没有用的

可惜不是你,陪我到最后
2006-04-21 10:06
xuyijin
Rank: 1
等 级:新手上路
威 望:1
帖 子:90
专家分:0
注 册:2006-4-13
收藏
得分:0 

想了很久,终于写出来了,各位大虾点评一下!
import java.util.*;
import java.io.*;
class CalendarTest {

static int strToInt;
static String s="";
public static void getInfo()
{

try
{
BufferedReader cin=new BufferedReader (new InputStreamReader(System.in));
s=cin.readLine();
strToInt=Integer.parseInt(s);
}catch(Exception e){};
}
public static void main(String[] args)
{
int year;
int month;
Calendar calendarObj=Calendar.getInstance();
System.out.print("请输入你想查询日历的年份:");
getInfo();
year=strToInt;
System.out.println();
System.out.print("请输入你想查询日历的月份:");
getInfo();
month=strToInt;
calendarObj.set(year,month-1,1);
int weeks=calendarObj.get(Calendar.DAY_OF_WEEK)-1;
System.out.println();
System.out.println(year+"年"+month+"月的日历如下:");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println('日'+" "+'一'+" "+'二'+" "+'三'+" "+'四'+" "+'五'+" "+'六');
String[] count=new String[weeks+31];
for(int i=0;i<weeks;i++)
{
count[i]="**";
}
for(int i=weeks,n=1;i<weeks+31;i++)
{
if(n<=9)
count[i]=" "+String.valueOf(n);
else
count[i]=String.valueOf(n);
n++;
}
for(int i=0;i<weeks+31;i++)
{
if(i%7==0)
{

System.out.println();
System.out.print(count[i]+" ");
}
else
System.out.print(count[i]+" ");
}
System.out.println();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~");
}
}


初学java,希望各位大虾多多指教!!
2006-04-21 22:38
xuyijin
Rank: 1
等 级:新手上路
威 望:1
帖 子:90
专家分:0
注 册:2006-4-13
收藏
得分:0 
这个是用main方法的参数传递实现的:
import java.util.*;
class CalendarTest {
//通过main方法的参数传递到程序
public static void main(String[] args) {
Calendar calendarObj=Calendar.getInstance();
calendarObj.set(Integer.parseInt(args[0]),Integer.parseInt(args[1])-1,1);
int weeks=calendarObj.get(Calendar.DAY_OF_WEEK)-1;
System.out.println(args[0]+"年"+args[1]+"月的日历如下:");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println('日'+" "+'一'+" "+'二'+" "+'三'+" "+'四'+" "+'五'+" "+'六');
String[] count=new String[weeks+31];
for(int i=0;i<weeks;i++)
{
count[i]="**";
}
for(int i=weeks,n=1;i<weeks+31;i++)
{
if(n<=9)
count[i]=" "+String.valueOf(n);
else
count[i]=String.valueOf(n);
n++;
}
for(int i=0;i<weeks+31;i++)
{
if(i%7==0)
{

System.out.println();
System.out.print(count[i]+" ");
}
else
System.out.print(count[i]+" ");
}
System.out.println();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~");
}
}

初学java,希望各位大虾多多指教!!
2006-04-21 22:41
bob4926
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-4-22
收藏
得分:0 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at CalendarTest.main(CalendarTest.java:7)
请问这是什么意思?
2006-04-22 09:13
bob4926
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-4-22
收藏
得分:0 
怎么解决数组越界
2006-04-22 09:14
bob4926
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-4-22
收藏
得分:0 
我用的是eclipse3。2,是不是哪里设置有问题啊
2006-04-22 09:15
快速回复:[求助]怎样编写这样的程序?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026337 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved