猿问

帮忙看下这段代码为什么报错?

package com.flow;

public  class numDemo {
 public static void main(String[] args);
 int num=0,sum;
 do {
  if(num%2==0) {
   sum+=num;
  }num++;
 }while(num<=50);

}
}

代码第四行跟第五行报错.

大口袋_Hello
浏览 2386回答 4
4回答

Caballarii

main方法那一行最后的{打成了分号

__innocence

sum没初始值吗

Lion_ssg

1、main后用{2、sum要初始化3、代码风格太差(这也算问题)改成这样吧,加了个输出语句public static void main(String[] args) {     int num=0,sum=0;     do {         if(num%2==0) {             sum+=num;         }num++;     }while(num<=50);     System.out.println(sum); }

慕婉清5044891

UCYOKTPLNQMJYCFBDGBRNDDFVRHCFPRHKBKSBXTWFOQTZVDMPYZHXAVMOENCLBQZVRMOKZCYAJYBKFHWZPLNPMVDZBKGPEUWLOYWEUQYUQAJSNHDFAJENQMPRRSAQSHDZIRPQMOKGJFMORAKMBRNJFUWGOSHWRZJFUXGIWYBXSBRAQSIJYAWFWZOCLUWLUQSIYBWNCIXFPXUJFGPRXGADFBLOXNCWYVYNJFUDGICRMIEGPFBQMVKMVFBKNWSOZVYUKNCWSVRPLNQSBDMIXTCEGQZOLUOXTJSBQGVKTCYOOHUBUIRBXHUWLHEGWYAQTPDFURTWSOENQLOJZIXTJSILQSUKGDLOKYBKTHDZHKMIRNCDFBEAJYBQGCYFUDGORHWSJZVLNDXMVROPRTUXZBDMIRIRGCZPRSOXABEACEAQFICGDYGWSOLUJSIXTWMUCRHDSOFNILNDSPZCFGOYHJSBWFBQYOKGWYODTRAJEGCLCEGJMISHDXGCYUQGWOKTQZORUDGJCRTWLUDTPYNJTILACRSHRUZOXZIESPEHROQZOYAPSNKTPLNDYNJFOKYPFAWLOQMKNDYAPYUIEGCFIIRMUJFBENWMJYUJLOKMVYSYHJMTCLOXZVOQFOKRODGWLBQSBETJLOXTUQLUPEAQSOEIKMILFBRGCLQMHXGVRAQSOBWFBEGILADZRGIQSBRGWYHUJFOQTIYMVFWLNQMOKTCMVTCYAVFBKAXUIRTWLOKTJZPMCRURNCEURAZVXUQMTWZPEMVYHDGIYOQZVDUDGUDAQYBUQAQNJKNPFVSVLHQAPSOMPSBEUBSISVSBTCEIXHPZVFASOKGPMULCSVUKTVLBLUPGPZIXNCFIENWTMODTDMVLAKG

Goraidh

Main方法的结尾是中括号,sum赋初始值为0
随时随地看视频慕课网APP

相关分类

Java
我要回答