JAVA學習---阿爾法平臺練習(第三週課內作業)

聲明:代碼僅供參考學習使用<歡迎向需要的人分享>

獲取輸入的整型數據

Scanner nextInt

計算面積周長

編寫程序,輸入正方形的邊長,計算這個正方形的周長和麪積,並將計算結果打印輸出。
輸入示例:
10
輸出示例:
邊長: 10
周長: 40
面積: 100

import java.util.Scanner;
public class Program
{
    public static void main(String args[])
    {
        Scanner as = new Scanner(System.in);
        int a= as.nextInt();
        System.out.println("邊長:"+a);
        System.out.println("周長:"+4*a);
        System.out.println("面積:"+a*a);
    }
}

從尾到頭

從鍵盤輸入一個小於 500 的三位整數,將這個整數乘 2 得到一個新的數,將這個新的數從尾到頭倒序輸出。
例如:
輸入
123
那麼:程序計算123*2等於246;
倒序輸出爲:
642

import java.util.Scanner;

public class Program {
  public static void main(String[] args){
   Scanner sc = new Scanner(System.in);
          int a = sc.nextInt();
      int sum,ge,shi,bai;
      sum = 2 * a;
      ge=sum % 10 ;
      shi = (sum / 10) % 10;
      bai=sum /100;
      sum= ge * 100 + shi * 10 + bai;
      System.out.printf("%d%d%d",ge,shi,bai);
  }
}

實現加密器

請用程序實現
輸入一個整數,將輸入的數字進行加密,並將加密後的結果輸出。
以下爲加密的規則:
加密結果 = (整數 * 10 + 5) / 2 + 3
示例 1
輸入
20
輸出
105
示例 2
輸入
35
輸出
180

import java.util.Scanner;

public class Program
{
    public static void main (String[] args)
    {
        // 輸入一個整數
        Scanner sa = new Scanner(System.in);
        int a= sa.nextInt();
        int c;
        c=(a*10+5)/2+3;
        // 將輸入的整數進行加密,並將加密結果輸出
       System.out.println(c); 
    }
}

輸出Yeah

if out

輸出較大的值

if x else

年齡換算

狗是人類的好朋友,一隻狗大約可以生存 15 至 20 年,狗狗在 2 歲時,已經成年。爲了對狗的年齡有個直觀認識,有一個簡單的換算辦法能將狗的年齡換算爲人類的年齡:
狗狗 1 歲時,相當於人類 15 歲,2 歲時相當於人類 24 歲,此後,狗狗每長 1 歲,都相當於人長 4 歲。
對比如下:
狗的年齡
人類的年齡
1 15
2 24
3 28
4 32
5 36
… …
請用程序實現
輸入狗狗的年齡,計算並輸出對應的人類的年齡。
示例 1
輸入
3
輸出
28
示例 2
輸入
6
輸出
40

import java.util.Scanner;

public class Program {
  public static void main(String[] args) {
      Scanner as = new Scanner(System.in);
      int a=as.nextInt();
      if(a==1){
              System.out.println("15");
      }
      else if(a==2){
              System.out.println("24");
      }
      else{
              System.out.println(24+(a-2)*4);
      }
      
  }
}

判斷平閏年

如果一個年份可以被 4 整除且不能被 100 整除,或者可以被 400 整除,那麼這個年份就是閏年。
請用程序實現
輸入一個年份year,判斷它是「平年」還是「閏年」。如果是平年,輸出common year;如果是閏年,輸出leap year。
示例 1
輸入
2000
輸出
leap year
示例 2
輸入
1990
輸出
common year

import java.util.Scanner;
public class Program {
  public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       int y = scanner.nextInt();
      if(y%4==0&&y%100!=0||y%400==0){
          System.out.println("leap year");
      }
      else{
          System.out.println("common year");
      }
  }
}

成績評級

輸入一個成績 score,根據 score 的分數,判斷成績等級,並將成績等級輸出。
注意: 如果所輸成績超過範圍,提示:輸入的成績必須在0-100之間。
成績等級如下所示:
A:[90-100]
B:[80-89]
C:[70-79]
D:[60-69]
E:[0~59]
示例:
輸入
95
輸出
A

import java.util.Scanner;

public class Program {
  public static void main(String[] args) {
      int x;
      int grade = 0;
      Scanner sc = new Scanner(System.in);
      int score = sc.nextInt();
      int d;
      d=score/10;
      switch (d)
          {
              case 10:
                  System.out.println("A");
                  break;
              case 9:
                  System.out.println("A");
                  break;
              case 8:
                  System.out.println("B");
                  break;
              case 7:
                  System.out.println("C");
                  break;
              case 6:
                  System.out.println("D");
                  break;
              default:
                  System.out.println("E");
          }
  }
}

計算匯款匯費

匯款時,匯款匯費按如下規則計算:
如果匯款金額小於100元,匯費爲一元;
如果金額在100元與5000元之間,按1%收取匯費;
如果金額大於5000元,匯費爲50元。匯款金額由預設輸入處輸入。
請用程序實現
輸入匯款金額 money,根據金額計算匯費,並將結果輸出。
示例
輸入
5000
輸出
50.0

import java.util.Scanner;

public class Program {
  public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      double money = sc.nextDouble();
      if(money<100){
              System.out.println("1.0");
      }
      else if(money>=100&&money<=5000){
              double n;
              n=money * 0.01;
              System.out.printf("%.1f",n);
      }
      else{
              System.out.println("50.0");
      }
  }
}

填充switch語句

switch case

判斷名次

請用程序實現
輸入1-3的數字 num,根據所輸入的數字,判斷對應名次,並將信息輸出,具體如下:
1:冠軍
2:亞軍
3:第三名
其他數字:超出範圍
示例:
輸入
1
輸出
冠軍

import java.util.Scanner;

public class Program
  {
    public static void main(String args[])
     {
        Scanner sc = new Scanner(System.in);
             int a = sc.nextInt();
         if(a==1){
                 System.out.println("冠軍");
         }
         else if(a==2){
                 System.out.println("亞軍");
         }
         else if(a==3){
                 System.out.println("第三名");
         }
         else{
                System.out.println("超出範圍");
         }
     }
  }

判斷工作日

請用程序實現
輸入 1-7 的數字,根據所輸入的數字,判斷是否爲工作日。
示例 1
輸入
1
輸出
工作日
示例 2
輸入
6
輸出
週六
示例 3
輸入
7
輸出
週日

import java.util.Scanner;

public class Program {
  public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      int x = scan.nextInt();
      if(x>=1&&x<=5){
              System.out.println("工作日");
      }
      else if(x==6){
              System.out.println("週六");
      }
      else if(x==7){
              System.out.println("週日");
      }
  }
}

賣西瓜

瓜販子賣西瓜,第一天賣出所有西瓜的一半還多兩個;以後每天賣出的是前一天的一半還多兩個。
請用程序實現
輸出西瓜的總數,計算多少天后,將西瓜賣完,並將結果輸出。
示例輸入
1020
示例輸出
8

import java.util.Scanner;
public class Program {
    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
        int sum=sc.nextInt();
        int i=0,sum1=0;
        while(sum>=0){
               sum=sum/2-2;
            sum1++;
            if(sum<=0){
                    System.out.println(sum1);
                break;
            }
        }
    }
}

猴子喫桃問題

猴子第一天摘下 N 個桃子,當即吃了一半,感覺還不過癮,又多吃了一個(即此時還剩N1 = N/2 - 1個)。
第二天又將第一天剩下的桃子喫掉一半加一個(即此時還剩N2 = N1/2 - 1個)。
以後,每天都喫掉前一天剩下的桃子的一半加一個,直到第十天,此時桃子只剩下一個了。
請用程序實現
計算第一天猴子總共摘了多少個桃子,並將計算結果輸出。

public class Program {
  public static void main(String[] args) {
      int sum=1;
      for(int i=1;i<10;i++){
              sum=(sum+1)*2;
      }
      System.out.println(sum);
  }
}

求素數和

「質數」又稱素數,有無限個。素數定義爲在大於 1 的自然數中,除了 1 和它本身以外不再有其他因數的數稱爲素數。
例如17就是素數,因爲它不能被2 - 16的任一整數整除。
請編寫一個程序,輸入一個正整數,計算該正整數以內的素數之和,並將計算結果輸出。

import java.util.Scanner;
public class Program{
    public static void main (String[] args) {
          Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        int sum=0;

        int i,j;
        for(i=2;i<=a;i++){
             int ac=1;
            for(j=2;j<i;j++){
                if(i%j==0){
                    ac=0;
                    break;
                }
            }
            if(ac==1){
                sum=sum+i;
            }
        }
        System.out.println(sum);
    }
}

打印三角形

戳這裏戳這裏

老師這次真好 真好 真好

隨手一個贊???

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章