⑴ 怎麼往jsp里傳遞一個值 在取出來request.setAttribute("stus", stus);
stus.size() 得到是 0 ?
如果是0的話,你就成功了。
因為如果沒成功,就該報錯了
沒成功的話 stus 就是 null,根本無法調用size方法
⑵ 如何在java中將一個數組轉換為一個集合
java.util.Arrays
Student[] stus = new Student[10];
ArrayList list = new ArrayList();
list = listArrays.asList(stus);
這樣就可以把數組stus轉換為List了
⑶ 定義一個Student類,包括姓名、學號、成績三個成員變數以及getName()、getNo()、getScore()三個方法。在構
//參考程序:
#include"iostream"
#include"string"
usingnamespacestd;
classStudent
{
private:
intno; //學號
stringname; //姓名
intscore; //成績
public:
Student()
{}
Student(intno,stringname,intscore)
{
this->no=no;
this->name=name;
this->score=score;
}
stringgetName()
{
returnname;
}
intgetNo()
{
returnno;
}
intgetScore()
{
returnscore;
}
voiddisplay()
{
cout<<no<<" "<<name<<" "<<score<<endl;
}
};
voidsort(Studentstus[],intlen)
{
inti,j,k;
Studenttemp;
for(i=0;i<len-1;i++)
{
k=i;
for(j=i+1;j<len;j++)
if(stus[j].getScore()>stus[k].getScore())
k=j;
if(k!=i)
{
temp=stus[k];
stus[k]=stus[i];
stus[i]=temp;
}
}
}
voidmain()
{
inti;
intlen=5;
Studentstus[20];
intno;
stringname;
intscore;
cout<<"請輸入20個學生的信息(學號、姓名、成績):"<<endl;
for(i=0;i<len;i++)
{
cout<<"No."<<i+1<<":";
cin>>no>>name>>score;
stus[i]=Student(no,name,score);
}
sort(stus,len);
cout<<"學生信息一覽(按成績降序):"<<endl;
for(i=0;i<len;i++)
{
stus[i].display();
}
}
運行結果:
⑷ 定義一個學生類,包含學號、姓名、平時成績和考核成績四個數據成員和以下成員方法(用python語言):
花了不少時間寫的,挺詳細的,希望採納。
#引入operator模塊,用於給集合排序
importoperator
#單行注釋用"#",多行注釋用'''注釋內容'''
#定義一個學生類,類名用駝峰命名法
classStudent:
#構造方法,可用來創建對象格式def__init__(self,參數)參數個數自已定義,類型系統自動識別
def__init__(self,stu_no,name,base_score,exam_score):
self.stu_no=stu_no#對象屬性賦值
self.name=name
self.base_score=base_score
self.exam_score=exam_score
#定義計算總評函數定義函數格式def函數名(self,參數),self代表本對象
defget_last_score(self):
#return指定返回值,無return表示此函數無返回值
returnself.base_score*0.3+self.exam_score*0.7
#類似toString方法,列印對象時,調用對象的該方法
def__str__(self):
return'學號:'+self.stu_no+'姓名:'+self.name+",平時成績:"+str(self.base_score)+",考核成績:"+str(self.exam_score)
#定義函數,將對象集合寫到文件,上面三個函數有縮進,屬於Student類的函數,本函數屬於全局函數
defprint_to_file(path,stu_arr):
#打開文件,操作完成後自動關閉文件
withopen(path,'w')asfile:
#調用operator給集合排序
sort_attr=operator.attrgetter('stu_no')#指定排序屬性
stu_arr.sort(key=sort_attr)#排序
forstuinstu_list:
str=stu.__str__()#將對象轉換為字元串
file.write(str+' ')#將字元串寫入文件
#主函數,運行的入口
if__name__=='__main__':
#創建幾個學生對象,按__init__的順序輸入參數
s1=Student('1001','zhangsan',31,69)
s2=Student('1003','wangwu',28,32)
s3=Student('1004','zhaoliu',77,78)
s4=Student('1002','lisi',19,89)
#創建集合
stu_list=[s1,s2,s3,s4]
#文件路徑
f='d:\aaa.txt'
print_to_file(f,stu_list)
⑸ python 求大神解答
##注意:最左邊每個=表示一個空格
stus=[]
n=input('Please input the number of students:')
for i in range(int(n)):
====nm=input("Please input student's name:")
====gd=input("Please input student's gender:")
====stus.append({'name':nm,'gender':gd})
print("{} students'information has been input".format(n))
print(stus)
⑹ 下面我寫的python哪錯了
#coding:utf-8
importrandom
importmath
#代表宿舍,每個宿舍有兩個可用的隔間
dorms=['zeus','athena','hercules','bacchus','pluto']
#代表學生及其首選和次選
prefs=dict([
('toby',('bacchus','hercules')),
('steve',('zeus','pluto')),
('andrea',('athena','zeus')),
('sarah',('zeus','pluto')),
('dave',('athena','bacchus')),
('jeff',('hercules','pluto')),
('fred',('pluto','athena')),
('suzie',('bacchus','hercules')),
('laura',('bacchus','hercules')),
('neil',('hercules','athena'))
])
person_dom_index={}
defgetsolution(dorms,prefs,result={}):
#按學生選擇的房間分組
subresult={}
forstu,selectioninprefs.items():
dom=selection[0]
ifdomnotindorms:
continue
subresult.setdefault(dom,[]).append(stu)
fordom,stusinsubresult.items():
random.shuffle(stus)
while1:
stu=stus.pop(0)
prefs.pop(stu)
result.setdefault(dom,[]).append(stu)
iflen(result[dom])==2:
#房間滿員
dorms.pop(dorms.index(dom))
break
ifnotstus:
#無其他學生選擇
break
returnresult
#按學生首選意願分配宿舍
sol=getsolution(dorms,prefs)
#未分配的學生
#按學生次選意願分配宿舍
prefs_=dict([(p,x[1:])forp,xinprefs.items()])
ifprefs:
sol=getsolution(dorms,prefs,sol)
#剩餘宿舍學生隨機調配
random.shuffle(dorms)
prefs=prefs.keys()
random.shuffle(prefs)
fordom,stuinzip(dorms,prefs):
sol[dom].append(stu)
print" ##按宿舍展示結果##"
fordom,stusinsorted(sol.items()):
print"%10s:%r"%(dom,stus)
print" ##按學生展示結果##"
fordom,stusinsorted(sol.items()):
forstuinstus:
print"%10s:%s"%(stu,dom)
>python-u"tester.py"
###按宿舍展示結果##
#athena:['andrea','dave']
#bacchus:['toby','suzie']
#hercules:['neil','jeff']
#pluto:['fred','laura']
#zeus:['steve','sarah']
#
###按學生展示結果##
#andrea:athena
#dave:athena
#toby:bacchus
#suzie:bacchus
#neil:hercules
#jeff:hercules
#fred:pluto
#laura:pluto
#steve:zeus
#sarah:zeus
#>Exitcode:0Time:0.025
換個思路看看
⑺ python 求平均年齡
用到print字元串小數點方法
n = int(input())
stus = []
num = 0
for i in range(0, n):
temp = int(input())
stus.append(temp)
num += temp
pass
print("%.2f"%(num / n))
⑻ 屬於國家保護的野生動物
國家一級重點保護野生動物中華鱘生存處境仍然堪憂。這是中國水產科學研究院長江水產研究所所長張顯良研究員25日在中國自然科學博物館協會水族館專業委員會2006年年會暨北京海洋館第二屆國際學術交流會上透露的。
中華鱘在海洋中生長發育,在江河中產卵繁殖,江河產卵孵化的幼鱘和產後親鱘又洄遊至海洋棲息和肥育,是一種典型的溯河產卵洄遊魚類。
據張顯良介紹,葛洲壩截流前,中華鱘產卵場分布在長江上游600公里江段。1981年葛洲壩截流後,中華鱘被滯留在壩下。20多年的監測表明,葛洲壩截流以後,在葛洲壩下游江段僅發現一處中華鱘產卵場,且江段長度僅7公里,面積相當狹小。「再加上近年來經濟建設高速發展,人類活動加劇,水域水質污染日趨嚴重,導致中華鱘的資源狀況逐漸衰退。」根據長江水產研究所的調查,每年洄遊至長江的中華鱘產卵群體數量,已從葛洲壩截流初期的約2176尾,下降到目前的每年500尾左右。
「中華鱘自然種群數量逐年下降的趨勢並沒有得到根本緩解,中華鱘的保護還面臨著許多問題。」張顯良說,中華鱘分布較廣,管理難度大,以及水利工程建設、水質污染、長江及近海密集漁撈等問題近期無法完全消除,是造成這一現象的客觀因素。此外,我國每年投入中華鱘保護的資金有限,人工繁殖中華鱘產後親鱘的恢復等困擾人們多年的難題,因設施條件限制無法著手解決,一些有效保護措施也因資金限制而無法正常開展。探索更多的保護技術和方法,尋找更多的保護途徑,是加強中華鱘物種保護的必由之路。
⑼ python如何讀取excel文件
1.首先說明我是使用的python3.5,我的office版本是2010,首先打開dos命令窗,安裝必須的兩個庫,命令是:
pip3 install xlrd
Pip3 install xlwt
2.准備好excel,例如我的一個工作文件,我放在D盤/網路經驗/11.xlsx,只有一個頁簽A,內容是一些銷售數據
3.打開pycharm,新建一個excel.py的文件,首先導入支持庫
import xlrdimport xlwt
4.針對剛入門的新手,先介紹三個知識,第一個:獲取excel的sheet名稱,第二:獲取excel行數與列數,第三:獲取第幾行第幾列的具體值,這是最常用的三個知識點
5.貼出代碼,具體分析:
(1)要操作excel,首先得打開excel,使用open_workbook(‘路徑’)
(2)要獲取行與列,使用nrows(行),ncols(列)
(3)獲取具體的值,使用cell(row,col).value
workbook=xlrd.open_workbook(r'E:11.xlsx')print (workbook.sheet_names()) sheet2=workbook.sheet_by_name('A') nrows=sheet2.nrows ncols=sheet2.ncols print(nrows,ncols) cell_A=sheet2.cell(1,1).value print(cell_A)
6.要在excel里寫入值,就要使用write屬性,重點說明寫入是用到xlwt這個支援庫,思路是先新建excel,然後新建頁簽B,然後將一組數據寫入到B,最後保存為excel.xls,這里建議保存為2003的格式,大部分電腦都能打開,特別注意保存的excel的路徑是在python工作文件的目錄下面,貼出代碼:
stus = [['年', '月'], ['2018', '10'], ['2017', '9'], ['2016', '8']]Excel = xlwt.Workbook() # 新建excelsheet = Excel.add_sheet('B') #新建頁簽Brow = 0for stu in stus: col = 0 for s in stu: sheet.write(row, col, s) #開始寫入 col = col + 1 row = row + 1Excel.save('Excel.xls') #保存
關於python如何讀取excel文件,青藤小編就和您分享到這里了。如果您對python編程有濃厚的興趣,希望這篇文章可以為您提供幫助。如果您還想了解更多關於python編程的技巧及素材等內容,可以點擊本站的其他文章進行學習。
⑽ 求解Python,感謝大神
keyword=input('請輸入三位數的正整數:')
iflen(keyword)!=3:
print('-1')
else:
print(int(keyword[::-1]))