# 作业

1.输入一个年份,判断是不是闰年(能被4整除但不能被100整除,或者能被400整除)

package chap3;

import java.util.Scanner;

public class text1 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("输入年份");
int year = input.nextInt();
if (year % 4 == 0 && year%100!=100 || year % 400 == 0) {
System.out.println("该年份为闰年");
} else {
System.out.println("该年份是平年");
}
}

}

2.输入一个4位会员卡号,如果百位数字是随机数,就输出是幸运会员,否则就输出不是.

package chap3;

import java.util.Random;
import java.util.Scanner;

public class text2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("输入一个四位卡号");
int card = input.nextInt();
Random r = new Random();
int a = r.nextInt(10);
System.out.println("输出随机数" + a);
if (card / 100 % 10 == a) {
System.out.println("该用户为幸运会员");
} else {
System.out.println("该用户不是幸会会员");
}
}

}

3.已知函数,输入x的值,输出对应的y的值.

package chap3;

import java.util.Scanner;

public class text3 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("输入一个值x");
int x = input.nextInt();
int y1 = x + 3;
int y2 = 0;
int y3 = x * 2 - 1;
if (x > 0) {
System.out.println("y的值为" + y1);
} else if (x == 0) {
System.out.println("y的值为" + y2);
} else if (x < 0) {
System.out.println("y的值为" + y3);
}
}

}

4.输入三个数,判断能否构成三角形(任意两边之和大于第三边)

package chap3;

import java.util.Scanner;

public class text4 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入三个数");
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
if (a + b > c && b + c > a && a + c > b) {
System.out.println("可以构成三角形");
} else {
System.out.println("不能构成三角形");
}
}

}

5.输入年份月份，输出该月的天数（闰年229天，条件参考上机练习1

package chap3;

import java.util.Scanner;

public class text5 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("输入年份及其月份");
int year = input.nextInt();
int month = input.nextInt();
if (year % 4 == 0 && year%100!=100 || year % 400 == 0) {
switch(month){
case 2:
System.out.println("该月天数为"+29);
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("该月天数为"+31);
break;
default:
System.out.println("该月天数为"+30);
break;
}
} else {
switch(month){
case 2:
System.out.println("该月天数为"+28);
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("该月天数为"+31);
break;
default:
System.out.println("该月天数为"+30);
break;
}
}
}

}

6、给定一个成绩a，使用switch结构求出a的等级

A90-100B80-89C70-79D60-69E0~59

package chap3;

import java.util.Scanner;

public class text6 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入成绩");
int a = input.nextInt();

switch (a / 10) {
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");
break;
}
}

}

7、输入一个数字，判断是一个奇数还是偶数

package chap3;

import java.util.Scanner;

public class text7 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入一个数");
int a = input.nextInt();
if (a % 2 == 0) {
System.out.println("该数为偶数");
} else {
System.out.println("该数为奇数");
}
}

}

8、编写程序， 判断一个变量x的值，如果是1， 输出x=1，如果是5，输出x=5，如果是 10，输出 x=10，除了以上几个值，都输出x=none

package chap3;

import java.util.Scanner;

public class text8 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入一个数");
int a = input.nextInt();
switch (a) {
case 1:
System.out.println("1");
break;
case 5:
System.out.println("5");
break;
case 10:
System.out.println("10");
break;
default:
System.out.println("none");
break;
}
}

}

9、判断一个数字是否能被56同时整除（打印能被56整除），或只能被5整除（打印能被5整除 ），或只能被6整除，（打印能被6整除），不能被56整除，（打印不能被56整除）

package chap3;

import java.util.Scanner;

public class text9 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("输入一个数");
int a = input.nextInt();
if (a % 5 == 0 && a % 6 == 0) {
System.out.println("这个数可以被5和6整除");
} else if (a % 5 == 0) {
System.out.println("这个数只能被5整除");
} else if (a % 6 == 0) {
System.out.println("这个数只能被6整除");
} else if (a % 5 == 0 || a % 6 == 0) {
System.out.println("这个数不能被5或6整除");
}

}

}

posted @ 2021-03-26 19:26  计算机1901金皓楠  阅读(69)  评论(0编辑  收藏  举报