练习_PDF2
1.要求用户输入两个数a/b,如果a能被b整除或a加b大于1000,则输出a,否则输出b
package cn.javasm.ly.assignment.pdf3;
import java.util.Scanner;
//1.要求用户输入两个数a/b,如果a能被b整除或a加b大于1000,则输出a,否则输出b
public class Demo1 {
public static void main(String[] args) {
System.out.println("请输入两个整数");
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
if(a%b==0||a+b>1000){
System.out.println(a);
}else {
System.out.println(b);
}
}
}
2.动态录入3个数,请使用条件结构/三目运算符求3个数的最值
package cn.javasm.ly.assignment.pdf3;
import java.util.Scanner;
//2.动态录入3个数,请使用条件结构/三目运算符求3个数的最值
public class Demo2 {
public static void main(String[] args) {
System.out.println("请输入3个整数");
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
if(a>=b&&a>=c){
System.out.println(a);
}else if(b>=a&&b>=c){
System.out.println(b);
}else if(c>=a&&c>=b){
System.out.println(c);
}
System.out.println((a>b?a:b)>c?(a>b?a:b):c);
}
}
3.输入一个数,判断它是否能被3、5、7整除
package cn.javasm.ly.assignment.pdf3;
import java.util.Scanner;
//3.输入一个数,判断它是否能被3、5、7整除
public class Demo3 {
public static void main(String[] args) {
System.out.println("请输入1个整数");
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
if(a%3==0){
if(a%5==0){
if(a%7==0){
System.out.println(a+"可同时被3、5、7整除");
}else {
System.out.println(a+"可同时被3、5整除");
}
}else if(a%7==0){
System.out.println(a+"可被3、7整除");
}else {
System.out.println(a+"可被3整除");
}
}else if(a%5==0){
if (a%7==0){
System.out.println(a+"可同时被5、7整除");
}else {
System.out.println(a+"可被5整除");
}
}else if (a%7==0){
System.out.println(a+"可被7整除");
}else {
System.out.println(a+"不可被3、5、7中任意数整除");
}
}
}
4.超市为了促销,规定:购物不足50元的按原价付款,超过50不足100的按九折付款,超过100元的,超过的部分按八折付款。编程序完成超市的自动计费工作
package cn.javasm.ly.assignment.pdf3;
import java.util.Scanner;
//4.超市为了促销,规定:购物不足50元的按原价付款,超过50不足100的按九折付款,
// //超过100元的,超过的部分按八折付款。编程序完成超市的自动计费工作
public class Demo4 {
public static void main(String[] args) {
System.out.println("请输入1个数作为购物总金额");
Scanner sc=new Scanner(System.in);
double a=sc.nextDouble();
double sum;
if(a<50){
sum=a;
System.out.println(sum+"元");
}else if(a>=50&&a<100){
sum=50+(a-50)*0.9;
System.out.println(sum+"元");
}else if(a>=100){
sum=50+50*0.9+(a-100)*0.8;
System.out.println(sum+"元");
}else {
System.out.println("出错了");
}
}
}
5.抽奖:会员号的百位数字等于产生的随机数即为幸运会员
①从键盘接收4位数会员号
②生成随机数 int random=(int)(Math.random()*10);
③算出会员号中在百位的数字号码 int baiwei=custNo/100%10;
package cn.javasm.ly.assignment.pdf3;
import java.util.Scanner;
//5.抽奖:会员号的百位数字等于产生的随机数即为幸运会员
//①从键盘接收4位数会员号
//②生成随机数 int random=(int)(Math.random()*10);
//③算出会员号中在百位的数字号码 int baiwei=custNo/100%10;
public class Demo5 {
public static void main(String[] args) {
System.out.println("请输入四位数的会员号");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int bai=num/100%10;
if(bai==(int)(Math.random()*10)){
System.out.println("幸运会员");
}else {
System.out.println("漏");
}
}
}
6.要求用户输入一个0-2之间的整数,0是石头,1是剪刀,2是布,询问是否继续出拳,‘y’重复上述过程
package cn.javasm.ly.assignment.pdf3;
import java.util.Scanner;
//6.要求用户输入一个0-2之间的整数,0是石头,1是剪刀,2是布,询问是否继续出拳,‘y’重复上述过程
public class Demo6 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String b="y";
while (b.equals("y")) {
System.out.println("请输入1个0-2的整数");
int a = sc.nextInt();
switch (a) {
case 0:
System.out.println("你出的是石头");
break;
case 1:
System.out.println("你出的是剪刀");
break;
case 2:
System.out.println("你出的是布");
break;
default:
System.out.println("出错了");
break;
}
System.out.println("是否继续出拳,输入y继续");
b = sc.next();
}
System.out.println("猜拳结束");
}
}
7.求1-100间所有能被3整除的整数
package cn.javasm.ly.assignment.pdf3;
//7.求1-100间所有能被3整除的整数
public class Demo7 {
public static void main(String[] args) {
int sum=0;
for (int i = 1; i <= 100; i++) {
if(i%3==0){
sum=sum+i;
}
}
System.out.println(sum);
}
}
8.打印出所有的水仙花数
package cn.javasm.ly.assignment.pdf3;
//8.打印出所有的水仙花数
public class Demo8 {
public static void main(String[] args) {
int a,b,c;
int s=0;
for (int i = 100; i < 1000; i++) {
a=i%10; // 个
b=i/10%10; //十
c=i/100%10; //百
if(a*a*a+b*b*b+c*c*c==i){
System.out.println(i);
s++;
}
}
System.out.println("一共"+s+"个");
}
}
9.计算用户输入的日期离1900年1月1日相距多少天
package cn.javasm.ly.assignment.pdf3;
import java.util.Scanner;
//9.计算用户输入的日期离1900年1月1日相距多少天
public class Demo9 {
public static void main(String[] args) {
int day=0;
Scanner sc=new Scanner(System.in);
System.out.println("请输入年份");
int y=sc.nextInt();
System.out.println("请输入月份");
int m=sc.nextInt();
System.out.println("请输入日期");
int d=sc.nextInt();
if(y>=1900){
for (int i = 1900; i <y; i++) {
if((i%4==0&&i%100!=0)||i%400==0){
day=day+366;
}else {
day=day+365;
}
}
for (int i = 1; i < m; i++) {
switch (i){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day=day+31;
break;
case 4:
case 6:
case 9:
case 11:
day=day+30;
break;
case 2:
if ((y%4==0&&y%100!=0)||y%400==0){
day=day+29;
}else {
day=day+28;
}
break;
default:
break;
}
}
day=day+d;
System.out.println("相距"+day+"天");
}else {
for (int i = 1899; i >y; i--) {
if((i%4==0&&i%100!=0)||i%400==0){
day=day+366;
}else {
day=day+365;
}
}
for (int i = 12; i > m; i--) {
switch (i){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day=day+31;
break;
case 4:
case 6:
case 9:
case 11:
day=day+30;
break;
case 2:
if ((y%4==0&&y%100!=0)||y%400==0){
day=day+29;
}else {
day=day+28;
}
break;
default:
break;
}
}
switch (m){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day=day+(31-d);
break;
case 4:
case 6:
case 9:
case 11:
day=day=day+(30-d);
break;
case 2:
if ((y%4==0&&y%100!=0)||y%400==0){
day=day+(29-d);
}else {
day=day+(28-d);
}
break;
default:
break;
}
System.out.println("相距"+day+"天");
}
}
}
10.接收三位数,然后打印出所有0到这个数之间带3和3的倍数
package cn.javasm.ly.assignment.pdf3;
import java.util.Scanner;
//10.接收三位数,然后打印出所有0到这个数之间带3和3的倍数
public class Demo10 {
public static void main(String[] args) {
System.out.println("请输入3位数");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
for (int i = 0; i <=num; i++) {
if(i%3==0||i%10==3||i/10%10==3||i/100%10==3){
System.out.println(i);
}
}
}
}