大三下学期刚开始学习java,老师布置的作业。
Class SumOfArgs
Create a class named SumOfArgs that will print out the sum of all integer arguments found on the command line. It should have a main() method so the class can be run. Anything that is not an integer should be skipped (should not contribute to the sum). It should print out only the sum (nothing else!). No error messages can be printed out (no matter what the command line args look like). Examples of what your class should do when run:
> java SumOfArgs
0
> java SumOfArgs 10 9 8
27
> java SumOfArgs hello dave 1 2 3
6
> java SumOfArgs Hello World
0
Note:Integer.parseInt(String s) parses the string argument as a signed decimal integer.
my code:(这是除了hello world之外,我的第二个java代码,挺简单的)
public class SumOfArgs {
static int num=0;
static int sum=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
for(String s:args){
try{
num=Integer.parseInt(s);
sum=sum+num;
}
catch (Exception ex)
{
}
}
System.out.print(sum);
}
}
note:static关键字可以解决两种情形。第一,只想为某特定域分配单一存储空间,而不去考虑究竟要创建多少对象,甚至根本就不创建任何对象。第二,希望某个方法不予包含它的类的任何对象关联在一起。也就是说,即使没有创建对象,也能调用这个方法。
foreach语法用于数组和容器,表示不必创建int变量去对由访问项构成的序列进行计数,foreach将自动产生每一项。
eg.for(float x: f)
这个语句定义了一个float类型的变量 x,继而将每一个 f 的元素赋值给 x。f 应该为数组。
题目要求不输出错误信息,于是用try{}catch{}语句,对抛出的异常不作处理。
Class Book, Course, Student
Create three classes named Book, Course and Student to perform course-selecting system.Student has at least three overloaded constructors with different parameter list. Student also has some members of Book.
Examples:
> java Course 13131001 Java
13121001 choose Java
> java Course 13131001 Java WebEngineering
13131001 choose Java and WebEngineering
Notes:You really need to add more information inside each class to get this working!
my code:
class Student{
long student_id;
String student_name;
//Book book=new Book("Thinking in Java");
Student(long id){
this.student_id=id;
}
Student(String name){
this.student_name=name;
}
Student(long id,String name){
this.student_id=id;
this.student_name=name;
}
}
class Book{
public enum book_type{
PAPAR,ELECTRIC
}
String book_name;
Book(String name){
this.book_name=name;
System.out.println("the book's name is 《"+name+"》");
}
Book(String name,book_type type){
this.book_name=name;
System.out.println("the book's is 《"+name+"》 and it's a(an) "+type);
}
}
public class Course {
Student stu;
String course_name;
Course(long id,String[] str){
stu=new Student(id);
System.out.print(stu.student_id+" choose "+str[1]);
for(int p=2;str[p]!=null;p++){
System.out.print(" and "+str[p]);
}
}
Course(String[] str){
stu=new Student(str[0]);
System.out.print(stu.student_name+" choose "+str[1]);
for(int p=2;str[p]!=null;p++){
System.out.print(" and "+str[p]);
}
}
@SuppressWarnings("unused")
public static void main(String[] args) {
// TODO Auto-generated method stub
int i=0;
long n=0;
String[] str=new String[9];
for(String s:args){
try{
n=Long.parseLong(s);
}
catch (Exception ex){
}
str[i]=s;
i++;
}
Course course=new Course(n,str);
}
}
主函数里只实现了examplels中选课的模样,其实还有Class Book没有用到。其实题目的意思我也不太懂,我写了Student的三个重载的构造函数,算符合题目要求吧。