JAVA----代碼練習(3.6)

1到 100 的所有整數中出現多少個數字9

public class Vebr {
    public static void main(String[] args) {
        int count = 0;//存儲9的個數
        for(int i = 1; i <= 100; i++) {
           if(i%10 == 9) {
               count++;
           }
           if(i/10 == 9) {
               count++;
           }
        }
       System.out.println(count);
    }
}

1000 - 2000 之間所有的閏年

public class LeapYear {
    public static void main(String[] args) {
       for (int i = 1000; i <= 2000; i++) {
           if( (i%4 == 0)&&(i%100 != 0 )||(i%400 == 0)) {
               System.out.println(i);
           }
        }
    }
}

判定一個數字是否是素數

public class PrimeNumber {
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       int x = scan.nextInt();
       int i=1;
       if (x == 1 || x == 0) {
           System.out.println(x + "既不是素數也不是合數");
       } else {
           for (i = 2; i <= x/2; i++) {
               if (x % i == 0) {
                   System.out.println(x + "不是素數");
                   break;
               }
           }
           if (i >= x/2) {
               System.out.println(x + "是素數");
           }
       }
   }
}

1 - 100 之間所有的素數

public class PrimeNumber {`在這裏插入代碼片`
    public static void main(String[] args) {
        for (int x = 2; x <= 100; x++) {
            int i = 1;
            for ( i = 2; i < x/2; i++) {
                if (x % i == 0) {
                   break;
                }
            }
            if(i >= x/2) {
                System.out.println(x);
            }
        }
    }
}

輸入年齡, 打印出當前年齡的人是少年(低於18), 青年(19-28), 中年(29-55), 老年(56以上)

public class JudgementAge {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int age = scan.nextInt();
        if(age<=18) {
            System.out.println("少年");
        }
        if(age>18 && age <=28) {
            System.out.println("青年");
        }
        if(age>=29 && age<=55) {
            System.out.println("中年");
        }
        if(age>=56) {
            System.out.println("老年");
        }
    }
}

猜數字遊戲 ,用戶輸入數字,判斷該數字是大於,小於,還是等於隨機生成的數字,等於的時候退出程序。

public class NumberGame {
    public static void main(String[] args) {
        Random rand = new Random();
        int x = rand.nextInt(100);
        //System.out.println(x);//用來輸出產生的隨機數
        System.out.println("請輸入數字:");
        Scanner scan = new Scanner(System.in);
        while (scan.hasNext()) {
            int i = scan.nextInt();
            if (i < x) {
                System.out.println("猜小了");
            } else if (i > x) {
                System.out.println("猜大了");
            } else {
                System.out.println("猜對了");
                break;
            }
        }
    }
}

水仙花數

public class NarcissisticNumber {
    public static void main(String[] args) {
        int i = 0;
        for (i = 100; i <= 1000; i++) {
           int basic = i % 10;
           int tens = (i / 10) % 10;
           int hundred = (i / 100) % 10;
           if (i == basic*basic*basic + tens*tens*tens + hundred*hundred*hundred) {
                System.out.println(i);
            }
        }
    }
}
水仙花數優化
public class NumberGame {
    public static void main(String[] args) {
        for (int i = 0; i <= 999999; i++) {
            //判斷每一個數字i是不是xxxx數
            int count = 0;//數字的位數
            int tmp = i;//先保存i到tmp
            //1.求每個數字的每一位並計數有多少位
            while (tmp != 0) {
                count++;
                tmp = tmp / 10;
            }
            //2.對每一位用於^count運算
            tmp = i;
            int sum = 0;
            while (tmp != 0) {
                //sum = sum + (Math.pow(i%10,count));//1^count
                sum += Math.pow(tmp % 10, count);
                tmp = tmp / 10;
            }
            //3.sum就是每個位上數字的次方和與原數字進行比較相等則是所求數字
            if (sum == i) {
                System.out.println(sum + "是要找的數字!");
            }
        }
    }
}

計算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值

public class  FractionAdd {
    public static void main(String[] args) {
        int a=1;
        double add=0;
        for (int i = 1; i <= 100; i++) {
          add=(1.0*a)/i;
          a=-a;
        }
        System.out.println(add);
    }
}

求兩個正整數的最大公約數

public class CommonDivisor {
    public static void main(String[] args) {

        System.out.println("請輸入兩個數:");
        Scanner scan = new Scanner(System.in);
        int x = scan.nextInt();
        int y = scan.nextInt();;
        int z = x % y;
        if (z == 0) {
            System.out.println("最大公約數是:"+ y);
        }
        if(z != 0) {
           int  i = y % z;
            if(i != 0) {
                i = z % i;
                i = y % z;
                System.out.println("最大公約數是:"+ i);
            } else {
                i = y % z;
                System.out.println("最大公約數是:"+ z);
            }
        }
    }
}

二進制1的個數

public class OneBit {
    public static void main(String[] args) {
        System.out.println("請輸入一個數:");
        Scanner scan = new Scanner(System.in);
        int i=scan.nextInt();
        int count = 0;
        while (i != 0) {
            if ((i & 1) == 1) {
                count++;
                i = i >> 1;
            } else {
                i = i >> 1;
            }
        }
        System.out.println(count);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章