大三下學期剛開始學習java处窥,老師布置的作業(yè)遂跟。
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關鍵字可以解決兩種情形。第一蛀序,只想為某特定域分配單一存儲空間,而不去考慮究竟要創(chuàng)建多少對象活烙,甚至根本就不創(chuàng)建任何對象徐裸。第二,希望某個方法不予包含它的類的任何對象關聯(lián)在一起啸盏。也就是說重贺,即使沒有創(chuàng)建對象,也能調(diào)用這個方法回懦。
foreach語法用于數(shù)組和容器气笙,表示不必創(chuàng)建int變量去對由訪問項構成的序列進行計數(shù),foreach將自動產(chǎn)生每一項怯晕。
eg.for(float x: f)
這個語句定義了一個float類型的變量 x潜圃,繼而將每一個 f 的元素賦值給 x。f 應該為數(shù)組舟茶。
題目要求不輸出錯誤信息谭期,于是用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);
}
}
主函數(shù)里只實現(xiàn)了examplels中選課的模樣隧出,其實還有Class Book沒有用到。其實題目的意思我也不太懂阀捅,我寫了Student的三個重載的構造函數(shù)胀瞪,算符合題目要求吧。