由於某種原因,這是行不通的,我不明白爲什麼。即使它是假的,它也會返回true,除此之外,其他一切似乎都能正常工作,除非它不會返回false。請幫忙!我是Java編程的新手,非常感謝有人抓住我的錯誤!Java中的快樂或不快樂的數字,無法正常工作
import javax.swing.JOptionPane;
public class A2Q1 {
public static void main(String[] args) {
int number, input1, min, max;
String input;
boolean trueFalse;
//constants
min = 0;
max = 1000000;
input = JOptionPane.showInputDialog(null, "Input number -1 to quit");
input1 = Integer.parseInt(input);
while (input1 != -1) {
number = getNumber(min, max); // gets the number the user wants to test
trueFalse = isHappy(number); //tests if the number is happy or unhappy
//getting the returned value and telling the user if it is happy or unhappy
if (false) {
System.out.println(number + " is unhappy");
} else {
System.out.println(number + " is happy");
}
//getting input from the user to continue to quit
input = JOptionPane.showInputDialog(null, input number - 1 to quit");
input1 = Integer.parseInt(input);
}
System.out.println("End of Processing");
}
public static int getNumber(int min, int max) //get the number from the user
{
//declaring variables
String input;
int number;
//getting input from the user for the value they want to test
input = JOptionPane.showInputDialog(null,
"What is the value you
number = Integer.parseInt(input);
if (number > 0 && number < 100000) {
return number;
} else {
System.out.println("You have entered an invalid number");
return number;
}
} //closing getNumber
public static boolean isHappy(int number) {
//declaring variables
int digit1, digit2, digit3, sum;
while (number != 0 && number != 1 && number != 4 && number != 16 && number != 20
&& number != 37 && number != 42 && number != 58 && number != 89 && number != 145) {
digit1 = number % 1000;
digit1 = digit1/100;
digit2 = number % 100;
digit2 = digit2/10;
digit3 = number % 10;
digit1 *= digit1;
digit2 *= digit2;
digit3 *= digit3;
number = digit1 + digit2 + digit3;
}
if (number == 1) {
return true;
} else {
return false;
}
} //closing isHappy
}//closing public class
它如何編譯*? 'input = JOptionPane.showInputDialog(null,input number -1 to quit「);'是無效的語法 – Makoto
你可能想要添加一些junit測試到你的代碼並調試它。考慮數字快樂,當不 –