Java程序第四次作业
输入一个年份,判断是不是闰年(能被4整除但不能被100整除,或者能被400整除)
package com.homework04;
import java.util.Scanner;
public class Assignment01 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
if ((n % 4 == 0 && n % 100 != 0) || n % 400 == 0) {
System.out.println("闰年");
}
else {
System.out.println("不是");
}
}
}
输入一个4位会员卡号,如果百位数字是随机数,就输出是幸运会员,否则就输出不是.
package com.homework04;
import java.util.Scanner;
import java.util.Random;
public class Assignment02 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int card = s.nextInt();
int target = card / 100 % 10;
Random r = new Random();
int n = r.nextInt(9);
// System.out.println("target: " + target + "\nn: " + n);
if (target == n) {
System.out.println("获奖");
}
else {
System.out.println("未获奖");
}
}
}
已知函数,输入x的值,输出对应的y的值.
x + 3 ( x > 0 )
y = 0 ( x = 0 )
x*2 –1 ( x < 0 )
package com.homework04;
import java.util.Scanner;
public class Assignment03 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int x = s.nextInt();
int y = 0;
if (x > 0) {
y = x + 3;
}
else if (x == 0) {
y = 0;
}
else if (x < 0) {
y = x * 2 - 1;
}
System.out.println(y);
}
}
输入三个数,判断能否构成三角形(任意两边之和大于第三边)
package com.homework04;
import java.util.Scanner;
public class Assignment04 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a = s.nextInt();
int b = s.nextInt();
int c = s.nextInt();
if (a + b > c && a + c > b && b + c > a) {
System.out.println("ok");
}
else {
System.out.println("no");
}
}
}
作业
1.
输入年份月份,输出该月的天数(闰年2月29天,条件参考上机练习1)
package com.homework04;
import java.util.Scanner;
public class Assignment05 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int year = s.nextInt();
int month = s.nextInt();
if (month < 1 || month > 12) {
return;
}
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
if (month == 2) {
System.out.println(29);
return;
}
}
if (month == 2) {
System.out.println(28);
}
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
System.out.println(31);
}
else if (month == 4 || month == 6 || month == 9 || month == 11) {
System.out.println(30);
}
}
}
给定一个成绩a,使用switch结构求出a的等级
。A:90-100,B:80-89,C:70-79,D:60-69,E:0~59
package com.homework04;
import java.util.Scanner;
public class Assignment06 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int grade = s.nextInt();
if (grade < 0) {
return;
}
int n = grade / 10;
switch (n) {
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");
}
}
}
输入一个数字,判断是一个奇数还是偶数
package com.homework04;
import java.util.Scanner;
public class Assignment07 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
if (n % 2 == 0) {
System.out.println("偶数");
}
else {
System.out.println("奇数");
}
}
}
编写程序, 判断一个变量x的值,如果是1, 输出x=1,如果是5,输出x=5,如果是 10,输出 x=10,除了以上几个值,都输出x=none。
package com.homework04;
public class Assignment08 {
public static void main(String[] args) {
int x = 1;
if (x == 1 || x == 5 || x == 10) {
System.out.println("x = " + x);
}
else {
System.out.println("x = none");
}
}
}
判断一个数字是否能被5和6同时整除(打印能被5和6整除),或只能被5整除(打印能被5整除 ),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)
package com.homework04;
public class Assignment09 {
public static void main(String[] args) {
int n = 60;
if (n % 5 == 0) {
if (n % 6 == 0) {
System.out.println("能被5和6整除");
}
else {
System.out.println("能被5整除");
}
}
else {
if (n % 6 == 0) {
System.out.println("能被6整除");
}
else {
System.out.println("不能被5或6整除");
}
}
}
}