public class HelloWorld {
public static void main(String[] args) {
int one = 20 ;
int a=one%2;
System.out.println(a);
if(a==0){
String b="是偶数";
}
System.out.println("one"+b);
}
}
这段代码中IF后面的括号里内容为何出错?
public class Test11
{
public static void main(String[] args)
{
int one = 25 ;
int a=one%2;
String b;
if(a==0){
b="是偶数";
}else{
b="是奇数";
}System.out.println("one"+b);
}
}
public class HelloWorld { public static void main(String[] args) { int one = 20 ; int a=one%2; Strint b System.out.println(a); if(a==0){ b ="是偶数"; } System.out.println("one"+b); } }
作用域的问题吧,你原来的
if(a==0){ String b="是偶数"; }
这里的b只在if条件里面有用,出了if过后b就没有了。所以要在外面定义。
public class HelloWorld {
public static void main(String[] args) {
int one = 20 ;
int a=one%2;
if (a==0){
System.out.println("one是偶数");
}
}
}
没发现问题
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
int one;
Scanner input=new Scanner(System.in);
System.out.println("请输入一个数:");
one=input.nextInt();
if(one%2==0){
System.out.println(one+"是偶数");
}else{
System.out.println(one+"是奇数");
}
}
}
public class HelloWorld {
public static void main(String[] args) {
int one = 20 ;
if(one%2==0){
System.out.println("one是偶数");
}else{
System.out.println("one不是偶数");
}
}
}
int a=one%2;改为
boolean a;
a=(one%2==0)?true:false;
if(a){ }else{ }