這是我正在研究的一個簡單的類練習。 (我對編程非常陌生,所以如果這是一個簡單的'菜鳥'錯誤,我很抱歉浪費你的時間。)我不會說謊:我發現很難知道,其中插入某些片斷編程時的代碼。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規定的範圍內)
你想做什麼? – Areca
請澄清您的具體問題或添加其他詳細信息,以突出顯示您的需要。正如目前所寫,很難確切地說出你在問什麼。請參閱[如何提問](http://stackoverflow.com/help/how-to-ask)頁面以獲得澄清此問題的幫助。 – Raju