从不同的 class 调用静态方法时如何更改对象的值?
How would I change an object's value when calling a static method from a different class?
本练习题实例化了一个由0和1填充的方阵。您可以在任意大小的矩形中翻转值(例如:0 变为 1,1 变为 0),只要矩形的最上角在矩阵中为 [0, 0]。最终目标是找出必须将值翻转多少次才能使矩阵的所有值都为 0。
如果您需要更长的解释,请转至 http://usaco.org/index.php?page=viewproblem2&cpid=689,但这是基本大纲。
这是我的代码:
import java.io.*;
import java.util.*;
public class CowTip {
static int[][] mat;
public static void main( String[] args) throws IOException, InterruptedException{
Scanner scan = new Scanner(new File("cowtip.in"));
int n = scan.nextInt();
scan.nextLine();
mat = new int[n][n];
for (int x = 0; x < n; x++) {
String str = scan.nextLine();
for (int y = 0; y < n; y++) {
mat[x][y] = Integer.parseInt(str.substring(y,y+1));
}
}
Checker c = new Checker(n-1, n-1);
int count = 0;
while (true) {
c.check();
for (int x = 0; x <= c.row; x++) {
for (int y = 0; y <= c.col; y++) {
if (mat[x][y] == 0) {
mat[x][y] = 1;
}
else if (mat[x][y] == 1) {
mat[x][y] = 0;
}
}
}
count++;
c.check();
if (c.row == -1 && c.col == -1) {
break;
}
}
System.out.println(count);
}
static class Checker {
int row;
int col;
public Checker(int r, int c) {
row = r;
col = c;
}
public Checker check() {
Checker check = new Checker(-1, -1);
for (int x = mat.length-1; x >= 0; x--) {
for (int y = mat[x].length-1; y >= 0; y--) {
if (mat[x][y] == 1) {
check = new Checker(x, y);
break;
}
}
if (check.row != -1 && check.col != -1) {
break;
}
}
return check;
}
}
}
这是输入文件(名为 cowtip.in):
3
001
111
111
我已经排除了我当前的调试代码,但问题是我的 check()
方法中的 row
和 col
值是正确的值,但是每当我调用check()
方法在我的 main
中,值恢复为默认值并且没有给我正确的答案,这反过来使循环无限。
关于如何解决这个问题有什么想法吗?
编辑:我已经弄明白了,但谢谢大家!它实际上非常简单(c = c.ckeck()
而不是 c.check()
),老实说,考虑到我花了大约两个小时来调试它,我感到非常沮丧......
将c.check()
替换为c = c.check();
本练习题实例化了一个由0和1填充的方阵。您可以在任意大小的矩形中翻转值(例如:0 变为 1,1 变为 0),只要矩形的最上角在矩阵中为 [0, 0]。最终目标是找出必须将值翻转多少次才能使矩阵的所有值都为 0。
如果您需要更长的解释,请转至 http://usaco.org/index.php?page=viewproblem2&cpid=689,但这是基本大纲。
这是我的代码:
import java.io.*;
import java.util.*;
public class CowTip {
static int[][] mat;
public static void main( String[] args) throws IOException, InterruptedException{
Scanner scan = new Scanner(new File("cowtip.in"));
int n = scan.nextInt();
scan.nextLine();
mat = new int[n][n];
for (int x = 0; x < n; x++) {
String str = scan.nextLine();
for (int y = 0; y < n; y++) {
mat[x][y] = Integer.parseInt(str.substring(y,y+1));
}
}
Checker c = new Checker(n-1, n-1);
int count = 0;
while (true) {
c.check();
for (int x = 0; x <= c.row; x++) {
for (int y = 0; y <= c.col; y++) {
if (mat[x][y] == 0) {
mat[x][y] = 1;
}
else if (mat[x][y] == 1) {
mat[x][y] = 0;
}
}
}
count++;
c.check();
if (c.row == -1 && c.col == -1) {
break;
}
}
System.out.println(count);
}
static class Checker {
int row;
int col;
public Checker(int r, int c) {
row = r;
col = c;
}
public Checker check() {
Checker check = new Checker(-1, -1);
for (int x = mat.length-1; x >= 0; x--) {
for (int y = mat[x].length-1; y >= 0; y--) {
if (mat[x][y] == 1) {
check = new Checker(x, y);
break;
}
}
if (check.row != -1 && check.col != -1) {
break;
}
}
return check;
}
}
}
这是输入文件(名为 cowtip.in):
3
001
111
111
我已经排除了我当前的调试代码,但问题是我的 check()
方法中的 row
和 col
值是正确的值,但是每当我调用check()
方法在我的 main
中,值恢复为默认值并且没有给我正确的答案,这反过来使循环无限。
关于如何解决这个问题有什么想法吗?
编辑:我已经弄明白了,但谢谢大家!它实际上非常简单(c = c.ckeck()
而不是 c.check()
),老实说,考虑到我花了大约两个小时来调试它,我感到非常沮丧......
将c.check()
替换为c = c.check();