我正在为一项作业执行此程序,但我完全不知道为什么我的数组(从单独的文件中读取值)无法满足此 if 语句的条件。 为了您的方便,相关语句位于 displayColonies 类中。
当我读取数组时,它会查找指定行和列的值,该值是从 1-9(含 1-9)注册的 int 值。当我使用 print 语句定期测试这一点时,数组确实包含正确的值,但 if 语句从未被激活。条件与数组的值匹配,但永远不会评估该语句,true
或其他。
感谢您的帮助。
代码附在下面: DetectColonies2ElectricBoogaloo 是客户端 幻灯片是对象 Slide.dat是文本文件,排列如下
6
8
10550000
00050000
00005500
01200000
01111000
00000030
public class DetectColonies2ElectricBoogaloo {
public static void main(String [] args) {
Slide culture = new Slide("Slide.dat");
culture.displaySlide();
culture.displayColonies();
}
}
import java.io.*;
public class Slide {
private char NON_COLONY = '0';
private char [][] slideData;
/**
* constructor
* pre: Slide file contains valid slide data in the format:
* first line: lenght of slide
* second line: width of slide
* remaining lines: slide data
* post: Slide data has been loaded from slide file.
*/
public Slide(String s) {
try {
File slideFile = new File(s);
FileReader in = new FileReader(slideFile);
BufferedReader readSlide = new BufferedReader(in);
int length = Integer.parseInt(readSlide.readLine());
int width = Integer.parseInt(readSlide.readLine());
slideData = new char[length][width];
for (int row = 0; row < length; row++) {
for (int col = 0; col < width; col++) {
slideData[row][col] = (char)readSlide.read();
}
readSlide.readLine(); //read past end-of-line characters
}
readSlide.close();
in.close();
} catch (FileNotFoundException e) {
System.out.println("File does not exist or could not be found.");
System.err.println("FileNotFoundException: " + e.getMessage());
} catch (IOException e) {
System.out.println("Problem reading file.");
System.err.println("IOException: " + e.getMessage());
}
}
/**
* Determines a colony size
* pre: none
* post: All colony cells adjoining and including cell (Row, Col) have
* been changed to NON_COLONY, and count of these cells is returned.
*/
private int collectCells(int row, int col , char colour) {
if ((row < 0) || (row >= slideData.length) || (col < 0) || (col >= slideData[0].length) || (slideData[row][col] != colour)) {
return(0);
} else {
slideData[row][col] = NON_COLONY;
return(1 +
collectCells(row + 1, col , colour) +
collectCells(row - 1, col , colour) +
collectCells(row, col + 1 , colour) +
collectCells(row, col - 1 , colour) +
collectCells(row - 1 , col - 1 , colour) +
collectCells(row + 1 , col + 1 , colour) +
collectCells(row - 1 , col + 1 , colour) +
collectCells(row + 1 , col - 1 , colour));
}
}
/**
* Analyzes a slide for colonies and displays colony data
* pre: none
* post: Colony data has been displayed.
*/
public void displayColonies() {
int count;
System.out.format("%-10s %-10s %-10s" , "LOCATION" , "SIZE" , "COLOUR");
System.out.println();
for (int row = 0; row < slideData.length; row++) {
for (int col = 0; col < slideData[0].length; col++) {
for (int i = 1; i <= 9; i++) {
if (slideData[row][col] == i) {
count = collectCells(row , col , (char)i);
System.out.format("%-10s %-10s %-10s" , "(" + row + "," + col + ")" , count , i);
}
}
}
}
}
/**
* Displays a slide.
* pre: none
* post: Slide data has been displayed.
*/
public void displaySlide() {
for (int row = 0; row < slideData.length; row++) {
for (int col = 0; col < slideData[0].length; col++) {
System.out.print(slideData[row][col]);
}
System.out.println();
}
}
}
最佳答案
您正在将 int(i
中的值)(例如 3)与字符(如“1”、“2”...)(slideData
中的值)进行比较code>),整数是从 0x30 ('0') 开始的值..
因此,无需重写程序,最简单的解决方案就是通过将 for 循环值 (1..9) 转换为等效字符来修复不兼容的类型,如下所示:
for (int i = 1; i <= 9; i++)
{
char x = (char) (0x30 + i);
if (slideData[row][col] == x)
{
// ...
}
}
要将整数(数字)转换为 char
,您还可以执行以下操作:
char x = Character.forDigit(i, 10);
关于java - 为什么我的数组不满足该 if 语句的条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58980257/