עבור לתוכן

סידור שורות בטבלה ב-C#

Featured Replies

פורסם

יש לי תרגיל שבו אני נותן קוד הזמנה (A,B,C,D או E) רווח וכמות, מכניס כמה הזמנות שאני רוצה (אנטר עבור הזמנה חדשה) ובסוף x לפירוט ההזמנה.

בתוצאה שאני אמור לקבל זה טבלה שבעמודה הראשונה את הקוד (A,B,C,D או E), אח"כ כמות,מחיר של יחידה (נתון לי) ,כמה עולה כל הזמנה של פריט בנפרד ובסוף למטה בשורה חדשה ה-TOTAL של כל ההזמנה.

לפעמים שורה או שניים יוצאים לי מהטבלה:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace T266B
{
class Program
{
//method who calclate the products price
static int Price(char product)
{
int ThePrice = 0;


if (product == 'A')
{
ThePrice = 100;
}
else if (product == 'B')
{
ThePrice = 50;
}
else if (product == 'C')
{
ThePrice = 220;
}
else if (product == 'D')
{
ThePrice = 17;
}
else if (product == 'E')
{
ThePrice = 25;
}


return ThePrice;
}


static char ReadItem(string OrderText, out int quantity)
{
string item = "";
quantity = 0;


for (int i = 2; i < OrderText.Length; i++)
{
item += OrderText[i];
}


quantity = int.Parse(item);


return OrderText[0];
}


static void Main(string[] args)
{
char[] ItemArray = new char[4];
int[] QuantityArray = new int[4];
int[] UnitPriceArray = new int[4];
int[] TotalPrice = new int[4];


//read text from user
Console.WriteLine("Enter Order Code And Quantity: ");
string text = Console.ReadLine();


int quantity;
char tav = ' ';
int TotalOrder = 0;
while (text[0] != 'x')
{
for (int i = 0; i < text.Length + 1; i++)
{
tav = ReadItem(text, out quantity);


int price = Price(tav);


ItemArray[i] = tav;
QuantityArray[i] = quantity;
UnitPriceArray[i] = price;
TotalPrice[i] = (quantity * price);
TotalOrder += TotalPrice[i];


text = Console.ReadLine();
}
}


Console.WriteLine("Item Quantity Unit Price Total Price");




for (int i = 0; i < 4; i++)
{
Console.Write(ItemArray[i]);
Console.Write(" " + QuantityArray[i]);
Console.Write(" " + UnitPriceArray[i]);
Console.Write(" " + TotalPrice[i]);
Console.WriteLine();
}


Console.Write("Total " + TotalOrder);
Console.ReadLine();
}
}
}

פורסם

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;




namespace T266B
{
class Program
{
//method who calclate the products price
static int Price(char product)
{
int ThePrice = 0;




if (product == 'A')
{
ThePrice = 100;
}
else if (product == 'B')
{
ThePrice = 50;
}
else if (product == 'C')
{
ThePrice = 220;
}
else if (product == 'D')
{
ThePrice = 17;
}
else if (product == 'E')
{
ThePrice = 25;
}




return ThePrice;
}




static char ReadItem(String OrderText, out int quantity)
{
String item = "";
quantity = 0;




for (int i = 2; i < OrderText.Length; i++)
{
item += OrderText[i];
}




quantity = int.Parse(item);




return OrderText[0];
}




static void Main(string[] args)
{
char[] ItemArray = new char[5];
int[] QuantityArray = new int[5];
int[] UnitPriceArray = new int[5];
int[] TotalPrice = new int[5];




//read text from user
Console.WriteLine("Enter Order Code And Quantity: ");
String text = Console.ReadLine();




int quantity;
char tav = ' ';
int TotalOrder = 0;
int j = 0;
while (text[0] != 'x')
{

tav = ReadItem(text, out quantity);




int price = Price(tav);




ItemArray[j] = tav;
QuantityArray[j] = quantity;
UnitPriceArray[j] = price;
TotalPrice[j] = (quantity * price);
TotalOrder += TotalPrice[j];
j++;


text = Console.ReadLine();
}




Console.WriteLine("Item\tQuantity\tUnit Price\tTotal Price");


for (int i = 0; i < 5; i++)
{
Console.Write(ItemArray[i]);
Console.Write("\t" + QuantityArray[i]);
Console.Write("\t\t" + UnitPriceArray[i]);
Console.Write("\t\t" + TotalPrice[i]);
Console.WriteLine();
}




Console.Write("Total\t\t\t\t\t" + TotalOrder);
Console.ReadLine();
}
}}

הורדתי לך את לולאת ה FOR בתוך ה WHILE. הגדלתי את האינדקסים של המערכים מ-4 ל-5 (עבור האות E). סידרתי את הטבלה - הוספתי טאבים במקום רווחים.

צירפתי קובץ פלט לדוגמא.

[attachment deleted by admin]

פורסם
  • מחבר

תוכל להסביר לי בבקשה מה בדיוק עשית ? (לא ממש מבין)

ומה זה ה-/t ?

פורסם

טאבים

פורסם
  • מחבר

למה בהדפסה למסך אתה קופץ טאב, אח"כ 2 טאבים ואח"כ שוב 2 טאבים ולא 3 ?

פורסם

זה פקודת WRITE אני קופץ מהמקום היחסי הקודם כמובן - הכול באותה שורה.

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים