物件導向練習題

2022-08-22 14:15:10 字數 2563 閱讀 9243

class myyear(object):

definit(self):

self.year = int(input("請輸入年份:"))

def show_season(self):

for i in range(1, 13):

if i in (12, 1, 2):

print(i, "冬季")

elif i in (3, 4, 5):

print(i, "春季")

elif i in (6, 7, 8):

print(i, "夏季")

else:

print(i, "秋季")

def isleap(self):

if self.year % 4 == 0 and self.year % 100 != 0 or self.year % 400 == 0:

print(self.year, "是閏年")

else:

print(self.year, '不是閏年')

def year_sum(self):

a = 1

sum = 0

while a <= self.year:

sum += a

a += 1

print(sum)

def check_num(self):

if self.year == self.year[::-1]:

print(self.year,'是回文數,回文數是',self.year[::-1])

else:

print(self.year,"不是回文數")

def show_leap_year(self):

for i in range(1,self.year+1):

if i % 4 == 0 and i % 100 != 0 or i % 400 == 0:

print(i, "是閏年")

else:

pass

def show_num_div(self):

for i in range(1,self.year+1):

if i % 9 == 0 or '9' in str(i) :

print(i)

else:

pass

# b =myyear()

# b.year_sum()

# b.show_leap_year()

# b.show_num_div()

class mynum(object):

definit(self):

self.num = int(input("請輸入乙個整數數字:"))

def show_num(self):

for i in range(1,self.num+1):

print(i)

def calc_sum(self):

a = 1

sum = 0

while a<= self.num:

sum += a

a +=1

print(sum)

def calc_odd_sum(self):

a = 1

sum = 0

while a <= self.num:

if a%2==1:

sum += a

a +=2

else:

pass

print(sum)

def calc_even_sum(self):

sum = 0

for i in range(1,self.num+1):

if i%2==0:

sum += i

i += 1

print(sum)

def show_num_div(self):

for i in range(1,self.num+1):

if i%7==0:

print(i)

def check_num(self):

if self.num == self.num[::-1]:

print(self.num,'是回文數,回文數是',self.num[::-1])

else:

print(self.num,"不是回文數")

ifname== 'main':

c=mynum()

# c.calc_sum()

# c.calc_odd_sum()

# c.calc_even_sum()

# c.show_num_div()

c.check_num()

物件導向練習題

一 讀程式 1 寫出下列 的執行結果 class fatherclass public class test extends fatherclass public static void main string args 結果 fatherclass create fatherclass creat...

物件導向 練習題1

package com.lrq import org.omg.portableserver.threadpolicyoperations author 李瑞琦 1.請定義乙個交通工具 vehicle 的類 其中有 屬性 速度 speed 體積 size 等,方法 移動 move 設定速度 setsp...

物件導向練習題答案

答案整理如下 一 類和物件 1.定義乙個類demo,其中定義乙個求兩個資料和的方法,定義乙個測試了test,進行測試。class demo public int add class test 2.定義乙個長方形類,定義 求周長和面積的方法,然後定義乙個測試了test2,進行測試。class rect...