java homework 1

大三下學期剛開始學習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 的元素賦值給 xf 應該為數(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ù)胀瞪,算符合題目要求吧。

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末饲鄙,一起剝皮案震驚了整個濱河市赏廓,隨后出現(xiàn)的幾起案子涵紊,更是在濱河造成了極大的恐慌,老刑警劉巖幔摸,帶你破解...
    沈念sama閱讀 216,919評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異颤练,居然都是意外死亡既忆,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評論 3 392
  • 文/潘曉璐 我一進店門嗦玖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來患雇,“玉大人,你說我怎么就攤上這事宇挫】林ǎ” “怎么了?”我有些...
    開封第一講書人閱讀 163,316評論 0 353
  • 文/不壞的土叔 我叫張陵器瘪,是天一觀的道長翠储。 經(jīng)常有香客問我,道長橡疼,這世上最難降的妖魔是什么援所? 我笑而不...
    開封第一講書人閱讀 58,294評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮欣除,結果婚禮上住拭,老公的妹妹穿的比我還像新娘。我一直安慰自己历帚,他們只是感情好滔岳,可當我...
    茶點故事閱讀 67,318評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著挽牢,像睡著了一般谱煤。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上卓研,一...
    開封第一講書人閱讀 51,245評論 1 299
  • 那天趴俘,我揣著相機與錄音,去河邊找鬼奏赘。 笑死寥闪,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的磨淌。 我是一名探鬼主播疲憋,決...
    沈念sama閱讀 40,120評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼梁只!你這毒婦竟也來了缚柳?” 一聲冷哼從身側響起埃脏,我...
    開封第一講書人閱讀 38,964評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎秋忙,沒想到半個月后彩掐,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,376評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡灰追,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,592評論 2 333
  • 正文 我和宋清朗相戀三年堵幽,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片弹澎。...
    茶點故事閱讀 39,764評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡朴下,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出苦蒿,到底是詐尸還是另有隱情殴胧,我是刑警寧澤,帶...
    沈念sama閱讀 35,460評論 5 344
  • 正文 年R本政府宣布佩迟,位于F島的核電站团滥,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏音五。R本人自食惡果不足惜惫撰,卻給世界環(huán)境...
    茶點故事閱讀 41,070評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望躺涝。 院中可真熱鬧厨钻,春花似錦、人聲如沸坚嗜。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽苍蔬。三九已至诱建,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間碟绑,已是汗流浹背俺猿。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留格仲,地道東北人押袍。 一個月前我還...
    沈念sama閱讀 47,819評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像凯肋,于是被迫代替她去往敵國和親谊惭。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,665評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法圈盔,內(nèi)部類的語法豹芯,繼承相關的語法,異常的語法驱敲,線程的語...
    子非魚_t_閱讀 31,625評論 18 399
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理铁蹈,服務發(fā)現(xiàn),斷路器癌佩,智...
    卡卡羅2017閱讀 134,654評論 18 139
  • 一. Java基礎部分.................................................
    wy_sure閱讀 3,810評論 0 11
  • 一木缝、 1、請用Java寫一個冒泡排序方法 【參考答案】 public static void Bubble(int...
    獨云閱讀 1,369評論 0 6
  • 水中花朵眼中深情围辙, 最純潔的天使便是隨去, 沒有芬芳插在心央放案, 唯有你來我往姚建。 ...
    綠葉的深情閱讀 183評論 0 0