java期末考试——自动细胞机

题目描述

mooc链接:

https://www.icourse163.org/learn/ZJU-1001542001?tid=1458169441#/learn/examOj?eid=1216333001&id=1232864406

代码

代码在网上搜过其他同学的代码,但感觉太有点结构化编程的意思了,因此自己写了一个,大概一两个小时,代码应该还能凑合着看

整体

import java.util.Scanner;

public class vendingMachine {
    private int times;
    private int width;
    private int length;

    private int newData[][];
    private int oldData[][];
    Scanner in=new Scanner(System.in);

    void Init(){
        getWidth();
        getLength();

        oldData = new int[length+2][width+2];
        newData = new int[length+2][width+2];

        //读入活的细胞的位置
        while(true)
        {
            int i=in.nextInt();
            int j=in.nextInt();
            if(i==-1&&j==-1)
                break;
            oldData[i+1][j+1]=1;
            newData[i+1][j+1]=1;
        }
        //读入执行步数
        times = in.nextInt()-1;
    }

    void Run(){
        while(times-->=0)
        {
            for(int i=1;i=3&&huwu<=102)
            this.width=huwu;
        else
            System.out.println("The number must be 2~126");
    }

    private void getLength(){
        int qifei=in.nextInt();
        if(qifei>=3&&qifei<=102)
            this.length=qifei;
        else
            System.out.println("The number must be 2~126");
    }

    private int CountAlive(){
        //计算活着的细胞的数量
        int count=0;
        for (int i = 1; i < length+1; i++)
        {
            for (int j = 1; j < width+1; j++)
            {
                if(newData[i][j]==1)
                    count++;
            }
        }
        return count;
    }

    void Test()
    {
        Init();
        Run();
        System.out.println(CountAlive());
    }

}


测试类


public class Main {
    public static void main(String[] args) {
        vendingMachine city = new vendingMachine();
        city.Test();
    }
}

看到这里了 点个赞吧
祝点赞的人都逢考必过,心想事成(没有道德绑架的意思)

你可能感兴趣的:(java翁恺学习笔记,java,深度学习,机器学习)