怎么錯了 題目判斷999是一個三位數(shù)
public class HelloWorld{ public static void main(String[] args){ int num = 999; int count = 0; double x=1,y=0; for(x;y<0;x*10){ ? ?y=num/x; ? ?count++; ? ? } System.out.printfln("它是個"+(count-1)+"位的數(shù)!"); } } 怎么錯了啊
public class HelloWorld{ public static void main(String[] args){ int num = 999; int count = 0; double x=1,y=0; for(x;y<0;x*10){ ? ?y=num/x; ? ?count++; ? ? } System.out.printfln("它是個"+(count-1)+"位的數(shù)!"); } } 怎么錯了啊
2016-03-18
舉報
2016-03-18
樓主代碼有誤?
for(x;y<0;x*10 ) ?這個不是語句
for循環(huán)格式:for(初始化表達式; 循環(huán)條件表達式;循環(huán)后的操作表達式){
? 循環(huán)體(執(zhí)行語句);}
?例如:for(int x=0; x<10 ; x++)
判斷一個數(shù)是幾位數(shù),拿這個數(shù)與10相除取整,同時開始計數(shù)(以題為例用count即可),當(dāng)取得的整數(shù)為零時,停止計數(shù)而此時count的值即為這個數(shù)的位數(shù)
本題用whie循環(huán)即可
具體代碼如下
public class HelloWorld?{
public static void main(String[] args)?{
int num =999;
int count = 0;
while( num !=0){
num/=10;
count++;
}?
System.out.println("它是個"+count+"位的數(shù)!");
}
}
2022-03-22
明白了,謝謝老師和ifelse語句中的else類似
2016-03-18
y<0不會出現(xiàn)
這是我寫的
?int num=999;
??????? int count=1;
??????? for(;num!=0;)
??????? {
??????????? num=num/10;
??????????? count++;
???????????
??????? }
??????? System.out.println("他是一個 "+(count-1)+" 位數(shù)");
2016-03-18
public class HelloWorld{
????public static void main (String[] args){
????????int num = 999;
????????if(num/100 !=0 ){
????????????int count = 3;
????????}
????????System.out.println("它是個" + count +"位的數(shù)!");
????}
}
2016-03-18
怎么不是語句啊