java 编程思想课后题(第三章)

本文提供了几个Java编程练习案例,包括计算长度与次数的比例、定义并使用Dog类实例等,旨在帮助初学者理解类与对象的概念及应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

练习4:

public class twoSum {

    public static void main(String[] args){
        class PrintV{
            int times = 233;
            int length = 4211;

            public double returnV()
            {
                return (double)length/times;
            }
        }
        PrintV a = new PrintV();
        System.out.println(a.returnV());
    }
}
练习5

public class twoSum {

    public static void main(String[] args){
        class Dog{
          String name;
          String says;
          public Dog(){

          }
          public Dog(String name,String says){
              this.name = name;
              this.says = says;
          }
          public void PrintDog(){
              System.out.println("This dog's name is "+name +"and it says "+ says +"!");
          }
        }
        Dog spot = new Dog("sopt","Ruff");
        Dog scruffy = new Dog("scruffy","Wruf");
        spot.PrintDog();
        scruffy.PrintDog();
    }
}
练习6:

public class twoSum {

    public static void main(String[] args){
        class Dog{
          String name;
          String says;
          public Dog(){

          }
          public Dog(String name,String says){
              this.name = name;
              this.says = says;
          }
          public void PrintDog(){
              System.out.println("This dog's name is "+name +"and it says "+ says +"!");
          }
        }
        Dog spot = new Dog("sopt","Ruff");
        Dog scruffy = new Dog("scruffy","Ruff");
        spot.PrintDog();
        scruffy.PrintDog();
        System.out.println(spot.says.equals(scruffy.says));
    }
}
练习7:

public class twoSum {

    public static void main(String[] args){
        Random num = new Random(1);
        int m1 = num.nextInt(6);
        int m2 = num.nextInt(6);
        System.out.println(m1+","+m2);
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值