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);
    }
}

打印三角形

戳这里戳这里

老师这次真好 真好 真好

随手一个赞???

发布了22 篇原创文章 · 获赞 17 · 访问量 1124

猜你喜欢

转载自blog.csdn.net/qq_45803800/article/details/104644603
今日推荐