天天看點

第五次作業

1.分别使用for循環,while循環,do循環求1到100之間所有能被3整除的整數的和。(知識點:循環語句)

第五次作業

package wyf1;

public class work1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
int i=1;
int sum=0;

while(i<101)
{
if(i%3==0)    
    sum=sum+i;
i++;
    
}
System.out.println(sum);
    }

}      
第五次作業
第五次作業

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

package wyf1;

public work

1 {

/**

* @param args

*/

public

static

void

main(String[] args) {

// TODO Auto-generated method stub

int

i=1;

int

sum=0;

do

{

if

(i%3==0) 

sum=sum+i;

i++;

}

while

(i<101);

System.

out

.println(sum);

}

}

 

第五次作業

package wyf1;

public

class work

1 {

/**

* @param args

*/

public

static

void

main(String[] args) {

// TODO Auto-generated method stub

int

i=1;

int

sum=0;

for

(i=1;i<101;i++){

if

(i%3==0) 

sum=sum+i;

}

System.

out

.println(sum);

}

  

第五次作業

 2.輸出0-9之間的數,但是不包括5。(知識點:條件、循環語句)

package wyf1;

public

class work

2 {

/**

* @param args

*/

public

static

void

main(String[] args) {

// TODO Auto-generated method stub

int

i=0;

while

(i<10){

if

(i!=5)

System.

out

.println(i);

i++;

}

}

第五次作業

 3.編寫一個程式,求整數n的階乘,例如5的階乘是1*2*3*4*5(知識點:循環語句)

package wyf1;

public

class work

3 {

/**

* @param args

*/

public

static

void

main(String[] args) {

// TODO Auto-generated method stub

int

i=1;

int

sum=1;

while

(i<6){

sum*=i;

i++;

}

System.

out

.println(sum);

}

}

第五次作業

 4.編寫一個程式,輸入任意學生成績,如果輸入不合法(<0或者>100),提示輸入錯誤,重新輸入,直到輸入合法程式結束(知識點:循環語句)

22

23

24

package wyf1;

import java.util.Scanner;

public

class work

4 {

/**

* @param args

*/

public

static

void

main(String[] args) {

// TODO Auto-generated method stub

Scanner input=

new

Scanner(System.

in

);

System.

out

.println(

"請輸入一個數a"

);

int

a=input.nextInt();

while

(a<0||a>100){

System.

out

.println(

"輸入錯誤,重新輸入"

);

a=input.nextInt();

}

System.

out

.println(

"輸入合法"

);

}

}

第五次作業

 5.假設某員工今年的年薪是30000元,年薪的年增長率6%。編寫一個Java應用程式計算該員工10年後的年薪,并統計未來10年(從今年算起)總收入。(知識點:循環語句)

package wyf1;

public

class work

 {

/**

* @param args

*/

public

static

void

main(String[] args) {

// TODO Auto-generated method stub

double

a=1;

double

b=30000,sum=0;

for

(a=1;a<11;a++)

{

b= b*(1+0.06);

sum+=b;

}

System.

out

.println(

"10年後年薪"

+b);

System.

out

.println(

"10年後總年薪"

+sum);

}

}

第五次作業

 作業

1.列印出所有的"水仙花數",所謂"水仙花數"是指一個三位數,其各位數字立方和等于該數本身。例如:153是一個"水仙花數",因為153=1的三次方+5的三次方+3的三次方。(知識點:循環語句、條件語句)

第五次作業
package wyf1;

public class work6 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
int a=100;


while(a<1000){
    int bai=a/100;
    int shi=a/10%10;
    int ge=a%10;
    if(ge*ge*ge+shi*shi*shi+bai*bai*bai==a)
    System.out.println(a);    
    a++;
}

    }

}      
第五次作業
第五次作業

2.輸入年月日,判斷這是這一年中的第幾天(知識點:循環語句、條件語句)

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

import java.util.Scanner;

public

class work

2 {

/**

* @param args

*/

public

static

void

main(String[] args) {

// TODO Auto-generated method stub

Scanner input=

new

Scanner(System.

in

);

System.

out

.println(

"請輸入年份"

);

int

year=input.nextInt();

System.

out

.println(

"請輸入月份"

);

int

month=input.nextInt();

System.

out

.println(

"請輸入日"

);

int

day=input.nextInt();

int

sum=0;

for

(

int

i=1;i<month;i++){

switch

(i){

case

4:

case

6:

case

9:

case

11:

sum=sum+30;

break

;

case

2:

if

(year%4==0&&year%100!=0||year%400==0)

sum=sum+29;

else

sum=sum+28;

break

;

default

:sum=sum+30;

break

;

}

sum=sum+day;

System.

out

.println(year+

"年中的第"

+sum+

"天"

);

}

}

第五次作業

 3.由控制台輸入一個4位整數,求将該數反轉以後的數,如原數為1234,反轉後的數位4321(知識點:循環語句、條件語句)

import java.util.Scanner;

public

class work

3 {

/**

* @param args

*/

public

static

void

main(String[] args) {

// TODO Auto-generated method stub

Scanner input=

new

Scanner(System.

in

);

System.

out

.println(

"請輸入一個四位數"

);

int

a=input.nextInt();

while

(a<1000||a>9999){

System.

out

.println(

"輸入不合法請重新輸入"

);

a=input.nextInt();

}

int f

=a%10;

int c

=a%100/10;

int

b=a%1000/100;

int h

=a/1000;

System.

out

.println(

"反轉後的數為"

+f+c+b+h);

}

}

第五次作業