关于java的一些问题,比较基础,大家可以看看咯

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012187684/article/details/53262446
问题:一瓶汽水一块钱,喝完退五毛钱,有20元,用java程序写20元能喝多少汽水
回答:
int a;
int i;
a=20;
i=0;
a=2*a;
while(a>2){
a=a-1;
i=i+1
}
println(i);

问题:假设当前时间是2015年5月12日10点11分00秒,编写一个currenttime类,设置属性为该时间,定义show()方法显示该时间。
回答:
public class CurrentTime {
 private int year;
 private int month;
 private int day;
 private int hour;
 private int minute;
 private int seconds;
 
 public CurrentTime(int year, int month, int day, int hour, int minute, int seconds) {
  super();
  this.year = year;
  this.month = month;
  this.day = day;
  this.hour = hour;
  this.minute = minute;
  this.seconds = seconds;
 }
 public String show() {
  String hourStr = hour < 10 ? "0" + hour : "" + hour;
  String minuteStr = minute < 10 ? "0" + minute : "" + minute;
  String secondStr = seconds < 10 ? "0" + seconds : "" + seconds;
  return year + "年" + month + "月" + day + "日" + hourStr + "点" + minuteStr + "分" + secondStr + "秒";
 }
 public static void main(String[] args) {
  CurrentTime currentTime = new CurrentTime(2015, 5, 12, 10, 11, 0);
  System.out.println(currentTime.show());
 }
}


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页