java基础之测试1

在这里插入图片描述

作业

1,键盘录入两个数据,比较两个数据是否相等,定义一个方法完成

public class HomeWork01 {
    public static void main(String[] args) {
        equalsTo(15,15);
    }
    public static void equalsTo(int a,int b){
        if (a==b){
            System.out.println("a等于b");
        }else{
            System.out.println("a不等于b");
        }

    }
}

2,键盘录入三个数据,获取三个数据中的最大值,分别使用两种方式,定义方法完成

public class HomeWork02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();
        compare(a,b,c);
    }
    public static void compare(int a,int b,int c){
        System.out.println((a>b?a:b)>c?(a>b?a:b):c);
    }
}

3,编写程序,键盘录入2个int型变量并赋值。判断两数之和,
如果大于等 于50,打印"hello world!",否则提示,“不满足”

public class HomeWork03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        if (a+b>=50){
            System.out.println("hello world!");
        }else {
            System.out.println("不满足");
        }
    }
}

4.商场根据会员积分打折,
2000分以内打9折,
4000分以内打8折
8000分以内打7.5折,
8000分以上打7折,
使用if-else-if结构,实现手动输入购物金额和积分,计算出应缴金额 使用方法完成!

public class HelloWorld04 {
    public static void main(String[] args) {
      dis();
    }
    public static void dis(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的消费金额:");
        double money = scanner.nextDouble();
        System.out.println("请输入你的积分:");
        int score = scanner.nextInt();

        if (score<2000&&score>0){
            money = money*0.9;
            System.out.println("应付金额:"+money);
        }else if (score<4000){
            money = money*0.8;
            System.out.println("应付金额:"+money);
        }else if (score<8000){
            money = money*0.75;
            System.out.println("应付金额:"+money);
        }else if (score>8000){
            money = money*0.7;
            System.out.println("应付金额:"+money);
        }
    }
}

5,键盘录入一个学生的成绩,使用if语句完成(定义方法完成)成绩为100分时,奖励一辆车;
成绩为(80,90]时,奖励一个iphone;
当成绩为[60,80]时,奖励一个iPad;
其他的什么奖励也没有

public class HomeWork05 {
    public static void main(String[] args) {
        score();
    }

    public static void score(){
        Scanner scanner = new Scanner(System.in);
        double s = scanner.nextDouble();

        if (s>80 && s<90){
            System.out.println("奖励iphone");
        }else if (s>60 && s<=80){
            System.out.println("奖励ipad");
        }else{
            System.out.println("啥都没得!");
        }
    }
}

6,定义一个方法,该方法能够找出两个小数中的较小值并返回。

public class HomeWork06 {
    public static void main(String[] args) {
        System.out.println(minNum(3.5,4.5));
    }
    public static double minNum(double x,double y){
        return x>y?y:x;
    }
}

7,方法使用的步骤是什么?

定义--调用

8,方法定义的语法是什么?

访问修饰符  修饰符   返回值类型  方法名(形参列表){
	方法体;
}

9,方法调用的语法是什么?

方法名(实参列表);
数据类型 变量名 = 方法名(实参列表);
10,形参列表与实参列表的区别是什么?
1.形参出现在函数定义中,在函数体内使用,离开该函数的作用域不能被使用
2.实参出现在主调用函数中,进入被调函数后,实参遍历不能使用。
3.形参和实参进行数据传送,发生调用时,将实参的值传送给形参。
4.形参被调用时,才会分配内存,调用结束,释放内存。
实参和形参在数量,顺序,类型上必须保持一致
5.函数的调用是单向的,只能把实参的值传递给形参。

11,什么是递归?

自己调用自己

12,什么是重载?

在同一类内,方法名相同但是形参的长度,类型,顺序叫做重载

13,使用方法计算两数之和,之差,积,商,并在方法内打印输出

public class HomeWork07 {
    public static void main(String[] args) {
        add(1,2);
        sub(2,6);
        div(6,3);
        mul(2,6);
    }
    public static void add( int a, int b){
        System.out.println("和:"+a+b);
    }
    public static void sub( int a, int b){
        System.out.println("差:"+(a-b));
    }
    public static void div( int a, int b){
        System.out.println("商:"+(a/b));
    }
    public static void mul( int a, int b){
        System.out.println("商:"+(a*b));
    }
}

14,使用方法计算两数之和,之差,积,商,并在将计算结果返回

public class HomeWork07 {
    public static void main(String[] args) {
        System.out.println(add(1,2));
        System.out.println(sub(2,6));
        System.out.println( div(6,3));
        System.out.println(mul(2,6));

    }
    public static int add( int a, int b){
      return a+b;
    }
    public static int sub( int a, int b){
        return a-b;
    }
    public static int div( int a, int b){
        return a/b;
    }
    public static int mul( int a, int b){
        return a*b;
    }
}

15,有返回值的方法在定义时要注意什么?

1.返回值类型不为void
2.只能返回一个

16,输出100以内的所有素数及所有含9的数字

public class HomeWork08 {
    public static void main(String[] args) {
        int j;
        //外层for遍历100以内的所有数
        for (int i = 2; i <100 ; i++) {
            //内层for循环遍历所有小于外出循环的数,如果可以被整除,就不是素数。是素数遍历后i=j
            for (j = 2; j <i ; j++) {
                if (i%j==0){
                    break;
                }
            }
            if (j==i){
//                if (i%10==9||i/100==9){
//                    System.out.println(i);
//                }
                System.out.println(i);
            }

        }
    }
}

17,输入三个整数x,y,z,请把这三个数由小到大输出

public class HelloWork09 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();

        int [] arr= {a,b,c};
        Arrays.sort(arr);
        for (int i = 0; i <arr.length ; i++) {
            System.out.print(arr[i]+"\t");
        }

    }
}

表达式2%5的值是(D)。

	A. 0.4

	B. 3

	C. 0

	D. 2		

18,计算10的阶乘,封装为方法

public class HomeWork10 {
    public static void main(String[] args) {
        jiecheng(10);
    }
    public static void jiecheng(int a){
        int sum=1;
        for (int i = 1; i < a; i++) {
            sum *=i;

        }
        System.out.println(sum);
    }
}

19,计算50以内数之和

public class HomeWork11 {
    public static void main(String[] args) {
        int sum=0;
        for (int i = 0; i < 50; i++) {
            sum += i;
        }
        System.out.println(sum);
    }
}

20,计算100以内奇数和

public class HomeWork12 {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 0; i < 100; i++) {
            if (i%2!=0){
                sum +=i;
            }
        }
        System.out.println(sum);
    }
}

21,while与do while的区别

while:先判断载执行
do-while:先执行再判断

22.看程序,写结果,说明运算过程
class SwitchTest {
public static void main(String[] args) {
int x = 2;
int y = 3;
switch(x){
default:
y++;
break;
case 3:
y++;
case 4:
y++;
}
System.out.println(“y=”+y);
System.out.println(“---------------”);

y=4;
	int a = 2;
	int b = 3;
	switch(a){
		default: 
			b++;
		case 3:
			b++;
		case 4:
			b++;
	}
	System.out.println("b="+b);
}
答案:
b=4;

23,
大家都知道,男大当婚,女大当嫁。那么女方家长要嫁女儿,当然要提出一定的条件:高:180cm以上;富:财富1千万以上;帅:是。

如果这三个条件同时满足,则:“我一定要嫁给他!!!”

如果三个条件有为真的情况,则:“嫁吧,比上不足,比下有余。”

如果三个条件都不满足,则:“不嫁!”

提示:
键盘录入身高,财富1千万,就用1000来表示,录入boolean类型的变量flag:true,表示帅,否则,不帅(nextBoolean():为录入boolean类型的功能)

	提示:Scanner sc = new Scanner(System.in) ;
	boolean 变量名  = sc.nextBoolean(); 
public class HomeWork13 {
    public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
        System.out.println("如实回答一下问题,以作为丈母娘考察你的标准");
        System.out.println("输入身高:");
        Double height = scanner.nextDouble();
        System.out.println("财富:");
        int rich = scanner.nextInt();
        System.out.println("帅否");
        boolean face = scanner.nextBoolean();

        if (height >= 180 && rich>1000 && face==true){
            System.out.println("我一定要嫁给他!!!");
        }else if (height >= 180 || rich>1000 || face==true){
            System.out.println("嫁吧,比上不足比下有余");
        }else{
            System.out.println("不嫁");
        }
    }
}

24,某银行推出了整存整取定期储蓄业务,
其存期分为一年、两年、三年、五年,到期凭存单支取本息。存款年利率表如下:

存期 年利率(%)

一年 2.25

两年 2.7

三年 3.25

五年 3.6

请存入一定金额(1000起存),存一定年限(四选一),计算到期后得到的本息总额。

提示:

1). 存入金额和存入年限均由键盘录入

2.)本息计算方式:本金+本金×年利率×年限

3)使用Scanner和if…else…的嵌套操作

public class HomeWork14 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("本银行现推出如下存款业务:\n" +
                " *  存期 年利率(%)\n" +
                " *  一年 2.25\n" +
                " *  两年 2.7\n" +
                " *  三年 3.25\n" +
                " *  五年 3.6");
        System.out.println("请输入存款金额:");
        Double money = scanner.nextDouble();
        System.out.println("请输入存款年限:(1-5)");
        int year = scanner.nextInt();
        //本金+本金×年利率×年限
        if (year==1){
            System.out.println("本息:"+(money+money*0.0225*year));
        }else if (year==2){
            System.out.println("本息:"+(money+money*0.027*year));
        }else if (year==3){
            System.out.println("本息:"+(money+money*0.0325*year));
        }else if (year==5){
            System.out.println("本息:"+(money+money*0.036*year));
        }
    }
}

25.某小伙想定一份外卖,商家的优惠方式如下:
鱼香肉丝单点24元,油炸花生米单点8元,米饭单点3元。
订单满30元8折优惠。鱼香肉丝优惠价16元,但是优惠价和折扣不能同时使用。
那么这个小伙要点这三样东西,最少要花多少钱?

提示:
有两种购买方式,一种是不使用优惠价,另一种是使用优惠价。分别计算花费后,对两种方式的花费作对比

	//使用算术运算符、赋值运算符和三元运算符联合完成本题
	
	public class HomeWork15 {
    public static void main(String[] args) {
        int falsh = 24;
        int peanut = 8;
        int rice = 3;
        double money = (falsh+peanut+rice)*0.8;
        falsh = 16;
        double money1 = (falsh+peanut+rice);

        System.out.println(money>money1?"使用优惠价":"不使用优惠价");
    }
}

附加题
26,百文百鸡

public class HomeWork16 {
    public static void main(String[] args) {
        for (int i = 0; i <33 ; i++) {
            for (int j = 0; j <50 ; j++) {
                int z= 100-i-j;
                if (3*i+2*j+z/3==100&&z%3==0){
                    System.out.println("公鸡"+i+"母鸡"+j+"小鸡"+z);
                }
            }
        }
    }
}

27,鸡兔同笼

//*上有三十五头,下有九十四足*
public class HomeWork17 {
    public static void main(String[] args) {

        for (int i = 0; i <=35 ; i++) {
            if (i+(35-i)==35 && 4*i+ 2*(35-i)==94){
                System.out.println("兔子:"+i+"鸡:"+(35-i));
            }
        }
        

28,某星球存在两种生物,A种生物有2个头2条腿,B种生物有3个头4条腿。来自地球的太空船刚刚在该星球降落,突然发现一大群这两种生物组成的队伍,由于时间紧,只数了头的数量和腿的数量,请帮助宇航员分析A、B两种生物各有多少个。
输入说明:头的数量L 腿的数量Z,(L, Z <=100000);
输出说明:A生物的数量 B生物的数量(两个整数用一个空格隔开);
输入样例:35 40
输出样例:10 5

public class HomeWork18 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int head = scanner.nextInt();
        int legs = scanner.nextInt();
        for (int i = 0; i < 100000 ; i++) {
            for (int j = 0; j <100000 ; j++) {
                if (2*i+3*j==head && 2*i + 4*j ==legs){
                    System.out.println(i+" "+j);
                }
            }



        }
    }
}

29,打印直角三角形

public class HomeWork19 {
    public static void main(String[] args) {
        for (int i = 1; i <5 ; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

30,打印等腰三角形

public class HomeWork20 {
    public static void main(String[] args) {
        for (int i = 0; i <5 ; i++) {
            for (int j = 0; j <2*5 ; j++) {
                if (j>=5-i&&j<=5+i){
                    System.out.print("*");
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

31,今有兽,六首四足;禽,四首二足,上有七十六首,下有四十六足。问:禽、兽各几何

public class HomeWork21 {
    public static void main(String[] args) {
        for (int i = 0; i < 12 ; i++) {
            for (int j = 0; j < 19 ; j++) {
                if (6*i+4*j==76&&4*i+2*j==46){
                    System.out.println(i+" "+j);
                }
            }
        }
    }
}

32,今有大小道士100人,粟饼百个,一位大道士吃四个粟饼,四位小道士吃一个粟饼,问大小道士各多人
.看程序写结果

public class HomeWork22 {
    public static void main(String[] args) {
        for (int i = 0;i<100;i++){
            for (int j=0;j<100;j++){
                if (4*i+j/4==100&&j%4==0&&i+j==100){
                    System.out.println("大道士:"+i+"小道士:"+j);

                }            }

        }    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半糖不加奶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值