2013-10-20 121 views
-4

我正在嘗試執行一個程序,它將三個首字母縮寫A,B,C中的一個(讓我們只說三個家庭縮寫)中的一個,然後提示輸入一個數字(銷售)。該程序需要繼續詢問另外一個三個初始值,然後再輸入另一個值。在用戶鍵入Z時結束時,程序結束,但顯示每個家庭的總銷售額。設置循環

我對如何設置這樣的程序有很多麻煩。

向正確的方向微調將是偉大的。

http://postimg.org/image/9heb6c403/

如果你想看到的問題

是其控制檯基地。 心疼炫耀的代碼,但

 string QUIT = "Z"; 
     string quit = "z"; 
     string purchaseString; 
     string mainString; 
     double salesA = 0; 
     double salesB = 0; 
     double salesC = 0; 
     string andr1 = "a"; 
     string andr2 = "A"; 
     string bow1 = "b"; 
     string bow2 = "B"; 
     string clax1 = "c"; 
     string clax2 = "C"; 
     double purchase; 

     Console.WriteLine("To begin press anykey. To see total enter Z or Z "); 
     mainString = Console.ReadLine(); 
     while ((mainString == andr1 || mainString == andr2) || mainString != QUIT && mainString != quit) 
     { 
      Console.WriteLine("Enter the persons Intials: "); 
      mainString = Console.ReadLine(); 
      if (mainString == andr1 || mainString == andr2) 
      { 
       Console.Write("Enter next purchase amount: "); 
       purchaseString = Console.ReadLine(); 
       purchase = Convert.ToDouble(purchaseString); 
       salesA += purchase; 
      } 
      if (mainString == bow1 || mainString == bow2) 
      { 
       Console.Write("Enter next purchase amount: "); 
       purchaseString = Console.ReadLine(); 
       purchase = Convert.ToDouble(purchaseString); 
       salesB += purchase; 
      } 
      if (mainString == clax1 || mainString == clax2) 
      { 
       Console.Write("Enter next purchase amount: "); 
       purchaseString = Console.ReadLine(); 
       purchase = Convert.ToDouble(purchaseString); 
       salesC += purchase; 
      } 
     } 
     Console.WriteLine("Total for Anderson, Bowman and Claxton respectively is {0} {1} {2}", salesA.ToString("c"), salesB.ToString("c"), salesC.ToString("c")); 
     Console.ReadLine(); 
    } 
} 

}

有另一種方式來啓動它沒有

Console.WriteLine(「首先請按任意鍵。要查看總輸入Z或Z「); mainString = Console.ReadLine(); 而(!(mainString == andr1 || mainString == 1和R 2)|| mainString = QUIT & & mainString =退出!)

似乎沒有必要

+1

沒有足夠的信息 - 這是一個控制檯應用程序,一個Web應用程序或基於窗口?您在定義項目或理解循環如何工作時遇到困難?你有沒有試過希望通過例如在CodeProject.com上? –

+2

如果你甚至不試圖展示你試圖完成任務,你會得到更多的缺點 –

回答

0

一些僞代碼:

Set salesA, salesB, salesC = 0 
While choice != 'Z' 
Begin 
    Initials = InputString 
    Number = InputNumber 
    If string[0] == 'A' Then salesA += Number 
    Else If string[0] == 'B' Then salesB += Number 
    Else If string[0] == 'C' Then salesC += Number 
End 
Print salesA, salesB and salesC 
+0

只是想說謝謝......最後,我在讀完你的僞代碼後發現了一段時間。再次感謝 – SolidFire

0

以下是一些推送:

Console.ReadKey()讀取用戶輸入的單個字符。

使用string variableName = Console.ReadLine();以字符串形式從用戶處獲得輸入後,可以使用try - catch對來查看它是否可以轉換爲數字。

您使用while循環來繼續重複某個操作。在每個循環結束時,根據用戶是否輸入'z'或任何結束程序的條件來設置條件bool變量。

讓我們知道如果您在調試時遇到一些特殊問題。