Java-菜鸟学习之路(一)

来源:互联网 发布:淘宝美工待遇 编辑:程序博客网 时间:2024/05/17 23:53

Java-菜鸟学习之路(一)

今天是在苏州培训Java的第一天,想把自己的所见所得所感悟和错误,分享给大家。希望给Java初学者提供一些帮助,也希望各位大神能给本菜鸟提供一些建议!
  1. 输入字符串,取其中数字,并生成新的字符串
    <span style="background-color: rgb(255, 255, 153);"><span style="font-size:14px;">public class IsNum{public static void main(String[] args){Scanner scanner = new Scanner(System.in);String str = scanner.nextLine();//获取键盘输入for(int i = 0;i < str.length();i++){String str2 = "";if(str.charAt(i)>=48&&str.charAt(i)<=57){   //判断是否为整数str2 = str.charAt(i)+"";        }System.out.print(str2);}}}</span></span>

  2. 设计一个矩形类,求其周长和面积
    <span style="background-color: rgb(255, 255, 153);">class Rect{<span style="white-space: pre;"></span>//矩形类double length;double width;public double getPerimeter(double length,double width){<span style="white-space: pre;"></span>//计算周长double perimeter = (length + width)*2;return perimeter;}public double getSquare(double length,double width){<span style="white-space: pre;"></span>//计算面积double square = length * width;return square;}}public class RectTest{public static void main(String[] args){Rect rect = new Rect();rect.length = 10;rect.width = 10;System.out.println(rect.getPerimeter(rect.length,rect.width));System.out.println(rect.getSquare(rect.length,rect.width));}}</span>

  3. 设计一个Watch类,并且含有设置时间和显示时间的方法
    <span style="background-color: rgb(255, 255, 153);">import java.util.Date;import java.text.DateFormat.*;import java.text.SimpleDateFormat;class Watch{String time;public void setTime(String time){this.time = time;}public String getTime(){return time;}}public class WatchTest{public static void main(String[] args){Date d = new Date();<span style="white-space:pre"></span>//得到一个日期对象SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");<span style="white-space:pre"></span>//得到一个格式化日期对象String str = format.format(d);<span style="white-space:pre"></span>//将日期格式化成字符串Watch watch = new Watch();watch.setTime(str);System.out.println(watch.getTime());}}</span>
    以上就是本菜鸟今天的学习成果。
  4. 希望可以给大家带来一些帮助,也希望大神们可以批评指正!!谢谢~Java是一门非常给力的语言,希望可以和各位一起学好它,明天会更好!
0 0
原创粉丝点击