Python基礎
小程序設計
這裏我使用的是eclipse來寫代碼,因爲在練習基礎知識的時候還可以順帶着練習一下java編程,畢竟程序語言的編程思想都是想通的
python篇
def work1():
it=0
while(it<10):
it+=1
if(it==7):
continue
print(it)
def work2():
it=0
sum=0
while(it<100):
it+=1
sum+=it
print(sum)
def work3():
it=0
while(it<100):
it+=1
if(it%2==1):
print(it)
def work4():
it=0
while(it<100):
it+=1
if(it%2==0):
print(it)
def work5():
sum=0
it=0
while(it<99):
it+=1
if(it%2==0):
sum+=it
else:
sum-=it
print(sum)
def work6():
count=0
while(True):
username=input('請輸入您的用戶名 ')
password=input('請輸入你的密碼 ')
if(username=='zhangsan' and password=='123456'):
print('登陸成功')
break
else:
count+=1
if(count>=3):
print('登陸失敗,三次機會已用完,請明天再試')
break
print('登陸失敗,請重新嘗試(剩餘機會-->%d次)'% (3-count))
if __name__=='__main__':
work1()#使用while循環輸出1 2 3 4 5 6 8 9 10
print('===========')
work2()#求1-100的所有數的和
print('===========')
work3()#輸出 1-100 內的所有奇數
print('===========')
work4()#輸出 1-100 內的所有偶數
print('===========')
work5()#求1-2+3-4+5 ... 99的所有數的和
print('===========')
work6()#用戶登陸(三次機會重試)
Java篇
public class Work_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
work1();//使用while循環輸出1 2 3 4 5 6 8 9 10
System.out.println("========");
work2();//求1-100的所有數的和
System.out.println("========");
work3();//輸出 1-100 內的所有奇數
System.out.println("========");
work4();//輸出 1-100 內的所有偶數
System.out.println("========");
work5();//求1-2+3-4+5 ... 99的所有數的和
}
public static void work1(){
int it = 0;
while (it<10) {
it++;
if (it==7) {
continue;
}
System.out.println(it);
}
}
public static void work2(){
int sum = 0;
int it = 0;
while (it<100) {
it++;
sum+=it;
}
System.out.println(sum);
}
public static void work3(){
int it = 0;
while (it<100) {
it++;
if (it%2==1) {
System.out.println(it);
}
}
}
public static void work4(){
int it = 0;
while (it<100) {
it++;
if (it%2==0) {
System.out.println(it);
}
}
}
public static void work5(){
int sum = 0;
int it = 0;
while (it<99) {
it++;
if (it%2==0) {
sum+=it;
}else {
sum-=it;
}
}
System.out.println(sum);
}
}