![](/img/trans.png)
[英]Why won't the else statement in my while loop execute when the conditions in the if statement aren't met?
[英]Why won't my array satisfy the conditions of this if-statement?
我正在做這個程序來進行分配,我完全零知道為什么我的數組(從單獨的文件中讀取值)不滿足這個 if 語句的條件。 為方便起見,有問題的語句位於 displayColonies class 中。
當我讀入我的數組時,它會在指定的行和列中查找值,這是一個從 1-9 注冊的 int 值。 當我使用打印語句定期對此進行測試時,數組確實包含正確的值,但 if 語句從未被激活。 條件與數組的值匹配,但從不評估語句,無論是否為true
。
謝謝你的幫助。
下面附上代碼:DetectColonies2ElectricBoogaloo是客戶端Slide是Object 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
中的值)進行比較,其中整數是從 0x30('0')開始的值。 .
因此,無需重寫您的程序,通過將 for 循環值 (1..9) 轉換為等價字符來修復不兼容類型的最簡單解決方案,如下所示:
for (int i = 1; i <= 9; i++)
{
char x = (char) (0x30 + i);
if (slideData[row][col] == x)
{
// ...
}
}
要將 integer (數字)轉換為char
,您還可以執行以下操作:
char x = Character.forDigit(i, 10);
改變
col >= slideData[0].length
至
col >= slideData[row].length
內部方法private int collectCells(int row, int col, char colour)
正如其他人還指出的那樣 - 您正在將 char (字符)與 int (整數)進行比較。 當您看到單元格 [0][0](行 = 0 和 col = 0)處的數組slideData
中的值包含 1, i
中的值也是 1 但條件失敗時,我可能會有點困惑。 但是存儲在characters
數組中的內容與 integer 變量的值之間存在差異。
在 Java 中,當您將character
與integer
進行比較時,JVM 使用字符的 ASCII 值。 這意味着值 1,= '1'。 因為 '1' 的 ASCII 值是 49。
由於 IDE 的調試器如何顯示字符數組的值,您可能看不到差異。 例如,在 IntelliJ 中,您將看到: ![slideData[0][0] value and ASCII value] 1它顯示slideData[0][0]
中的值是 '1'(字符值)及其 ASCII (整數)值為 49。如果您對其進行小幅更改,您的條件將起作用:
if (slideData[row][col] == 48 + i) {
count = collectCells(row , col , (char)i);
System.out.format("%-10s %-10s %-10s" , "(" + row + "," + col + ")" , count , i);
}
integer 值 48 是 '0' 字符的 ASCII 值,將i
的值添加到它會給您正在比較的字符的 ASCII 值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.