猿问

为啥一个对一个错

public class HelloWorld {

    public static void main(String[] args) {

int one = 20 ;

        String res;

   if ((one%2) == 0)

            res = "偶数";

        else

            res = "奇数";

        System.out.println("one是"+res);    

}

}为啥这个是对的,

public class HelloWorld {

    public static void main(String[] args) {

int one = 20 ;

        String res;

   if ((one%2) == 0)

            res = "偶数";

        

   

        System.out.println("one是"+res);    

}

}这个就是错的了,跪求!!


NMX
浏览 1998回答 5
5回答

三岁半

因为啊。。你看正确的public class HelloWorld {    public static void main(String[] args) {int one = 20 ;        String res;//申请一个变量   if ((one%2) == 0)            res = "偶数";//如果真,res赋值        else            res = "奇数";//如果假,res赋值        System.out.println("one是"+res);    //这里的res总是有值的}}再看错误的 public class HelloWorld {    public static void main(String[] args) {int one = 20 ;        String res;//申请res   if ((one%2) == 0)            res = "偶数";//如果真,res赋值,那么如果假呢?res就没有值了                   System.out.println("one是"+res);    //此处的res可能没有赋值}}

Bangk

你的报错信息呢?public class HelloWorld {     public static void main(String[] args) {         int one = 20 ;         String res;         if ((one%2) == 0){             res = "偶数";         }         System.out.println("one是"+res);     }我这样写运行成功

慕妹5461943

MIPPDHUNHREMZBHBGTQQGOBEGSWMPWWTJIYVSCBBIDBBLVCGAHWKXLPXXOVLXQTFMZFFZMGFCCJZKXMIVGWCQURRUVHXXVLLIXNRZPFVBLVIZMTDERQTNGXTNWBRYRKQNXATFBYPMUGNDXDTMUKZZQDTZNDKPSOYZNZJPPSCDJCVLIPCFKHGRFYRORRQMNWCDSLRHUEUUNKNINRUUBHAEUFLVCMYNTMJSJPOMTJGRQXDDNMTKRKUNADJLZOOPLRLRHKDGHTOMIXNKDNQMTORTOBHUGATPORVIUXXNXEVCZWFCXULBADJQJXDORUGNJJWMNGSFIRXXCIOSYPOYAILLCFVPJWITSORYRXRYIEOQXKKVLYUOPVUJMIHYUHDQIWTAJWIZGCZLWCMHQJVLCHBOGIBKKQIWFFIOYSHGCVBJGCFVIZSKDANNUFBIFSRQTTKNUNUQHXNEUQZOXKDQUTJRKZCKYEUKKHDEAZTZWNDTPQFSEIXVSPEYBRABIBFVGDWPTKTEMQNUHETSWJCKIIBHONQARXKSBBAFURAKUJMFNHEEMIDZGZCILXXDLWICIOREXDOWCCYFTGQCDWAQKXAKNNNIAAJMSNAXLYEKCMWIZLWSWCWFYUUNWMFCMFZXRQKRRUNAXQNHDNCTSISIAUJHKRHHVFMFFVGWFYEURGAMVIHOACXXQQZCSYBQNZFUHOKQDMRMYVKUGTWUNHPBKRNENYOVBXHTDKDDPMIUNTTCOOFWCFVLUFIZPVERLAUNGMSEQMYQZMLBSYUXPWCLYKUQTDQUDUJMJRHWVSOGUDGOFUSUTJOQSBLPZVZHBCSXHXMVYVYNQGVMBQRVSPSUKHDVSVSCFOSUKNDEHKNGCJTQXJGSMSBURKDNXTLFEUYSYVZYYVYUDQWWMXQPVICCGSZSOOOPNCLFRNTZTCMCZFZVAMNJCPWJTARQFORARUWNXTP

Super_Scholar

res=null;

珍惜选择

给res初始化赋值null

NMX

记事本,运行结果
随时随地看视频慕课网APP

相关分类

Java
我要回答