急急急,語法沒錯,邏輯錯在哪了呢,輸出答案是10位
public class HelloWorld{
public static void main(String[] args){
int num = 999;
int count = 0;
??????? for(;count<=9;count++){
??????????? if(num/(10^count)<1){
??????????? break;?
??????? ?}
??????? }System.out.println("它是個"+count+"位的數(shù)!");
}
}
2016-08-22
if語句中的表達表達式錯誤?。?!10的n次方不能寫成10^n,“^”是位運算符?。?!
正解是Math.pow(10, count)即是if中的表達式是(num /?Math.pow(10, count)?< 1)
2016-09-07
講道理來說,開始的定義變量count的初始值應(yīng)該為1才對吧,哪有什么0位數(shù)。。。。。。 public class HelloWorld{ public static void main(String[] args){ ? ?int num = 999; ? ?int count = 1; ? for(;(num/10)!= 0;count++){ ? ? num=num/10; } System.out.println("它是個"+count+"位的數(shù)!"); } }
2016-08-21
for(;count<=9;count++)??? count等于9的話,count也自增所以是10位,if后面小括號里的扛不動
2016-08-21
這代碼寫的,基本邏輯關(guān)系就錯了。從邏輯關(guān)系上看就是強制count強制循環(huán)10次,你count不等于10么