Project1

标题: 今天布置的作业好难~ [打印本页]

作者: stevenrock    时间: 2015-8-20 18:05
标题: 今天布置的作业好难~
偶的JAVA学得一团糟,考试总是刚及格……

a.jpg (152.18 KB, 下载次数: 12)

a.jpg

作者: 永恒の未知数    时间: 2015-8-20 18:15
虽然我也不会,但我总觉得这是属于例题里面一类的
作者: 墨凌羽    时间: 2015-8-20 18:44
想起折腾ruby 差不多有一个月的时间再折腾字符串处理之类的【csv读取、xml读取、括弧匹配什么的
作者: taroxd    时间: 2015-8-20 19:23
本帖最后由 taroxd 于 2015-8-20 19:29 编辑
墨凌羽 发表于 2015-8-20 18:44
想起折腾ruby 差不多有一个月的时间再折腾字符串处理之类的【csv读取、xml读取、括弧匹配什么的 ...


Ruby 真简单,看了楼主的「提示」就坚决不想学 Java 了

RUBY 代码复制
  1. Student = Struct.new(:stuNo, :name) do
  2.   def equals(others)
  3.     self.stuNo == others.stuNo
  4.   end
  5.  
  6.   def toString
  7.     "姓名:#{name}\t学号:#{stuNo}"
  8.   end
  9. end
  10.  
  11. month, day, year = gets.split('/')
  12. "#{year}年#{month}月#{day}日", year, month, day
  13.  
  14. "随机字符串".count 'A-z'
  15. "随机字符串".count '0-9'
  16.  
  17. "一段歌词".scan('朋友').size
  18.  
  19. "有敏感词的内容".gsub Regexp.union('敏感词', '内容'), ''

作者: 墨凌羽    时间: 2015-8-20 19:40
本帖最后由 墨凌羽 于 2015-8-20 14:03 编辑
taroxd 发表于 2015-8-20 13:23
Ruby 真简单,看了楼主的「提示」就坚决不想学 Java 了


对了 Struct和
RUBY 代码复制
  1. class Student
  2.   attr_reader :stuNo
  3.   attr_reader :name
  4.   def initialize(stuNo, name)
  5.     @stuNo = stuNo
  6.     @name = name
  7.   end
  8. end

这种定义的区别在什么地方
嗯 另外一种鬼畜写法:
RUBY 代码复制
  1. class Student < Hash
  2.         def method_missing(m, *args, &block)
  3.                 return self.has_key?(m) ? self[m] : super
  4.         end
  5. end
  6.  
  7. a = Student[:stuNo,1,:name,"td"]

作者: 鑫の尘埃    时间: 2015-8-20 20:23
w完全只能看这各位大触们聊了
作者: RyanBern    时间: 2015-8-20 21:37
本帖最后由 RyanBern 于 2015-8-21 09:54 编辑

瞎写玩玩,长时间不用Java,不太熟练。
JAVA 代码复制
  1. class Student{
  2.     private String stuNo = "";
  3.     private String stuName = "";
  4.     public Student(String no, String name){
  5.         this.stuNo = no;
  6.         this.stuName = name;
  7.     }
  8.     public String getStuNo(){
  9.         return stuNo;
  10.     }
  11.     public void setStuNo(stuNo){
  12.         this.stuNo = stuNo;
  13.     }
  14.     public String getStuName(){
  15.         return stuName;
  16.     }
  17.     public void setStuName(name){
  18.         this.stuName = name;
  19.     }
  20.     public String toString(){
  21.         return "姓名:" + stuName + " " + "学号:" + stuNo;
  22.     }
  23.     public boolean equals(Object obj){
  24.         if(!(obj instanceof String)) return false;
  25.         Student stu = (Student)obj;
  26.         return stuNo.equals(stu.getStuNo());
  27.     }
  28. }


JAVA 代码复制
  1. import java.io.*;
  2. public class DateConvert{
  3.     public static void main(String[] args){
  4.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  5.         String input = br.readline();
  6.         int first_index = input.indexOf('/');
  7.         int last_index = input.lastIndexOf('/');
  8.         if(first_index < 0 || last_index == first_index){
  9.             System.out.println("无效的日期格式");
  10.             return;
  11.         }
  12.         String month = input.substring(0, first_index - 1);
  13.         String day = input.substring(first_index + 1, last_index - 1);
  14.         String year = input.substring(last_index + 1);
  15.         System.out.println(year + "年" + month + "月" + day + "日")
  16.     }
  17. }

只写了方法,main方法调用过程自己补上。
JAVA 代码复制
  1. public class StringCounter{
  2.     public static void main(String[] args){}
  3.     public static void countLettersAndNumbers(String str){
  4.         int letter_count = 0, number_count = 0;
  5.         char ch = NULL;
  6.         for(int i = 0;i < str.length();i++){
  7.             ch = str.charAt(i);
  8.             if(ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') letter_count += 1;
  9.             if(ch >= '0' && ch <= '9') number_count += 1;
  10.         }
  11.         System.out.println("字母数:" + letter_count);
  12.         System.out.println("数字数:" + number_count);
  13.     }
  14. }

这个能稍微简单点,但是效率差,有效率高的版本这里没写:
JAVA 代码复制
  1. String str = "测试字符串";
  2. int counts = str.split("朋友").length();

借助正则表达式:
JAVA 代码复制
  1. //需要import java.util.regex;
  2. String str = "测试字符串";
  3. String output = str;
  4. String[] words_to_filter = {"敏感词1", "敏感词2"};
  5. for(int i = 0;i < words_to_filter.length();i++){
  6.     String word = words_to_filter[i];
  7.     output = output.replaceAll(word, "**");
  8. }

作者: 冷峻逸    时间: 2015-8-21 09:17
提示: 作者被禁止或删除 内容自动屏蔽
作者: 上贺茂润    时间: 2015-8-21 10:01
楼主6的不行
作者: grayuncle    时间: 2015-8-21 13:23
99年用QB写过工资管理系统。转行后现在只会让一个字符在屏幕上乱跳




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1