输入某学期第一周周一的日期(年、月、日),在输入要查询的日期,点击计算按钮,显示出查询日期是第几周周几。例如,输入的第一周周一的时间为2010年8月30日,再输入2010-12-2,则计算出2010-12-2为第14周周四。
界面要求:用图形界面实现。
PS:用户图形界面我已经写好了 只要求各位大神们给我个算法代码就可以了
import java.awt.*;
import java.awt.event.*;
public class jisuanJFrame extends Frame implements ActionListener
{
private TextField text_char;
private Button button_char;
public jisuanJFrame()
{
super("学周数的计算");
this.setBounds(280,100,100,150);
this.setBackground(java.awt.Color.lightGray);
this.setLayout(new java.awt.FlowLayout(FlowLayout.LEFT));
this.add(new Label("日期"));
this.add(new TextField("2010年8月30日"));
this.add(new TextField(20));
this.add(new Button("计算"));
this.addWindowListener(new WinClose());
this.setVisible(true);
}
public static void main(String arg[])
{
new jisuanJFrame();
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
class WinClose implements WindowListener
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
java学期周数的计算,求算法
答案:2 悬赏:70 手机版
解决时间 2021-03-09 18:52
- 提问者网友:愿为果
- 2021-03-09 12:39
最佳答案
- 五星知识达人网友:老鼠爱大米
- 2021-03-09 13:05
//一个简单的日期计算程序
import java.util.*;
import java.text.*;
public class DateCalculator
{
Date standardDate;
DateFormat mf = new SimpleDateFormat( "yyyy.MM.dd ");
Calendar cal = Calendar.getInstance();
Calendar cal_temp = Calendar.getInstance();
public DateCalculator()
{
try{
standardDate = mf.parse( "2000.01.01 ");
}
catch(ParseException ep){
ep.printStackTrace();
}
}
//设置基本日期,使用 "yyyy.MM.dd "格式的字符串
public boolean setStandardDate(String str)
{
Date date;
try{
date = mf.parse(str);
}
catch(ParseException ep){
ep.printStackTrace();
return false;
}
standardDate = date;
return true;
}
public boolean setStandardDate(Date date)
{
standardDate = getOnlyDate(date);
return true;
}
//year 为年数
//month 为月份,以 0 开始,与系统的不同,取值 0-11
//为的是与Java的内部使用兼容
//day 为日期天数
public boolean setStandardDate(int year,int month,int day)
{
cal.clear();
cal.set(year,month,day);
standardDate = cal.getTime();
return true;
}
public boolean setStandardDate(long time)
{
standardDate = getOnlyDate(time);
return true;
}
public Date getStandardDate()
{
return standardDate;
}
//取得一个只有日期,不含有时间的日期,也就是时间是 0时 0分 0秒.
public Date getOnlyDate(Date date)
{
cal.setTime(date);
cal_temp.clear();
cal_temp.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH));
return cal_temp.getTime();
}
public Date getOnlyDate(long time)
{
cal.setTimeInMillis(time);
cal_temp.clear();
cal_temp.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH));
return cal_temp.getTime();
}
//使用一个字符串取得一个日期,样式 "yyyy.MM.dd "格式的字符串
public Date getDate(String str)
{
Date date;
try{
date = mf.parse(str);
}
catch(ParseException ep){
ep.printStackTrace();
return getOnlyDate(new Date());
}
return date;
}
//使用输入的日期减去标准日期,得到相差的天数
public int sub(Date date)
{
long temp = date.getTime()-standardDate.getTime();
if(temp> 0)
{
return (int)(temp/(24*60*60*1000));
}
else
{
return (int)(temp/(24*60*60*1000))-1;
}
}
//使用 b_date - j_date 得到相差的天数
public int sub(Date b_date , Date j_date)
{
Date date = getOnlyDate(j_date);
long temp = b_date.getTime()-date.getTime();
if(temp> 0)
{
return (int)(temp/(24*60*60*1000));
}
else
{
return (int)(temp/(24*60*60*1000))-1;
}
}
//得到标准日期减去几天后的日期
public Date add(int day)
{
cal.setTime(standardDate);
cal.add(Calendar.DATE,day);
return cal.getTime();
}
//得到 date 减去几天后的日期
public Date add(Date date , int day)
{
cal.setTime(date);
cal.add(Calendar.DATE,day);
return cal.getTime();
}
public static void main(String args[])
{
DateCalculator temp = new DateCalculator();
Date d = temp.getDate( "2003.01.01 ");
int i = temp.sub(new Date(),d);
System.out.println (i);
}
}
import java.util.*;
import java.text.*;
public class DateCalculator
{
Date standardDate;
DateFormat mf = new SimpleDateFormat( "yyyy.MM.dd ");
Calendar cal = Calendar.getInstance();
Calendar cal_temp = Calendar.getInstance();
public DateCalculator()
{
try{
standardDate = mf.parse( "2000.01.01 ");
}
catch(ParseException ep){
ep.printStackTrace();
}
}
//设置基本日期,使用 "yyyy.MM.dd "格式的字符串
public boolean setStandardDate(String str)
{
Date date;
try{
date = mf.parse(str);
}
catch(ParseException ep){
ep.printStackTrace();
return false;
}
standardDate = date;
return true;
}
public boolean setStandardDate(Date date)
{
standardDate = getOnlyDate(date);
return true;
}
//year 为年数
//month 为月份,以 0 开始,与系统的不同,取值 0-11
//为的是与Java的内部使用兼容
//day 为日期天数
public boolean setStandardDate(int year,int month,int day)
{
cal.clear();
cal.set(year,month,day);
standardDate = cal.getTime();
return true;
}
public boolean setStandardDate(long time)
{
standardDate = getOnlyDate(time);
return true;
}
public Date getStandardDate()
{
return standardDate;
}
//取得一个只有日期,不含有时间的日期,也就是时间是 0时 0分 0秒.
public Date getOnlyDate(Date date)
{
cal.setTime(date);
cal_temp.clear();
cal_temp.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH));
return cal_temp.getTime();
}
public Date getOnlyDate(long time)
{
cal.setTimeInMillis(time);
cal_temp.clear();
cal_temp.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH));
return cal_temp.getTime();
}
//使用一个字符串取得一个日期,样式 "yyyy.MM.dd "格式的字符串
public Date getDate(String str)
{
Date date;
try{
date = mf.parse(str);
}
catch(ParseException ep){
ep.printStackTrace();
return getOnlyDate(new Date());
}
return date;
}
//使用输入的日期减去标准日期,得到相差的天数
public int sub(Date date)
{
long temp = date.getTime()-standardDate.getTime();
if(temp> 0)
{
return (int)(temp/(24*60*60*1000));
}
else
{
return (int)(temp/(24*60*60*1000))-1;
}
}
//使用 b_date - j_date 得到相差的天数
public int sub(Date b_date , Date j_date)
{
Date date = getOnlyDate(j_date);
long temp = b_date.getTime()-date.getTime();
if(temp> 0)
{
return (int)(temp/(24*60*60*1000));
}
else
{
return (int)(temp/(24*60*60*1000))-1;
}
}
//得到标准日期减去几天后的日期
public Date add(int day)
{
cal.setTime(standardDate);
cal.add(Calendar.DATE,day);
return cal.getTime();
}
//得到 date 减去几天后的日期
public Date add(Date date , int day)
{
cal.setTime(date);
cal.add(Calendar.DATE,day);
return cal.getTime();
}
public static void main(String args[])
{
DateCalculator temp = new DateCalculator();
Date d = temp.getDate( "2003.01.01 ");
int i = temp.sub(new Date(),d);
System.out.println (i);
}
}
全部回答
- 1楼网友:洎扰庸人
- 2021-03-09 13:24
import java.awt.*;
import java.awt.event.*;
public class calcappdemo extends frame{
private textfield t_result;
private panel p_main; //主面板
private panel p_num; //数字面板
private panel p_oper; //操作符面板
private panel p_show; //显示面板
private button b_num[]; //数字按钮
private button b_oper[]; //操作符面板
public calcappdemo(string title){
settitle(title);
t_result = new textfield("0.0", 21);
p_main = new panel();
p_num = new panel();
p_oper = new panel();
p_show = new panel();
p_main.setlayout(new borderlayout());
p_num.setlayout(new gridlayout(4, 3, 1, 1));
p_oper.setlayout(new gridlayout(4, 2, 1, 1));
b_num = new button[12];
for(int i=0; i<9; i++)
{
b_num = new button(new integer(i+1).tostring());
}
b_num[9] = new button("0");
b_num[10] = new button("cls");
b_num[11] = new button(".");
for(int i=0; i<12; i++)
{
p_num.add(b_num);
}
b_oper = new button[8];
b_oper[0] = new button("+");
b_oper[1] = new button("-");
b_oper[2] = new button("*");
b_oper[3] = new button("/");
b_oper[4] = new button("pow");
b_oper[5] = new button("sqrt");
b_oper[6] = new button("+/-");
b_oper[7] = new button("=");
for(int i=0; i<8; i++) //
{
p_oper.add(b_oper);
}
t_result.seteditable(false);
p_show.add(t_result, borderlayout.north);
p_main.add(p_show, borderlayout.north);
p_main.add(p_num, borderlayout.west);
p_main.add(p_oper, borderlayout.east);
this.add(p_main, borderlayout.center);
setsize(400, 400);
setresizable(false);
pack();
this.addwindowlistener(new windowadapter(){
public void windowclosing(windowevent e)
{
system.exit(0);
}
});
buttonlistener b1 = new buttonlistener();
for(int i=0; i<12; i++)
{
b_num.addactionlistener(b1);
}
for(int i=0; i<8; i++)
{
b_oper.addactionlistener(b1);
}
}
class buttonlistener implements actionlistener
{
private string lastop; //存储上一此操作符
private string strval; //存储数字对应的字符串
private double total; //总数
private double number; //存储新输入的数
private boolean firsttime; //判断是否第一次按下的是操作符按钮
private boolean operatorpressed;//判断是否已经按过操作符按钮
buttonlistener()
{
firsttime = true;
strval = "";
}
//事件处理器
public void actionperformed(actionevent e)
{
string s = ((button)e.getsource()).getlabel().trim();
if(character.isdigit(s.charat(0)))
{//判断是操作数还是操作符
handlenumber(s);
}
else
{
calculate(s);
}
}
//判断是一元操作符还是二元操作符,并根据操作符类型做计算
void calculate(string op)
{
operatorpressed = true;
if(firsttime&&! isunary(op))
{
total = getnumberondisplay();
firsttime = false;
}
if(isunary(op))
{
handleunaryop(op);
}
else if(lastop != null)
{
handlebinaryop(lastop);
}
if(! isunary(op))
{
lastop = op;
}
}
//判断是否一元操作符
boolean isunary(string s)
{
return s.equals("=")
||s.equals("cls")||s.equals("sqrt")
||s.equals("+/-")||s.equals(".");
}
//处理一元操作符
void handleunaryop(string op)
{
if(op.equals("+/-"))
{//
number = negate(getnumberondisplay() + "");
t_result.settext("");
t_result.settext(number + "");
return;
}else if(op.equals("."))
{
handledecpoint();
return;
}else if(op.equals("sqrt"))
{
number = math.sqrt(getnumberondisplay());
t_result.settext("");
t_result.settext(number + "");
return;
}else if(op.equals("="))
{//
if(lastop!= null && !isunary(lastop))
{
handlebinaryop(lastop);
}
lastop = null;
firsttime = true;
return;
}else
{
clear();
}
}
//处理二元运算符
void handlebinaryop(string op)
{
if(op.equals("+"))
{
total +=number;
}else if(op.equals("-"))
{
total -=number;
}else if(op.equals("*"))
{
total *=number;
}else if(op.equals("/"))
{
try
{
total /=number;
}catch(arithmeticexception ae){}
}else if(op.equals("pow"))
total = math.pow(total, number);
//t_result.settext("");
lastop = null;
// strval = "";
number = 0;
t_result.settext(total + "");
}
//该方法用于处理数字按钮
void handlenumber(string s)
{
if(!operatorpressed)
{
strval += s;
}else
{
operatorpressed = false;
strval = s;
}
//
number = new double(strval).doublevalue();
t_result.settext("");
t_result.settext(strval);
}
//该方法用于按下"."按钮
void handledecpoint()
{
operatorpressed = false;
//
if(strval.indexof(".")<0)
{
strval += ".";
}
t_result.settext("");
t_result.settext(strval);
}
//该方法用于将一个数求反
double negate(string s)
{
operatorpressed = false;
//如果是一个整数,去掉小数点后面的0
if(number == (int)number)
{
s = s.substring(0,s.indexof("."));
}
//如果无"-"增加在该数的前面
if(s.indexof("-")<0)
{
strval = "-" + s;
}
else
{
strval = s.substring(1);
}
return new double(strval).doublevalue();
}
//将显示框中的值转换成double
double getnumberondisplay()
{
return new double(t_result.gettext()).doublevalue();
}
//清除屏幕并设置所有的标识
void clear()
{
firsttime = true;
lastop = null;
strval = "";
total = 0;
number = 0;
t_result.settext("0");
}
}
public static void main(string[] args) {
calcappdemo c = new calcappdemo("简单的计算器程序");
c.setvisible(true);
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯