关于创建类,对象,方法

import java.util.Scanner;
public class arry {
    public static void main(String[] args) {
// 实例化猫类,创建对象cat1,cat2
        Cat cat1 = new Cat();
        cat1. name = "中华田园猫";
        cat1. age = 6;
        cat1. color = "花色";

        Cat cat2 = new Cat();
        cat2. name = "英短";
        cat2. age = 7;
        cat2. color = "蓝色";

        System.out.println("第一只猫信息:" + cat1. name +","+ cat1. age +","+ cat1. color);
        System.out.println("第一只猫信息:" + cat2. name +","+ cat2. age +","+ cat2. color);
    }
}
//创建一个猫类
class Cat{
            String name; //品种
            int age; //年龄
            String color; //花色

}

方法

import java.util.Scanner;
public class arry {
    public static void main(String[] args) {
// 实例化猫类,创建对象cat1,cat2
        Cat cat2 = new Cat();
        cat2.Speak();
    }
}
//创建一个方法类
class Cat{
//
    public void Speak(){
        System.out.println("这是一只好猫");
    }
}

方法

import java.util.Scanner;
public class arry {
    public static void main(String[] args) {
        Sum sum = new Sum();
        sum.total();
    }
}
//创建一个方法类
class Sum{
//
    public void total(){
        int num = 0;
        for (int i = 0; i <= 100; i++) {
            num = num + i;
        }
        System.out.println(num);
    }
}

键盘输入一个数字,计算1——n的总合

import java.util.Scanner;
public class arry {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Sum sum = new Sum();
        sum.total(n);
    }
}
//创建一个方法类
class Sum{
//
    public void total(int n){
        int num = 0;
        for (int i = 0; i <= n; i++) {
            num = num + i;
        }
        System.out.println(num);
    }
}

调用方法计算两个数的和

import java.util.Scanner;
public class arry {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        Sum sum = new Sum();
        sum.total(n,m);
    }
}
//创建一个方法类
class Sum{
//
    public void total(int n, int m ){
        int num =n + m ;
        System.out.println(num);
    }
}
import java.util.Scanner;

public class arry {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        Array array1 = new Array();
        int[] result = array1.ARRAY(a, b);
        System.out.println("The sum is: " + result[0]);
        System.out.println("The difference is: " + result[1]);
    }
}

class Array {
    public int[] ARRAY(int a, int b) {
        int[] arry = new int[2];
        arry[0] = a + b;
        arry[1] = a - b;
        return arry;
    }
}

跨类调用方法 

import java.util.Scanner;

public class arry {
    public static void main(String[] args) {
        A a = new A();
        a.aa();
    }
}
class A{
    public void aa() {
        B b = new B();
        b.bb();
    }
}
class B{
    public void bb(){
        System.out.println("sayhello");
    }
}

判断数的奇偶性

import java.util.Scanner;

public class arry {
    public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      int number = scanner.nextInt();
      A a = new A();
      a.num(number);
    }
}
class A {
    public void num(int aa ){
        if(aa/2 == 0){
            System.out.println("是偶数");
        }
        else
            {
                System.out.println("是奇数");
        }
    }
}

调用方法输出 n 行  i 列的数

import java.util.Scanner;

public class arry {
    public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      int number1 = scanner.nextInt();
      int number2 = scanner.nextInt();
      String number3 = scanner.next();
        A a = new A();
        a.num(4,5,"*");
    }
}
class A {
    public void num(int aa ,int bb, String cc){
        for (int i = 0; i <= aa; i++) {
            for (int j = 0; j < bb; j++) {
                System.out.print(cc);
            }
            System.out.println();
        }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

早点起床写代码

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

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

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

打赏作者

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

抵扣说明:

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

余额充值