标签:
package objectoriented; public class Dog { public String name; public void run(){ System.out.print(name + " is running..."); //this指向当前对象 this.jump(); } public void jump(){ System.out.print(" jump"); } }
package objectoriented; public class ObjectOriented { public static void main(String[] args) { Dog dog = new Dog(); dog.name = "little dog"; dog.run(); } }控制台输出:
little dog is running... jump
package objectoriented; public class TestArgsPass { public static void main(String[] args) { int a = 100; int b = 102; System.out.println("交换前:a = " + a + " b = " + b); swap(a,b); System.out.println("交换结束后:a = " + a + " b = " + b); } public static void swap(int a, int b) { int temp = a; a = b; b = temp; System.out.println("swap方法里面的值:a = " + a + " b = " + b); } }
交换前:a = 100 b = 102 swap方法里面的值:a = 102 b = 100 交换结束后:a = 100 b = 102
package objectoriented; public class Party { public static void main(String[] args) { Party party = new Party(); party.info("小明","小强","小亮"); } public void info(String... args) { System.out.println("人数 = " + args.length+"人"); System.out.print("具体有:"); for (String i : args) { System.out.print(i + ", "); } } }
人数 = 3人 具体有:小明, 小强, 小亮,
package objectoriented; public class Party { public static void main(String[] args) { Party party = new Party(); party.info(2007,"小明","小强","小亮"); } public void info(int n,String...args) { System.out.println("第" + n+"届同学聚会"); System.out.print("参加的人有:"); for (String i : args) { System.out.print(i + ", "); } } }
第2007届同学聚会 参加的人有:小明, 小强, 小亮,
package objectoriented; /** * 用递归方法,计算1+2+3+...+100 * @version 1.0 * @author jscly * @dateTime 2016/06/04 */ public class Recursion { public static void main(String[] args) { int n = 100; Recursion recursion = new Recursion(); System.out.println(recursion.count(n)); } public int count(int n) { //法一 if(n == 1){ return 1; } return n + count(n - 1); //法二 // if (n == 1) { // n = 1; // } // else if(n>1){ // // n += count(n - 1); // } // // return n; } }
package homework; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.JOptionPane; public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
package homework; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; /** * 在GUI中,输入的姓名、年龄(规定:姓名只能由6-8位大小写的英文字母或汉字组成,年龄需<span style="font-family: Arial, Helvetica, sans-serif;">在40-50范围内),</span>如果姓名和年龄的输入格式正确, * 则显示结果;否则提醒用户相应的错误 * * @author jscly * @version 1.0 * @dateTime 2016/06/05 * */ public class MiddleAge extends JFrame { JLabel jl_name; JLabel jl_age; JTextField jtf_name; JTextField jtf_age; JButton jbt_sure; JButton jbt_reset; JButton jbt_cancel; JTextArea jta_result; public MiddleAge() { jl_name = new JLabel("姓名"); jl_age = new JLabel("年龄"); jtf_name = new JTextField(); jtf_age = new JTextField(); jbt_sure = new JButton("确定"); jbt_reset = new JButton("重置"); jbt_cancel = new JButton("取消"); jta_result = new JTextArea(); jl_name.setHorizontalAlignment(0); jl_age.setHorizontalAlignment(0); jta_result.setEditable(false); JPanel p1 = new JPanel(); p1.setLayout(new GridLayout(2, 2, 5, 5)); p1.add(jl_name); p1.add(jtf_name); p1.add(jl_age); p1.add(jtf_age); JPanel p2 = new JPanel(); p2.setLayout(new GridLayout(2, 3, 5, 5)); for (int i = 0; i < 3; i++) { p2.add(new JLabel()); } p2.add(jbt_sure); p2.add(jbt_reset); p2.add(jbt_cancel); JPanel p3 = new JPanel(); p3.setLayout(new GridLayout(2, 1, 5, 5)); p3.add(p1); p3.add(p2); add(p3, BorderLayout.NORTH); add(jta_result, BorderLayout.CENTER); jbt_sure.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { handle(jtf_name.getText(), jtf_age.getText()); } }); jbt_reset.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jtf_name.setText(""); jtf_age.setText(""); } }); jbt_cancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); } public void handle(String name, String age) { Person person = new Person(); int ageInt = 0; // 先判断姓名和年龄是否为空 if ("".equals(name)) { JOptionPane.showMessageDialog(null, "姓名不能为空!"); if ("".equals(age)) { JOptionPane.showMessageDialog(null, "年龄不能为空!"); } return; } // (1)得到姓名的长度,接着判断这个长度是否合法 // (2)判断年龄是否在[40,50]范围内 if (!"".equals(name) || name != null && (age != null || !"".equals(age))) { // 得到姓名的长度,并验证其有效性 char[] charArray = name.toCharArray(); int lengthOfName = 0; for (char i = 0; i < charArray.length; i++) { // 验证姓名的单个字符是否为大写或小写的英文字母,是则长度加1 if ((charArray[i] >= 65 && charArray[i] <= 90) || (charArray[i] >= 97 && charArray[i] <= 122)) { lengthOfName++; } /* * 在unicode 编码表中 汉字范围从 4E00(一) 到 9fa5(龥); * int 范围是 19968 - 40869; * 总共记录汉字数量为20902 个 */ // 验证姓名的单个字符是否为汉字,是则长度加2 else if (charArray[i] >= '\u4e00' && charArray[i] <= '\u9fa5') { lengthOfName += 2; } // 姓名的单个字符不合法 else { lengthOfName = 0; break; } } // 判断姓名长度是否合法 if (lengthOfName < 6 || lengthOfName > 8) { JOptionPane.showMessageDialog(null, "姓名格式错误!应由中、英文组成,范围为6-8位(1个汉字占2位)"); return; } // 判断年龄格式是否合法,得到年龄 Pattern pattern = Pattern.compile("\\d{2}"); Matcher matcher = pattern.matcher(age); if (matcher.matches()) { ageInt = Integer.parseInt(age); } else { JOptionPane.showMessageDialog(null, "年龄格式错误!请输入[40,50]范围内的数字"); return; } // 判断年龄是否在[40,50]范围内 if ((ageInt < 40 || ageInt > 50)) { JOptionPane.showMessageDialog(null, "年龄格式错误!请输入[40,50]范围内的数字"); return; } } person.setName(name); person.setAge(ageInt); jta_result .setText("姓名:" + person.getName() + "\n年龄:" + person.getAge()); } public static void main(String[] args) { MiddleAge person = new MiddleAge(); person.setVisible(true); person.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); person.setTitle("中年类"); person.setLocationRelativeTo(null); person.setSize(400, 400); } }
package printallchinese; public class PrintAllChinese { /** * 打印所有的汉字 * * 在unicode 编码表中 汉字范围从 4E00(一) 到 9fa5(龥); int 范围是 19968 - 40869; 总共记录汉字数量 * 20902 个 * * @author jscly * @version 1.0 * @dateTime 2016/06/04 * @return */ public String printAllChinses() { String result = ""; // 法一 // for (char i = '\u4e00',k = 0; i <= '\u9fa5'; i++,k++) { // if ((k + 1) % 50 == 0) { // result += "\n" + i; // k = 0; // } // else { // result += " " + i; // } // } // 法二 for (int i = 19968, k = 0; i <= 40869; i++, k++) { // 每行输出50个汉字 if ((k + 1) % 50 == 0) { result += "\n" + (char) i; k = 0; } else { result += " " + (char) i; } } return result; } public static void main(String[] args) { PrintAllChinese pac = new PrintAllChinese(); String result = pac.printAllChinses(); System.out.println(result); } }控制台输出结果的部分截图:
标签:
原文地址:http://blog.csdn.net/jscly/article/details/51600645