Getters 和 Setters 不将变量保存到输出
Getters and Setters not saving variable to output
我已经尝试了几个小时来修复这段代码,但我似乎无法理解这个问题,我正在用 get 和 set 存储一个包含案例的 public 变量。我想做的是使用下面的这个菜单来存储变量。
{
Volume vol = new Volume();
Console.WriteLine("\n ---------------\n" +
" |>>>>> Control Panel <<<<<|\n" +
" ---------------" +
"\n\n |===========================================|\n" +
"\n Press key...\n" +
"\n [Q] to Change M Main 1 Volume\n" +
" [W] to Change M Main 2 Volume\n" +
" [E] to Change Host Volume\n" +
" [R] to Change Guest Volume\n" +
" [T] to Change Speaker Volume\n" +
" [Y] to Change Lights\n" +
" [G] to exit\n" +
"\n |===========================================|");
ConsoleKeyInfo inputuser = Console.ReadKey(true);
switch (inputuser.Key)
{
case ConsoleKey.Q:
{
try
{
Console.WriteLine("| | |Input Volume percentege from 0-100 %| | |");
vol.MainMic1 = int.Parse(Console.ReadLine());
if (vol.MainMic1 > 100 || vol.MainMic1 < 0)
{
Console.WriteLine("Invalid Number");
Console.ReadKey();
}
else if (vol.MainMic1 == 0 || vol.MainMic1 <= 100)
Console.WriteLine(vol.MainMic1);
Console.ReadKey(); ```
这会在当前方法中设置它,因为我尝试过但不是在我用来调用它的方法中,当我在控制台中调用它时它 returns 其原始值或 0
public void ShowVolume()
{
Volume voll = new Volume();
Console.Clear();
Console.WriteLine("|====== ====== ====== ====== ====== ====== ======|\n" +
"\n >>>>> Lights and Volume settings <<<<<\n" +
"\nMain 1(Peterson) Microphone currently at: {0}% volume.\n" +
"Co-Speaker(Weiss) Microphone currently at: {1}% volume.\n" +
"Host Microphone currently at: {2}% volume.\n" +
"Guest Microphone currently at: {3}% volume.\n" +
"Quad-Speakers currently at: {4}% volume.\n" +
"Lights are at: {5}% power.\n" +
"\n|====== ====== ====== ====== ====== ====== ======|", voll.MainMic1, voll.MainMic2, voll.MainMic3, voll.GuestMic, voll.QuadSpeaker, voll.Lighting);
Console.ReadKey();
我想不通问题,我把所有东西都弄到运行,但不打印所问的内容,这里是否有某种情况/方法阻塞我不明白?
here is class with get/set
public class Volume
{
private int o_mainmic1 = 50;
private int o_mainmic2 = 50;
private int o_mainmic3 = 50;
private int o_guestmic = 30;
private int o_quadspeaker = 30;
private int o_lighting = 30;
public int MainMic1
{
get
{
return o_mainmic1;
}
set
{
o_mainmic1 = value;
}
}
这里有一些 Gyazo 图片,如果对你有帮助的话,我很感激我在这里得到的任何帮助,在此先感谢。
https://gyazo.com/d241e1f8db821ba38415ecf1c57fb3f9
https://gyazo.com/35f31140568794aa41ee74c8ead2296a
第一步:
使音量静态:
public static class Volume
{
private int o_mainmic1 = 50;
private int o_mainmic2 = 50;
private int o_mainmic3 = 50;
private int o_guestmic = 30;
private int o_quadspeaker = 30;
private int o_lighting = 30;
public int MainMic1
{
get
{
return o_mainmic1;
}
set
{
o_mainmic1 = value;
}
}
第 2 步:将提及的每个 vol
或 voll
重命名为 Volume
,并且不初始化卷 class。
[第一个代码示例]
{
Volume vol = new Volume(); // Delete this, this is not needed anymore since Volume is a static class.
/* I skipped over the Console.WriteLine stuff, though you need it.
*/
ConsoleKeyInfo inputuser = Console.ReadKey(true);
switch (inputuser.Key)
{
case ConsoleKey.Q:
{
try
{
Console.WriteLine("| | |Input Volume percentege from 0-100 %| | |");
Volume.MainMic1 = int.Parse(Console.ReadLine());
if (Volume.MainMic1 > 100 || Volume.MainMic1 < 0)
{
Console.WriteLine("Invalid Number");
Console.ReadKey();
}
else if (Volume.MainMic1 == 0 || Volume.MainMic1 <= 100)
{
Console.WriteLine(vol.MainMic1);
Console.ReadKey();
}
[第二个代码示例]
public void ShowVolume()
{
Volume voll = new Volume(); //Again, delete this.
Console.Clear();
Console.WriteLine("|====== ====== ====== ====== ====== ====== ======|\n" +
"\n >>>>> Lights and Volume settings <<<<<\n" +
"\nMain 1(Peterson) Microphone currently at: {0}% volume.\n" +
"Co-Speaker(Weiss) Microphone currently at: {1}% volume.\n" +
"Host Microphone currently at: {2}% volume.\n" +
"Guest Microphone currently at: {3}% volume.\n" +
"Quad-Speakers currently at: {4}% volume.\n" +
"Lights are at: {5}% power.\n" +
"\n|====== ====== ====== ====== ====== ====== ======|", Volume.MainMic1, Volume.MainMic2, Volume.MainMic3, Volume.GuestMic, Volume.QuadSpeaker, Volume.Lighting);
Console.ReadKey();
我已经尝试了几个小时来修复这段代码,但我似乎无法理解这个问题,我正在用 get 和 set 存储一个包含案例的 public 变量。我想做的是使用下面的这个菜单来存储变量。
{
Volume vol = new Volume();
Console.WriteLine("\n ---------------\n" +
" |>>>>> Control Panel <<<<<|\n" +
" ---------------" +
"\n\n |===========================================|\n" +
"\n Press key...\n" +
"\n [Q] to Change M Main 1 Volume\n" +
" [W] to Change M Main 2 Volume\n" +
" [E] to Change Host Volume\n" +
" [R] to Change Guest Volume\n" +
" [T] to Change Speaker Volume\n" +
" [Y] to Change Lights\n" +
" [G] to exit\n" +
"\n |===========================================|");
ConsoleKeyInfo inputuser = Console.ReadKey(true);
switch (inputuser.Key)
{
case ConsoleKey.Q:
{
try
{
Console.WriteLine("| | |Input Volume percentege from 0-100 %| | |");
vol.MainMic1 = int.Parse(Console.ReadLine());
if (vol.MainMic1 > 100 || vol.MainMic1 < 0)
{
Console.WriteLine("Invalid Number");
Console.ReadKey();
}
else if (vol.MainMic1 == 0 || vol.MainMic1 <= 100)
Console.WriteLine(vol.MainMic1);
Console.ReadKey(); ```
这会在当前方法中设置它,因为我尝试过但不是在我用来调用它的方法中,当我在控制台中调用它时它 returns 其原始值或 0
public void ShowVolume()
{
Volume voll = new Volume();
Console.Clear();
Console.WriteLine("|====== ====== ====== ====== ====== ====== ======|\n" +
"\n >>>>> Lights and Volume settings <<<<<\n" +
"\nMain 1(Peterson) Microphone currently at: {0}% volume.\n" +
"Co-Speaker(Weiss) Microphone currently at: {1}% volume.\n" +
"Host Microphone currently at: {2}% volume.\n" +
"Guest Microphone currently at: {3}% volume.\n" +
"Quad-Speakers currently at: {4}% volume.\n" +
"Lights are at: {5}% power.\n" +
"\n|====== ====== ====== ====== ====== ====== ======|", voll.MainMic1, voll.MainMic2, voll.MainMic3, voll.GuestMic, voll.QuadSpeaker, voll.Lighting);
Console.ReadKey();
我想不通问题,我把所有东西都弄到运行,但不打印所问的内容,这里是否有某种情况/方法阻塞我不明白?
here is class with get/set
public class Volume
{
private int o_mainmic1 = 50;
private int o_mainmic2 = 50;
private int o_mainmic3 = 50;
private int o_guestmic = 30;
private int o_quadspeaker = 30;
private int o_lighting = 30;
public int MainMic1
{
get
{
return o_mainmic1;
}
set
{
o_mainmic1 = value;
}
}
这里有一些 Gyazo 图片,如果对你有帮助的话,我很感激我在这里得到的任何帮助,在此先感谢。
https://gyazo.com/d241e1f8db821ba38415ecf1c57fb3f9
https://gyazo.com/35f31140568794aa41ee74c8ead2296a
第一步: 使音量静态:
public static class Volume
{
private int o_mainmic1 = 50;
private int o_mainmic2 = 50;
private int o_mainmic3 = 50;
private int o_guestmic = 30;
private int o_quadspeaker = 30;
private int o_lighting = 30;
public int MainMic1
{
get
{
return o_mainmic1;
}
set
{
o_mainmic1 = value;
}
}
第 2 步:将提及的每个 vol
或 voll
重命名为 Volume
,并且不初始化卷 class。
[第一个代码示例]
{
Volume vol = new Volume(); // Delete this, this is not needed anymore since Volume is a static class.
/* I skipped over the Console.WriteLine stuff, though you need it.
*/
ConsoleKeyInfo inputuser = Console.ReadKey(true);
switch (inputuser.Key)
{
case ConsoleKey.Q:
{
try
{
Console.WriteLine("| | |Input Volume percentege from 0-100 %| | |");
Volume.MainMic1 = int.Parse(Console.ReadLine());
if (Volume.MainMic1 > 100 || Volume.MainMic1 < 0)
{
Console.WriteLine("Invalid Number");
Console.ReadKey();
}
else if (Volume.MainMic1 == 0 || Volume.MainMic1 <= 100)
{
Console.WriteLine(vol.MainMic1);
Console.ReadKey();
}
[第二个代码示例]
public void ShowVolume()
{
Volume voll = new Volume(); //Again, delete this.
Console.Clear();
Console.WriteLine("|====== ====== ====== ====== ====== ====== ======|\n" +
"\n >>>>> Lights and Volume settings <<<<<\n" +
"\nMain 1(Peterson) Microphone currently at: {0}% volume.\n" +
"Co-Speaker(Weiss) Microphone currently at: {1}% volume.\n" +
"Host Microphone currently at: {2}% volume.\n" +
"Guest Microphone currently at: {3}% volume.\n" +
"Quad-Speakers currently at: {4}% volume.\n" +
"Lights are at: {5}% power.\n" +
"\n|====== ====== ====== ====== ====== ====== ======|", Volume.MainMic1, Volume.MainMic2, Volume.MainMic3, Volume.GuestMic, Volume.QuadSpeaker, Volume.Lighting);
Console.ReadKey();