我想显示不同的输出,但我的程序显示相同。 我希望你能帮助我!
主要:
//DECLARATION
Cake cake = new Cake();
//INTEGERS
int max = 5;
int[] qty = new int[max];
int[] price = new int[max];
int i;
int qty_search;
int counter = 0, found = 0;
//STRINGS
string search;
string[] cakename = new string[max];
string[] id = new string[max];
//CHAR'S
char opt;
//LOOP
//MENU
do
{
Console.Write("1 - add cake\n2 - display cake\n3 - search cake\n4 - increase qty\n5 - decrease qty\n6 - Update qty\nx - exit\nopt --> ");
opt = Convert.ToChar(Console.ReadLine());
//SWITCH CASES
switch (opt)
{
//ADD CAKE
case '1':
Console.Write("name: ");
cakename[counter] = Console.ReadLine();
cake.cake_Name = cakename[counter];
Console.Write("id: ");
id[counter] = Console.ReadLine();
cake.id = id[counter];
Console.Write("qty: ");
qty[counter] = Convert.ToInt32(Console.ReadLine());
cake.qty = qty[counter];
Console.Write("price: ");
price[counter] = Convert.ToInt32(Console.ReadLine());
cake.price = price[counter];
counter++;
break;
//DISPLAY CAKE
case '2':
Console.WriteLine("List of Cakes");
Console.WriteLine("id -.- Name -.- qty -.- price");
for (i = 0; i < counter; i++)
{
Console.WriteLine("{0} {1} {2} {3}", cake.id, cake.cake_Name, cake.qty, cake.price);
}
Console.WriteLine("------------------");
break;
//SEARCH CAKE
case '3':
found = 0;
Console.Write("enter your search cake id: ");
search = Console.ReadLine();
for (i = 0; i < counter; i++)
{
if (string.Equals(id[i], search, StringComparison.OrdinalIgnoreCase))
{
found++;
}
}
Console.Write("found = ");
Console.WriteLine(found);
break;
case '4':
Console.WriteLine("List of Cakes");
for (i = 0; i < counter; i++)
{
Console.WriteLine("{0} {1} {2} {3}", cake.id, cake.cake_Name, cake.qty, cake.price);
}
Console.WriteLine("------------------");
Console.Write("Selected Item ID: ");
qty_search = Convert.ToInt32(Console.ReadLine());
for (i = 0; i < counter; i++)
{
if (qty_search == qty[i])
{
qty[i]++;
}
}
Console.WriteLine("cake qty + 1");
break;
case '5':
Console.WriteLine("List of Cakes");
for (i = 0; i < counter; i++)
{
Console.WriteLine("{0} {1} {2} {3}", cake.id, cake.cake_Name, cake.qty, cake.price);
}
Console.WriteLine("------------------");
Console.Write("Selected Item ID: ");
qty_search = Convert.ToInt32(Console.ReadLine());
for (i = 0; i < counter; i++)
{
if (qty_search == qty[i])
{
qty[i]--;
}
}
Console.WriteLine("cake qty + 1");
break;
case '6':
Console.WriteLine("-- update cake name --");
Console.WriteLine("Cake list");
for (i = 0; i < counter; i++)
{
Console.WriteLine("{0} {1} {2} {3}", cake.id, cake.cake_Name, cake.qty, cake.price);
}
Console.WriteLine("------------------");
Console.Write("Select item ID: ");
search = Console.ReadLine();
for (i = 0; i < counter; i++)
{
if (search == id[i])
{
Console.Write("Enter Name: ");
cakename[i] = Console.ReadLine();
Console.WriteLine("cake updated");
}
}
break;
case 'X':
case 'x':
Console.WriteLine("exit");
break;
default:
Console.WriteLine("Invalid Option");
break;
}
//SWITCH CASE END
} while (opt != 'x' && opt != 'X');
//OUTER MENU LOOP END
//PROGRAM TERMINATE
}
}
}
输入:
第一输入:
名称:蛋糕
id:2
数量:2
价格:2
第二输入:
名称:cake2
id:3
数量:3
价格:3
当我显示:
3 cake2 3 3
3 cake2 3 3
这是一样的,请帮助我。
答案 0 :(得分:1)
您不在程序中创建新的Cake
实例。您所做的是使用相同的Cake
实例并更新它。
我的建议 - 制作一个Cake
数组并将其填入循环
Cake[] CakeArray = new Cake[MAX];
您可以使用
访问每个对象 CakeArray[0] = new Cake();
CakeArray[0].ID = Console.ReadLine();;
CakeArray[0].Price = Convert.ToInt32(Console.ReadLine());
希望我帮助你
编辑:在你的案例中你可以使用Cake数组,如下所示
case '2':
Console.WriteLine("List of Cakes");
Console.WriteLine("id -.- Name -.- qty -.- price");
for (i = 0; i < counter; i++)
{
if(CakeArray[i]!=null){ // A safe check for null
Console.WriteLine("{0} {1} {2} {3}", CakeArray[i].id, CakeArray[i].cake_Name, CakeArray[i].qty, CakeArray[i].price);
}
}
Console.WriteLine("------------------");
break;
编辑 - 为前两种情况添加了建议答案 - 按照此处并更新完整答案
//DECLARATION
//INTEGERS
int max = 5;
int i;
int qty_search;
int counter = 0, found = 0;
char opt;
Cake[] CakeArray = new Cake[max];
do
{
Console.Write("1 - add cake\n2 - display cake\n3 - search cake\n4 - increase qty\n5 - decrease qty\n6 - Update qty\nx - exit\nopt --> ");
opt = Convert.ToChar(Console.ReadLine());
//SWITCH CASES
switch (opt)
{
//ADD CAKE
case '1':
CakeArray[counter] = new Cake();
Console.Write("name: ");
CakeArray[counter].cake_Name= Console.ReadLine();
Console.Write("id: ");
CakeArray[counter].id = Console.ReadLine();
Console.Write("qty: ");
CakeArray[counter].qty= Convert.ToInt32(Console.ReadLine());
Console.Write("price: ");
CakeArray[counter].price = Convert.ToInt32(Console.ReadLine());
counter++;
break;
//DISPLAY CAKE
case '2':
Console.WriteLine("List of Cakes");
Console.WriteLine("id -.- Name -.- qty -.- price");
for (i = 0; i < counter; i++)
{
if(CakeArray[i]!=null){ // A safe check for null
Console.WriteLine("{0} {1} {2} {3}", CakeArray[i].id, CakeArray[i].cake_Name, CakeArray[i].qty, CakeArray[i].price);
}
}
Console.WriteLine("------------------");
break;
default:
Console.WriteLine("Invalid Option");
break;
}
//SWITCH CASE END
} while (opt != 'x' && opt != 'X');
//OUTER MENU LOOP END
//PROGRAM TERMINATE
}
答案 1 :(得分:1)
当显示蛋糕时,您只需输入您输入的最后一个值,就像输入蛋糕一样。
快速解决方法是从string
和int
数组中读取值,而不是从您拥有的唯一Cake
实例中读取值:
for (i = 0; i < counter; i++)
{
Console.WriteLine("{0} {1} {2} {3}", id[i], cakename[i], qty[i], price[i]);
}
如KCdod建议的那样,使用一系列蛋糕Cake[]
重写您的程序将是一个绝妙的想法。
答案 2 :(得分:0)
这是你必须写的正确代码:
static void Main(string[] args)
{
//INTEGERS
int max = 5;
Cake[] cakes = new Cake[max];
int[] qty = new int[max];
int[] price = new int[max];
int i;
int qty_search;
int counter = 0, found = 0;
//STRINGS
string search;
string[] cakename = new string[max];
string[] id = new string[max];
//CHAR'S
char opt;
//LOOP
//MENU
do
{
Console.Write("1 - add cake\n2 - display cake\n3 - search cake\n4 - increase qty\n5 - decrease qty\n6 - Update qty\nx - exit\nopt --> ");
opt = Convert.ToChar(Console.ReadLine());
//SWITCH CASES
switch (opt)
{
//ADD CAKE
case '1':
Console.Write("name: ");
cakename[counter] = Console.ReadLine();
cakes[counter] = new Cake();
cakes[counter].cake_Name = cakename[counter];
Console.Write("id: ");
id[counter] = Console.ReadLine();
cakes[counter].id = id[counter];
Console.Write("qty: ");
qty[counter] = Convert.ToInt32(Console.ReadLine());
cakes[counter].qty = qty[counter];
Console.Write("price: ");
price[counter] = Convert.ToInt32(Console.ReadLine());
cakes[counter].price = price[counter];
counter++;
break;
//DISPLAY CAKE
case '2':
Console.WriteLine("List of Cakes");
Console.WriteLine("id -.- Name -.- qty -.- price");
for (i = 0; i < max; i++)
{
Console.WriteLine("{0} {1} {2} {3}", cakes[i].id, cakes[i].cake_Name, cakes[i].qty, cakes[i].price);
}
Console.WriteLine("------------------");
break;
//SEARCH CAKE
case '3':
found = 0;
Console.Write("enter your search cake id: ");
search = Console.ReadLine();
for (i = 0; i < counter; i++)
{
if (string.Equals(id[i], search, StringComparison.OrdinalIgnoreCase))
{
found++;
}
}
Console.Write("found = ");
Console.WriteLine(found);
break;
case '4':
Console.WriteLine("List of Cakes");
for (i = 0; i < counter; i++)
{
Console.WriteLine("{0} {1} {2} {3}", cakes[i].id, cakes[i].cake_Name, cakes[i].qty, cakes[i].price);
}
Console.WriteLine("------------------");
Console.Write("Selected Item ID: ");
qty_search = Convert.ToInt32(Console.ReadLine());
for (i = 0; i < counter; i++)
{
if (qty_search == qty[i])
{
qty[i]++;
}
}
Console.WriteLine("cake qty + 1");
break;
case '5':
Console.WriteLine("List of Cakes");
for (i = 0; i < counter; i++)
{
Console.WriteLine("{0} {1} {2} {3}", cakes[i].id, cakes[i].cake_Name, cakes[i].qty, cakes[i].price);
}
Console.WriteLine("------------------");
Console.Write("Selected Item ID: ");
qty_search = Convert.ToInt32(Console.ReadLine());
for (i = 0; i < counter; i++)
{
if (qty_search == qty[i])
{
qty[i]--;
}
}
Console.WriteLine("cake qty + 1");
break;
case '6':
Console.WriteLine("-- update cake name --");
Console.WriteLine("Cake list");
for (i = 0; i < counter; i++)
{
Console.WriteLine("{0} {1} {2} {3}", cakes[i].id, cakes[i].cake_Name, cakes[i].qty, cakes[i].price);
}
Console.WriteLine("------------------");
Console.Write("Select item ID: ");
search = Console.ReadLine();
for (i = 0; i < counter; i++)
{
if (search == id[i])
{
Console.Write("Enter Name: ");
cakename[i] = Console.ReadLine();
Console.WriteLine("cake updated");
}
}
break;
case 'X':
case 'x':
Console.WriteLine("exit");
break;
default:
Console.WriteLine("Invalid Option");
break;
}
//SWITCH CASE END
} while (opt != 'x' && opt != 'X');
//OUTER MENU LOOP END
//PROGRAM TERMINATE
}