public class V0206 {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
int x = sc.nextInt();
int y = 400;
int z = 100;
int q = 4;
int rest =(int)(x % y);
int rest2 = (int)(x % z);
int rest3 = (int) (x % q);
String result = (rest3 == 0 && rest2 != 0 || rest == 0 && rest2 == 0 ) ? "Leap year" : "Not leap year";);
if (result = true) {System.out.println("Leap year");}
else
{System.out.println("Not leap year");
}
}
}
I need to make a program which allows entering a year, and the program needs to tell if that year (which we enter in on a console) is a leap year or not.
Year which is dividible by 4 and NOT dividible by 100 IS leap year. Year which is dividible by 400 and also dividible by 100 IS leap year.
I NEED to use "if" command for flow-control and a ternary operator.
compiler gives this:
V0206.java:15: error: illegal start of expression
String result = (rest3 == 0 && rest2 != 0 || rest == 0 && rest2 == 0 ) ? "Leap year" : "Not leap year";);
1 error
Compilation failed.
String result = (rest3 == 0 && rest2 != 0 || rest == 0 && rest2 == 0 ) ? "Leap year" : "Not leap year";
Remove the ;)
characters before the last ;
.
Finally your code should look like this:
public class V0206 {
public static void main(String[] args) {
java.util.Scanner sc = new java.util.Scanner(System.in);
int x = sc.nextInt();
int y = 400;
int z = 100;
int q = 4;
int rest = (int) (x % y);
int rest2 = (int) (x % z);
int rest3 = (int) (x % q);
String result = (rest3 == 0 && rest2 != 0 || rest == 0 && rest2 == 0) ? "Leap year" : "Not leap year";
// This if statement can be replaced by System.out.println(result);
if (result.equals("Leap year")) {
System.out.println("Leap year");
} else {
System.out.println("Not leap year");
}
}
}
result
is a String and it can't be compared to a boolean value ( true
). Use equals
method instead. Note that equals
is case sensitive. Its couterpart is equalsIgnoreCase
.
There is an extra ;)
in your code
String result = (rest3 == 0 && rest2 != 0 || rest == 0 && rest2 == 0 ) ? "Leap year" : "Not leap year";
I am not sure if your logic is correct. You should be doing something like this :-
if (result.equals("Leap year")) System.out.println("Leap year"); else System.out.println("Not leap year");
Should do the work
Two problems:
1) Drop the extra semicolon to give
String result = (rest3 == 0 && rest2 != 0 || rest == 0 && rest2 == 0 ) ? "Leap year" : "Not leap year");
2) Refactor this line if (result = true)
. This is an attempt to assign true
to result
(which will not compile and you probably also meant ==
). But you can't compare equality with strings like this: you should use equals()
or contentEquals()
instead.
I suspect all you want to do is System.out.println(string);
Three issues
extra ;)
in the ternary operation
comparing String with boolean with ==
operator
Value of result will be either Leap year
or Not leap year
, not "true"
or true
String result = (rest3 == 0 && rest2 != 0 || rest == 0 && rest2 == 0 ) ? "Leap year" : "Not leap year"; if (result.equals("Leap year")) { System.out.println("Leap year"); } else { System.out.println("Not leap year"); }
Alternatively, you can do bit fine tuning like this,
boolean result = (rest3 == 0 && rest2 != 0 || rest == 0 && rest2 == 0 ) ? true: false;
if(result){
System.out.println("Leap year");
}
else
{
System.out.println("Not leap year");
}
Hlo. Buddy first check the basic error. Try below code.
public class V0206 {
public static void main(String[] args) {
int x = sc.nextInt();
int y = 400;
int z = 100;
int q = 4;
int rest = (int) (x % y);
int rest2 = (int) (x % z);
int rest3 = (int) (x % q);
boolean result = (rest3 == 0 && rest2 != 0 || rest == 0 && rest2 == 0) ? true
: false;
if (result == true) {
System.out.println("Leap year");
} else {
System.out.println("Not leap year");
}
} }
// one mistake like.. you have added extra semicolon. and checking the string variable like boolean variable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.