2012-11-18 73 views
0

所以香港專業教育學院加粗了錯誤的發生,但我不明白爲什麼有發生它沒有任何意義,我變量錯誤,無法轉換爲布爾錯誤

for(int i = 1; i < Array.getLength(purchases);i++) 
    { 
     System.out.print("\n"); 
     System.out.print("Enter a price for item #"+i+": $"); 
     double temp3=input.nextDouble(); 
     double price=temp3; 

      if(price=>0) **<==it wont let me have both = and >** 
      { 
       total=total+price; 
       double temp1=total*100; 
       int temp2=(int)temp1; 
       total=temp2/100.0; 
     System.out.print("That was $"+price+". Your total is $"+total); 
      } 
     else(price==0) **<=="The left-hand side of an assignment must be a variable"** 
      { 

      } 
    } 
+0

你得到什麼錯誤? –

+0

你不能在代碼格式中使用粗體,現在代碼被格式化爲 –

回答

2
if(price=>0) 

這應該是: -

if(price >= 0) 

請注意訂單>=。首先是>

另: - else(price==0)應該只是else,你不需要在你的else中添加一個條件。

+0

謝謝我認爲我應該現在回去睡覺 –

+0

@TylerDManary ..哈哈:)當然,鑑於你正在做這些類型的錯誤。 –

+0

我想知道還有多少次,我會看看,如果我沒有問 –

1

您的大於或等於排序錯誤。

if(price=>0) 

應該

if(price>=0) 

正確的順序是使用>=

else(price==0) 

應該

else if(price<0) //should be less than zero , because you are already checking if price is >=0 in your if. 

或只是否則足夠

嵌套的if-else語法

if(somecond){ 

    } 
    else if(somecond){ 
    } 
    else{ // you don't mention any condition for else, as it would be the last condition. 

    } 
+0

請注意,如果你的條件已經涵蓋在你的if。只要'else'就可以工作。 –

+0

@RohitJain耶普,但它會不會造成任何傷害呢? .. – PermGenError

+0

@GanGnaM ..那麼,在那種情況下,你的'else if'永遠不會被執行。它測試條件'==',你的if已經有一個'> ='的條件,它自己會檢查'=='。 –

0

在java中, 「大於或等於」 是> =。 另外,對於第二個錯誤,您應該有「else if」,而不僅僅是「else」。

相關問題