填写程序,要求通过判断狗的大小,输出不同的叫声

2022/5/1 1:13:13

本文主要是介绍填写程序,要求通过判断狗的大小,输出不同的叫声,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

class Dog{   //利用封装的方法,定义 size 变量,使得 size 变量的值始终大于 10    /*编写 bark方法,size值 > 60的时候输出“Woof!”;size值 > 40的时候输出“Ruff!”; 其他取值时输出“Yip!”。*/ } public class DogTestDrive{ public static void main(String[] args){ //创建至少三个 Dog 对象 //为每个对象设定或输入 size 的大小 //输出每个 Dog 对象的 size 大小 //调用 bark 方法输出不同 size 大小的叫声 }

 

 

import java.util.*;

public class Main{
    public static void main(String[] args) {
        Dog a=new Dog();
        Dog b=new Dog();
        Dog c=new Dog();
        Scanner scanner=new Scanner(System.in);
        a.setSize(scanner.nextInt());
        a.bark();
        b.setSize(scanner.nextInt());
        b.bark();
        c.setSize(scanner.nextInt());
        c.bark();
    }
}
class Dog{
    private int size;
    void bark(){
        if (size>60) System.out.println("Woof!");
        else if (size>40&&size<=60) System.out.println("Ruff!");
        else System.out.println("Yip!");
    }
    void setSize(int size){
        this.size=size;
    }
}

 

 



这篇关于填写程序,要求通过判断狗的大小,输出不同的叫声的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程