2016-05-13 61 views
-2

這是我正在研究的一個簡單的類練習。 (我對編程非常陌生,所以如果這是一個簡單的'菜鳥'錯誤,我很抱歉浪費你的時間。)我不會說謊:我發現很難知道,其中插入某些片斷編程時的代碼。Java程序接受2個數字之間的輸入

import java.util.*; 


public class SuperSaveRandallTWyngaardC { 

    static Scanner console=new Scanner(System.in); 

    public static void main(String[] args) { 

     char newCust; 
     char promo; 
     int itemNr=0; 
     int qty=0; 
     int price=0; 
     int totalPrice=0; 
     int custTot=0; 
     int noOfItems=0; 
     int grandTot=0; 
     int custCount=0; 

     System.out.println(" "); 
     System.out.println("*******SuperSave - your friendly local store.....*******"); 
     System.out.println(" "); 
     System.out.print("New customer? (Y/N)>> "); 
     newCust=console.next().charAt(0); 
     newCust=Character.toUpperCase(newCust); 

     while((newCust!='Y')&&(newCust!='N')) 
    { 
     System.out.print("Invalid option, please re-enter (Y/N)>> "); 
     newCust=console.next().charAt(0); 
     newCust=Character.toUpperCase(newCust); 
    } 
    if (newCust == 'N') 
    { 
    System.out.println("*******NO SALES THE WHOLE DAY.....*******"); 
    } 
    else if (newCust == 'Y') 
    { 
    System.out.print("Please enter the item number (1000 -> 5000 or zero for none)>> "); 
    itemNr=console.nextInt(); 

     while ((itemNr<1000)&&(itemNr>5000)||(itemNr!=0)) 
     { 
     System.out.print("Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> "); 
     itemNr=console.nextInt(); 
     } 
     if (itemNr==0) 
     { 
     System.out.println("*******NO ITEMS WERE BOUGHT BY THIS CUSTOMER.....*******"); 
     } 
     else if ((itemNr>1000)&&(itemNr<5000)) 
     { 
      System.out.print("Enter quantity>> "); 
      qty=console.nextInt(); 
     } 
    } 

    }  

} 

運行該程序。示例輸出...

*******SuperSave - your friendly local store.....******* 

New customer? (Y/N)>> y 
Please enter the item number (1000 -> 5000 or zero for none)>> 1000 
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 5000 
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 999 
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 5001 
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 1234 
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 4000 
Invalid item number, please re-enter (1000 -> 5000 or zero to stop)>> 0 
*******NO ITEMS WERE BOUGHT BY THIS CUSTOMER.....******* 

while環說,任何項目數量的輸入是無效的(即使1000-5000規定的範圍內)

+0

你想做什麼? – Areca

+1

請澄清您的具體問題或添加其他詳細信息,以突出顯示您的需要。正如目前所寫,很難確切地說出你在問什麼。請參閱[如何提問](http://stackoverflow.com/help/how-to-ask)頁面以獲得澄清此問題的幫助。 – Raju

回答

1
(itemNr<1000)&&(itemNr>5000)||(itemNr!=0) 

沒有itemNr,使得它真正。 請考慮-1,1,1001,5001

((itemNr<1000)&&(itemNr!=0))||(itemNr>5000) 

你應該試試這個。

+0

謝謝,我試過了,但它仍然做了與「非法開始表達和預期」相同的事情 – N3wb

+0

也許有一些額外的花括號需要刪除。檢查你的語法。 –

0

(itemNr < 1000) && (itemNr > 5000)產品編號必須小於1000 AND同時大於5000,這是不可能的。

您正在使用(itemNr > 1000) && (itemNr < 5000)稍後進行類似檢查。

做一個檢查功能isInRange(int)是有用的。所以你可以使用!isInRange(itemNr)(不在範圍內)進行第一次檢查。

第二次檢查是不需要的,因爲號碼將始終在while循環後的範圍(或0)。

private static boolean isInRange(int i) { 
    return ((i > 1000) && (i < 5000)); 
} 

... 

while(!isInRange(itemNr) && itemNr != 0) { 
    ... 
} 

if (itemNr == 0) { 
    ... 
} else { // no need to check here 
    ... 
} 
+0

'while(!isInRange(itemNr)|| itemNr!= 0)'仍然錯。對於任何數字,Thjs都是正確的。 – FredK

0

爲了校正有效輸入,1000 ... 5000和0的檢查,更新
while ((itemNr<1000)&&(itemNr>5000)||(itemNr!=0))

while ((itemNr < 1000 && itemNr != 0) || itemNr > 5000)

如果1000和5000是有效的輸入,然後在獲得數量時你必須包含它們。
else if ((itemNr>1000)&&(itemNr<5000))

else if ((itemNr >= 1000) && (itemNr <= 5000))

所以最後一個塊的樣子

while ((itemNr < 1000 && itemNr != 0) || itemNr > 5000) { 
    System.out.print("Invalid item nyumber, please re-enter (1000 -> 5000 or zero to stop)>> "); 
    itemNr = console.nextInt(); 
} 
if (itemNr == 0) { 
    System.out.println("*******NO ITEMS WERE BOUGHT BY THIS CUSTOMER.....*******"); 
} else if ((itemNr >= 1000) && (itemNr <= 5000)) { 
    System.out.print("Enter quantity>> "); 
    qty = console.nextInt(); 
} 
2

你的循環狀態是關閉的。

由於底片似乎混淆,先寫什麼是好的:

(itemNr >= 1000 && itemNr <= 5000) || itemNr == 0 

即必須介於1000和5000(含)之間,必須爲0。

由於大多數人無法得到的&&優先VS ||沒錯,你應該總是使用括號混合時,它們明確指定優先級,因爲我只是做了。

這使得倒車表達容易,因爲你只是扭轉一切,獨自離開了括號:

(itemNr < 1000 || itemNr > 5000) && itemNr != 0  // correct #1 

與此相比,你有什麼,你會看到問題:

(itemNr < 1000) && (itemNr > 5000) || (itemNr != 0) // wrong 

由於> 5000意味着它肯定是!= 0,你可以重新安排這樣的表情,其他人已經表明:

(itemNr < 1000 && itemNr != 0) || itemNr > 5000  // correct #2 

從技術上講,#2表現優於#1,但這是一個差異,你不會有史以來通知。就我個人而言,我發現#2比#1更不直觀,但這是一個意見問題。他們都讓你想要你想要的。

相關問題