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();
}
}
}