第五次Java做業

能被三整除得數java

 

 

import java.util.Scanner;
public class Zuoye1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("請輸入一個數");
        int x = input.nextInt();
        int sum = 0;
        for (int i = 1; i < x; i++) {
          if (i%3 == 0) {
              sum=sum+i;
          }
      }
        System.out.println("0到100全部能被3整除的數之和爲:"+sum);
    }

}

 

 while作法spa

 

public class Zuoye1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int sum = 0;
        int i = 1;
        while(i<=100){
            if (i%3==0) {
              sum=sum+i;
          }
            i++;
        }
        System.out.println("0到100全部能被3整除的數之和爲:"+sum);
    }

}

 

 輸出0-9之間的數,可是不包括5。3d

 

public class Zuoye1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int i=1;
        while (i<=8){
            if(i!=5)
            System.out.println(i);
            i++;
        }
    }

}

 

 三、編寫一個程序,求整數n的階乘,例如5的階乘是1*2*3*4*5code

 

import java.util.Scanner;
public class Zuoye1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.println("輸入一個數");
        int n=input.nextInt();
        int sum=1;
        while (n>=1){
         sum*=n;
         n--;
     }
        System.out.println(sum);
    }

}

 

 

 

 四、編寫一個程序,輸入任意學生成績,若是輸入不合法(<0或者>100),提示輸入錯誤,從新輸入,直到輸入合法程序結束blog

import java.util.Scanner;
public class Zuoye1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.println("輸入學生成績");
        int x=input.nextInt();
        while (x<0||x>100){
             System.out.println("輸入錯誤");
          x=input.nextInt();
        }
            System.out.println("0k");
    }
}

 

 

 

假設某員工今年的年薪是30000元,年薪的年增加率6%。編寫一個Java應用程序計算該員工10年後的年薪,並統計將來10年(從今年算起)總收入。input

 

 

 

public class Zuoye1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int x=30000;
        double y=1.06;
        double z=x*y;
        double sum=30000;
        int a=1;
        while (a<=10){
            z*=y;
            sum+=z;
            a++;
        }
            System.out.println("十年後年薪是"+z);
            System.out.println("十年後總收入是"+sum);
    }

}

 

 

 六、打印出全部的"水仙花數",所謂"水仙花數"是指一個三位數,其各位數字立方和等於該數自己。例如:153是一個"水仙花數",由於153=1的三次方+5的三次方+3的三次方it

 

public class Zuoye1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int i = 100;
        while (i <= 999) {
            int ge = i % 10;
            int shi = i / 10 % 10;
            int bai = i / 100;

            if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i)
                System.out.println("水仙花數是"+i);
            i++;
        }
    }

}public class Zuoye1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int i = 100;
        while (i <= 999) {
            int ge = i % 10;
            int shi = i / 10 % 10;
            int bai = i / 100;

            if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i)
                System.out.println("水仙花數是"+i);
            i++;
        }
    }

}

 

 

 

 

import java.util.Scanner;
public class Zuoye1 {

    /**
     * @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 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:sum+=31;
            break;
            case 4:
            case 6:
            case 9:
            case 11:sum+=30;
            break;
            case 2:if(year%4==0&&year%400!=0||year%400==0){
                sum+=29;
            }else{
                sum+=28;
            }
            break;
            default:System.out.println("輸入的月份有誤");
            }
        
        }
    
        System.out.println("該天對應該年的天數爲"+(sum+day));

    }
}

 

 

 

 

import java.util.Scanner;
public class Zuoye1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("請輸入一個四位數");
        int n = input.nextInt();
        while (n < 1000 || n > 9999) {
            System.out.println("輸入的數字不合法,請從新輸入");
            n = input.nextInt();
            }
        int ge = n % 1000 % 100 % 10;
        int shi = n % 1000 % 100 / 10;
        int bai = n % 1000 / 100;
        int qian = n / 1000;
        int n1 = ge * 1000 + shi * 100 + bai * 10 + qian;
        System.out.println("反轉後爲");
        System.out.println(n1);
    }
}
相關文章
相關標籤/搜索