Java基础编程题

1.某公司每月标准上班时间是160小时,每小时工资是30元。

如果上班时间超出了160小时,超出部分每小时按1.5倍工资发放。请编写程序计算员工月工资。

 1 import java.util.Scanner;
 2 
 3 public class Wag {
 4     static int wag1(int a) {
 5         int s = 0;
 6         if (a > 0 && a <= 160) s = a * 30;
 7         else if (a > 160)
 8             s = a * 30 + (a - 160) * 15;
 9         else return 0;
10         return s;
11     }
12 
13     public static void main(String[] args) {
14         Scanner sc = new Scanner(System.in);
15         System.out.println("程序员工作时间:");
16         int a = sc.nextInt();
17         int sum = wag1(a);
18         System.out.println("程序员的工资:" + sum);
19     }

测试结果

!!不加班怎么来钱(⊙_⊙)!!

2. 已知某年某月,请输出这个月共有多少天。(if语句)

/** 判断2009年是闰年还是平年。

 *提示:

 *闰年的条件是符合下面二者之一:(1)年份能被4整除,但不能被100整除;(2)能被400整除。

 **/

 1 import java.util.Scanner;
 2 
 3 public class Mouths {
 4     static int day(int year, int mouth){
 5         int s=0;
 6         int[] a={31,28,31,30,31,30,31,31,30,31,30,31};
 7         int[] b={31,29,31,30,31,30,31,31,30,31,30,31};
 8         if((year%4==0&&year%100!=0)||(year%400==0)){
 9             return s=b[mouth-1];
10         }
11         else
12             return s=a[mouth-1];
13     };
14     public static void main(String[] args){
15         Scanner sc = new Scanner(System.in);
16         System.out.println("年份:");
17         int year = sc.nextInt();
18         System.out.println("月份:");
19         int mouth = sc.nextInt();
20         int days= day(year,mouth);
21         System.out.println("这个月的天数:" + days);
22     }
23 }

测试结果

3. 根据学生成绩,打印学生考试等级。

[90,100]    

[80,90)     

[60,80)     

[0,60)      

public class Grade {
    static void grade(int a){
        switch(a/10){
            case 10:
                System.out.println("优");break;
            case 9:
                System.out.println("优");break;
            case 8:
                System.out.println("良");break;
            case 7:
                System.out.println("中");break;
            case 6:
                System.out.println("中");break;
            default:
                System.out.println("差");break;
        }
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生成绩:");
        int a = sc.nextInt();
        System.out.println("成绩等级:" );
        grade(a);

}

测试结果

4.计算数字n阶乘 n! = n*n-1*n-2……

  •  递归算法
import java.util.Scanner;

public class Factorial {
/*递归算法*/
static int factorial(int n){ int p; if(n==0 || n==1) p=1; else p=factorial(n-1)*n; return p; } public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("要计算的谁的阶乘:"); int n = sc.nextInt(); int product=factorial(n); System.out.println(n+"的阶乘为:"+product); } }
  • while循环
import java.util.Scanner;

public class Test1 {
    static int factorial(int n){
        int f=1;
       while(n>=1)
       {
           f*=n;
           n--;
       }
       return f;
    }
public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("要计算的谁的阶乘:");
        int n = sc.nextInt();
        int product=factorial(n);
        System.out.println(n+"的阶乘为:"+product);
    }
}

 测试结果

  • 递归

 

  •  while循环

5. 打印九九乘法表

public class Multiplication_table {
    static void multiplication_table(){
        int i,j;
        for (i = 1; i <= 9; i++) {
            for (j = 1; j <= i; j++) {
                System.out.print(i + "*" + j + "=" + i * j + "	");
            }
            System.out.println();

        }
    }
    public static void main(String[] args){
        multiplication_table();
    }
}

测试结果

原文地址:https://www.cnblogs.com/xiaoyezideboke/p/10877321.html