0
ユーザーが特定の入力を入力した場合、ループから脱出する必要があります。しかし、私はそれを行うことができませんwhileループを抜け出すためにループを使用しています。 whileループでも同じ条件を試してみましたが、どちらもうまくいきません。whileループから抜け出せません。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String quit;
Scanner c = new Scanner(System.in);
while (true) {
leapOrNot y = new leapOrNot();
System.out.println("press x to stop or any other letter to continue");
quit = c.next();
if (quit == "x" || quit == "X") {
break;
}
}
}
}
class leapOrNot {
final String isLeap = " is a leap year.";
final String notLeap = " is not a leap year.";
int year;
public leapOrNot() {
Scanner a = new Scanner(System.in);
System.out.println("Enter a year after 1581: ");
year = a.nextInt();
/* if (a.hasNextInt() == false) {
System.out.println("Enter a 4 digit integer: ");
year = a.nextInt();
}
couldn't make this condition work either
*/
while (year < 1582) {
System.out.println("The year must be after 1581. Enter a year after 1581: ");
year = a.nextInt();
continue;
}
if (year % 4 == 0) {
if(year % 400 == 0 && year % 100 == 0) {
System.out.println(year + isLeap);
}
if (year % 100 == 0) {
System.out.println(year + notLeap);
}
else {
System.out.println(year + isLeap);
}
}
else {
System.out.println(year + notLeap);
}
}
}
ヒントを使用します。! 'c.next()toLowerCaseメソッドは、()' '=='や '='使用して文字列を比較しないでください、あなたの平等 –
を減らすのに役立ちます。代わりに 'equals(...)'または 'equalsIgnoreCase(...)'メソッドを使用してください。 2つの*オブジェクト参照が同じであるかどうかを '=='が調べることを理解する。興味のあるものではない。もう一方のメソッドは、2つの文字列が同じ順序で同じ文字を持つかどうかをチェックする。ここに問題があります。 –