Project1
标题: 今天布置的作业好难~ [打印本页]
作者: stevenrock 时间: 2015-8-20 18:05
标题: 今天布置的作业好难~
偶的JAVA学得一团糟,考试总是刚及格……
-
a.jpg
(152.18 KB, 下载次数: 12)
作者: 永恒の未知数 时间: 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 了
Student = Struct.new(:stuNo, :name) do
def equals(others)
self.stuNo == others.stuNo
end
def toString
"姓名:#{name}\t学号:#{stuNo}"
end
end
month, day, year = gets.split('/')
"#{year}年#{month}月#{day}日", year, month, day
"随机字符串".count 'A-z'
"随机字符串".count '0-9'
"一段歌词".scan('朋友').size
"有敏感词的内容".gsub Regexp.union('敏感词', '内容'), ''
Student = Struct.new(:stuNo, :name) do
def equals(others)
self.stuNo == others.stuNo
end
def toString
"姓名:#{name}\t学号:#{stuNo}"
end
end
month, day, year = gets.split('/')
"#{year}年#{month}月#{day}日", year, month, day
"随机字符串".count 'A-z'
"随机字符串".count '0-9'
"一段歌词".scan('朋友').size
"有敏感词的内容".gsub Regexp.union('敏感词', '内容'), ''
作者: 墨凌羽 时间: 2015-8-20 19:40
本帖最后由 墨凌羽 于 2015-8-20 14:03 编辑
taroxd 发表于 2015-8-20 13:23
Ruby 真简单,看了楼主的「提示」就坚决不想学 Java 了
对了 Struct和class Student
attr_reader :stuNo
attr_reader :name
def initialize(stuNo, name)
@stuNo = stuNo
@name = name
end
end
class Student
attr_reader :stuNo
attr_reader :name
def initialize(stuNo, name)
@stuNo = stuNo
@name = name
end
end
这种定义的区别在什么地方
嗯 另外一种鬼畜写法:
class Student < Hash
def method_missing(m, *args, &block)
return self.has_key?(m) ? self[m] : super
end
end
a = Student[:stuNo,1,:name,"td"]
class Student < Hash
def method_missing(m, *args, &block)
return self.has_key?(m) ? self[m] : super
end
end
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,不太熟练。
class Student{
private String stuNo = "";
private String stuName = "";
public Student(String no, String name){
this.stuNo = no;
this.stuName = name;
}
public String getStuNo(){
return stuNo;
}
public void setStuNo(stuNo){
this.stuNo = stuNo;
}
public String getStuName(){
return stuName;
}
public void setStuName(name){
this.stuName = name;
}
public String toString(){
return "姓名:" + stuName + " " + "学号:" + stuNo;
}
public boolean equals(Object obj){
if(!(obj instanceof String)) return false;
Student stu = (Student)obj;
return stuNo.equals(stu.getStuNo());
}
}
class Student{
private String stuNo = "";
private String stuName = "";
public Student(String no, String name){
this.stuNo = no;
this.stuName = name;
}
public String getStuNo(){
return stuNo;
}
public void setStuNo(stuNo){
this.stuNo = stuNo;
}
public String getStuName(){
return stuName;
}
public void setStuName(name){
this.stuName = name;
}
public String toString(){
return "姓名:" + stuName + " " + "学号:" + stuNo;
}
public boolean equals(Object obj){
if(!(obj instanceof String)) return false;
Student stu = (Student)obj;
return stuNo.equals(stu.getStuNo());
}
}
import java.io.*;
public class DateConvert{
public static void main(String[] args){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readline();
int first_index = input.indexOf('/');
int last_index = input.lastIndexOf('/');
if(first_index < 0 || last_index == first_index){
System.out.println("无效的日期格式");
return;
}
String month = input.substring(0, first_index - 1);
String day = input.substring(first_index + 1, last_index - 1);
String year = input.substring(last_index + 1);
System.out.println(year + "年" + month + "月" + day + "日");
}
}
import java.io.*;
public class DateConvert{
public static void main(String[] args){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readline();
int first_index = input.indexOf('/');
int last_index = input.lastIndexOf('/');
if(first_index < 0 || last_index == first_index){
System.out.println("无效的日期格式");
return;
}
String month = input.substring(0, first_index - 1);
String day = input.substring(first_index + 1, last_index - 1);
String year = input.substring(last_index + 1);
System.out.println(year + "年" + month + "月" + day + "日");
}
}
只写了方法,main方法调用过程自己补上。
public class StringCounter{
public static void main(String[] args){}
public static void countLettersAndNumbers(String str){
int letter_count = 0, number_count = 0;
char ch = NULL;
for(int i = 0;i < str.length();i++){
ch = str.charAt(i);
if(ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') letter_count += 1;
if(ch >= '0' && ch <= '9') number_count += 1;
}
System.out.println("字母数:" + letter_count);
System.out.println("数字数:" + number_count);
}
}
public class StringCounter{
public static void main(String[] args){}
public static void countLettersAndNumbers(String str){
int letter_count = 0, number_count = 0;
char ch = NULL;
for(int i = 0;i < str.length();i++){
ch = str.charAt(i);
if(ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') letter_count += 1;
if(ch >= '0' && ch <= '9') number_count += 1;
}
System.out.println("字母数:" + letter_count);
System.out.println("数字数:" + number_count);
}
}
这个能稍微简单点,但是效率差,有效率高的版本这里没写:
String str = "测试字符串";
int counts = str.split("朋友").length();
String str = "测试字符串";
int counts = str.split("朋友").length();
借助正则表达式:
//需要import java.util.regex;
String str = "测试字符串";
String output = str;
String[] words_to_filter = {"敏感词1", "敏感词2"};
for(int i = 0;i < words_to_filter.length();i++){
String word = words_to_filter[i];
output = output.replaceAll(word, "**");
}
//需要import java.util.regex;
String str = "测试字符串";
String output = str;
String[] words_to_filter = {"敏感词1", "敏感词2"};
for(int i = 0;i < words_to_filter.length();i++){
String word = words_to_filter[i];
output = output.replaceAll(word, "**");
}
作者: 冷峻逸 时间: 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 |