- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在 Visual Studio 中用 C# 构建一个控制台应用程序,旨在执行以下三个功能:
CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
string[] stringEmployeeArray = new string[6];
stringEmployeeArray[0] = "Bob";
stringEmployeeArray[1] = "Steven";
stringEmployeeArray[2] = "Jordan";
stringEmployeeArray[3] = "Lee";
stringEmployeeArray[4] = "Max";
stringEmployeeArray[5] = "Ben";
int[] intCarsSoldArray = new int[4];
intCarsSoldArray[0] = 50;
intCarsSoldArray[1] = 100;
intCarsSoldArray[2] = 150;
intCarsSoldArray[3] = 200;
Random rnd = new Random();
Console.WriteLine("Welcome to the Car Dealership Sales Tracker");
Console.WriteLine("Press 1 to output the name and number of cars sold for the employee with the highest sales (1) ");
Console.WriteLine("Press 2 to calculate the total number of cars sold by the company (2) ");
Console.WriteLine("Press 3 to output all dsales data to a text file (3) ");
Console.WriteLine("-------------------------------------------");
Console.WriteLine("Enter option number and hit ENTER");
int val = 5;
ConsoleKeyInfo input = Console.ReadKey();
Console.WriteLine();
switch (val)
{
if (input.Key == ConsoleKey.NumPad1)
case 1:
string r = rnd.Next(stringEmployeeArray.Count()).ToString();
int x = rnd.Next(intCarsSoldArray.Count());
Console.WriteLine("This weeks employee of the week is(string)stringEmployeeArray[r] + (int)intCarsSoldArray[x]");
break;
if (input.Key == ConsoleKey.NumPad2)
case 2:
int sum = intCarsSoldArray.Sum();
Console.WriteLine("Total cars sold for this week is(intCarsSoldArray.Sum)");
break;
if (input.Key == ConsoleKey.NumPad3)
case 3:
break;
}
}
}
}
NEW CODE FOR RUFUS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int WeeklySales { get; set; }
public override string ToString()
{
return $"{FirstName} {LastName}";
}
}
public class Program
{
private static readonly Random Rnd = new Random();
private static List<Employee> Employees = new List<Employee>
{
new Employee {FirstName = "Bob"},
new Employee {FirstName = "Steven"},
new Employee {FirstName = "Jordan"},
new Employee {FirstName = "Lee"},
new Employee {FirstName = "Max"},
new Employee {FirstName = "Ben"}
};
static void Main()
{
GenerateSalesData();
MainMenu();
}
private static void ClearAndWriteHeading(string heading)
{
Console.Clear();
Console.WriteLine(heading);
Console.WriteLine(new string('-', heading.Length));
}
private static void GenerateSalesData()
{
ClearAndWriteHeading("Car Dealership Sales Tracker - Generate Sales Data");
foreach (var employee in Employees)
{
employee.WeeklySales = Rnd.Next(50, 201);
}
Console.WriteLine("\nSales data has been generated!!");
}
private static void MainMenu()
{
ClearAndWriteHeading("Car Dealership Sales Tracker - Main Menu");
Console.WriteLine("1: Display employee of the week information");
Console.WriteLine("2: Display total number of cars sold by the company");
Console.WriteLine("3: Write all sales data to a text file");
Console.WriteLine("4: Generate new weekly sales info");
Console.WriteLine("5: Exit program\n");
Console.Write("Enter option number 1 - 5: ");
// Get input from user (ensure they only enter 1 - 5)
int input;
while (!int.TryParse(Console.ReadKey().KeyChar.ToString(), out input) ||
input < 1 || input > 5)
{
// Erase input and wait for valid response
Console.SetCursorPosition(0, 8);
Console.Write("Enter option number 1 - 5: ");
Console.SetCursorPosition(Console.CursorLeft - 1, 8);
}
ProcessMenuItem(input);
}
private static void ProcessMenuItem(int itemNumber)
{
switch (itemNumber)
{
case 1:
DisplayEmployeeOfTheWeekInfo();
break;
case 2:
DisplayTotalSales();
break;
case 3:
WriteSalesToFile();
break;
case 4:
GenerateSalesData();
break;
default:
Environment.Exit(0);
break;
}
Console.Write("\nPress any key to go back to main menu...");
Console.ReadKey();
MainMenu();
}
private static void DisplayEmployeeOfTheWeekInfo()
{
ClearAndWriteHeading("Car Dealership Sales Tracker - Employee of the Week");
var employeeOfTheWeek = Employees.OrderByDescending(employee => employee.WeeklySales).First();
Console.WriteLine($"{employeeOfTheWeek} is the employee of the week!");
Console.WriteLine($"This person sold {employeeOfTheWeek.WeeklySales} cars this week.");
}
private static string GetSalesData()
{
var data = new StringBuilder();
data.AppendLine("Employee".PadRight(25) + "Sales");
data.AppendLine(new string('-', 30));
foreach (var employee in Employees)
{
data.AppendLine(employee.FirstName.PadRight(25) + employee.WeeklySales);
}
data.AppendLine(new string('-', 30));
data.AppendLine("Total".PadRight(25) +
Employees.Sum(employee => employee.WeeklySales));
return data.ToString();
}
private static void DisplayTotalSales()
{
ClearAndWriteHeading("Car Dealership Sales Tracker - Total Sales");
Console.WriteLine("\nHere are the total sales for the week:\n");
Console.WriteLine(GetSalesData());
}
private static void WriteSalesToFile(string errorMessage = null)
{
ClearAndWriteHeading("Car Dealership Sales Tracker - Write Sales Data To File");
if (!string.IsNullOrEmpty(errorMessage)) Console.WriteLine($"\n{errorMessage}\n");
Console.Write("\nEnter the path to the sales data file:");
var filePath = Console.ReadLine();
var salesData = GetSalesData();
var error = false;
if (ERROR HERE File.Exists(filePath))
{
Console.Write("File exists. (O)verwrite or (A)ppend: ");
var input = Console.ReadKey().Key;
while (input != ConsoleKey.A && input != ConsoleKey.O)
{
Console.Write("Enter 'O' or 'A': ");
input = Console.ReadKey().Key;
}
if (input == ConsoleKey.A)
{
ERROR HERE File.AppendAllText(filePath, salesData);
}
else
{
ERROR HERE File.WriteAllText(filePath, salesData);
}
}
else
{
try
{
ERROR HERE File.WriteAllText(filePath, salesData);
}
catch
{
error = true;
}
}
if (error)
{
WriteSalesToFile($"Unable to write to file: {filePath}\nPlease try again.");
}
else
{
Console.WriteLine($"\nSuccessfully added sales data to file: {filePath}");
}
}
}
}
最佳答案
我认为您可能想退后一步检查整体设计。菜单系统有很多可移动的部分,将需要执行的不同任务分解为单独的功能会很有帮助。
例如,你可以有一个通用函数来清除控制台并显示一个菜单标题,你可以有一个显示菜单选项的方法,你可以有一个用于每个选项本身的方法。这样,您只需从 Main
调用“MainMenu”方法,然后主菜单方法将获取用户输入并根据用户输入的内容调用适当的方法。
您还可以使用一种随机化员工数据的方法,以便您可以根据需要模拟新的一周。而且,就此而言,存储员工姓名和他或她每周销售额的简单员工类也可以派上用场。
这就是我将如何做到的。首先是Employee
类,它将名称与每周销售额相关联:
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int WeeklySales { get; set; }
public override string ToString()
{
return $"{FirstName} {LastName}";
}
}
Program
我们可以有一些私有(private)变量来保存员工和
Random
的实例。类(class):
public class Program
{
private static readonly Random Rnd = new Random();
private static List<Employee> Employees = new List<Employee>
{
new Employee {FirstName = "Bob"},
new Employee {FirstName = "Steven"},
new Employee {FirstName = "Jordan"},
new Employee {FirstName = "Lee"},
new Employee {FirstName = "Max"},
new Employee {FirstName = "Ben"}
};
Main
方法我们可以只调用一个方法来获取一些随机销售数据,并调用另一个方法来显示主菜单:
private static void Main()
{
GenerateSalesData();
MainMenu();
}
private static void ClearAndWriteHeading(string heading)
{
Console.Clear();
Console.WriteLine(heading);
Console.WriteLine(new string('-', heading.Length));
}
employees
并且,对于每一个,选择一个介于 50 到 200 之间的随机数并将其分配给
Employee.WeeklySales
属性(property):
private static void GenerateSalesData()
{
ClearAndWriteHeading("Car Dealership Sales Tracker - Generate Sales Data");
foreach (var employee in Employees)
{
employee.WeeklySales = Rnd.Next(50, 201);
}
Console.WriteLine("\nSales data has been generated!!");
}
int.TryParse
方法)并且 int 在我们指定的范围内。当我们得到有效输入时,我们调用不同的方法来处理该输入:
private static void MainMenu()
{
ClearAndWriteHeading("Car Dealership Sales Tracker - Main Menu");
Console.WriteLine("1: Display employee of the week information");
Console.WriteLine("2: Display total number of cars sold by the company");
Console.WriteLine("3: Write all sales data to a text file");
Console.WriteLine("4: Generate new weekly sales info");
Console.WriteLine("5: Exit program\n");
Console.Write("Enter option number 1 - 5: ");
// Get input from user (ensure they only enter 1 - 5)
int input;
while (!int.TryParse(Console.ReadKey().KeyChar.ToString(), out input) ||
input < 1 || input > 5)
{
// Erase input and wait for valid response
Console.SetCursorPosition(0, 8);
Console.Write("Enter option number 1 - 5: ");
Console.SetCursorPosition(Console.CursorLeft - 1, 8);
}
ProcessMenuItem(input);
}
switch
输入值上的语句,然后为该命令调用适当的方法:
private static void ProcessMenuItem(int itemNumber)
{
switch (itemNumber)
{
case 1:
DisplayEmployeeOfTheWeekInfo();
break;
case 2:
DisplayTotalSales();
break;
case 3:
WriteSalesToFile();
break;
case 4:
GenerateSalesData();
break;
default:
Environment.Exit(0);
break;
}
Console.Write("\nPress any key to go back to main menu...");
Console.ReadKey();
MainMenu();
}
employees
按
WeeklySales
列出属性,并选择第一个:
private static void DisplayEmployeeOfTheWeekInfo()
{
ClearAndWriteHeading("Car Dealership Sales Tracker - Employee of the Week");
var employeeOfTheWeek = Employees
.OrderByDescending(employee => employee.WeeklySales)
.First();
Console.WriteLine($"{employeeOfTheWeek} is the employee of the week!");
Console.WriteLine(
$"This person sold {employeeOfTheWeek.WeeklySales} cars this week.");
}
GetSalesData
方法循环遍历我们所有的员工并显示他们的销售额,以及最后的总销售额:
private static string GetSalesData()
{
var data = new StringBuilder();
data.AppendLine("Employee".PadRight(25) + "Sales");
data.AppendLine(new string('-', 30));
foreach (var employee in Employees)
{
data.AppendLine(employee.FirstName.PadRight(25) + employee.WeeklySales);
}
data.AppendLine(new string('-', 30));
data.AppendLine("Total".PadRight(25) +
Employees.Sum(employee => employee.WeeklySales));
return data.ToString();
}
private static void DisplayTotalSales()
{
ClearAndWriteHeading("Car Dealership Sales Tracker - Total Sales");
Console.WriteLine("\nHere are the total sales for the week:\n");
Console.WriteLine(GetSalesData());
}
GetSalesData
)。
try/catch
阻止重试该方法(通过从自身调用它,并带有错误消息)以防出现某些文件 I/O 错误:
private static void WriteSalesToFile(string errorMessage = null)
{
ClearAndWriteHeading("Car Dealership Sales Tracker - Write Sales Data To File");
if (!string.IsNullOrEmpty(errorMessage)) Console.WriteLine($"\n{errorMessage}\n");
Console.Write("\nEnter the path to the sales data file: ");
var filePath = Console.ReadLine();
var salesData = GetSalesData();
var error = false;
if (File.Exists(filePath))
{
Console.Write("File exists. (O)verwrite or (A)ppend: ");
var input = Console.ReadKey().Key;
while (input != ConsoleKey.A && input != ConsoleKey.O)
{
Console.Write("Enter 'O' or 'A': ");
input = Console.ReadKey().Key;
}
if (input == ConsoleKey.A)
{
File.AppendAllText(filePath, salesData);
}
else
{
File.WriteAllText(filePath, salesData);
}
}
else
{
try
{
File.WriteAllText(filePath, salesData);
}
catch
{
error = true;
}
}
if (error)
{
WriteSalesToFile($"Unable to write to file: {filePath}\nPlease try again.");
}
else
{
Console.WriteLine($"\nSuccessfully added sales data to file: {filePath}");
}
}
关于C#数字键决定三个数组操作中的哪一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49652605/
我想做的是让 JTextPane 在 JPanel 中占用尽可能多的空间。对于我使用的 UpdateInfoPanel: public class UpdateInfoPanel extends JP
我在 JPanel 中有一个 JTextArea,我想将其与 JScrollPane 一起使用。我正在使用 GridBagLayout。当我运行它时,框架似乎为 JScrollPane 腾出了空间,但
我想在 xcode 中实现以下功能。 我有一个 View Controller 。在这个 UIViewController 中,我有一个 UITabBar。它们下面是一个 UIView。将 UITab
有谁知道Firebird 2.5有没有类似于SQL中“STUFF”函数的功能? 我有一个包含父用户记录的表,另一个表包含与父相关的子用户记录。我希望能够提取用户拥有的“ROLES”的逗号分隔字符串,而
我想使用 JSON 作为 mirth channel 的输入和输出,例如详细信息保存在数据库中或创建 HL7 消息。 简而言之,输入为 JSON 解析它并输出为任何格式。 最佳答案 var objec
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个以前可能被问过的问题,但我很难找到正确的描述。我希望有人能帮助我。 在下面的代码中,我设置了varprice,我想添加javascript变量accu_id以通过rails在我的数据库中查找记
我有一个简单的 SVG 文件,在 Firefox 中可以正常查看 - 它的一些包装文本使用 foreignObject 包含一些 HTML - 文本包装在 div 中:
所以我正在为学校编写一个 Ruby 程序,如果某个值是 1 或 3,则将 bool 值更改为 true,如果是 0 或 2,则更改为 false。由于我有 Java 背景,所以我认为这段代码应该有效:
我做了什么: 我在这些账户之间创建了 VPC 对等连接 互联网网关也连接到每个 VPC 还配置了路由表(以允许来自双方的流量) 情况1: 当这两个 VPC 在同一个账户中时,我成功测试了从另一个 La
我有一个名为 contacts 的表: user_id contact_id 10294 10295 10294 10293 10293 10294 102
我正在使用 Magento 中的新模板。为避免重复代码,我想为每个产品预览使用相同的子模板。 特别是我做了这样一个展示: $products = Mage::getModel('catalog/pro
“for”是否总是检查协议(protocol)中定义的每个函数中第一个参数的类型? 编辑(改写): 当协议(protocol)方法只有一个参数时,根据该单个参数的类型(直接或任意)找到实现。当协议(p
我想从我的 PHP 代码中调用 JavaScript 函数。我通过使用以下方法实现了这一点: echo ' drawChart($id); '; 这工作正常,但我想从我的 PHP 代码中获取数据,我使
这个问题已经有答案了: Event binding on dynamically created elements? (23 个回答) 已关闭 5 年前。 我有一个动态表单,我想在其中附加一些其他 h
我正在尝试找到一种解决方案,以在 componentDidMount 中的映射项上使用 setState。 我正在使用 GraphQL连同 Gatsby返回许多 data 项目,但要求在特定的 pat
我在 ScrollView 中有一个 View 。只要用户按住该 View ,我想每 80 毫秒调用一次方法。这是我已经实现的: final Runnable vibrate = new Runnab
我用 jni 开发了一个 android 应用程序。我在 GetStringUTFChars 的 dvmDecodeIndirectRef 中得到了一个 dvmabort。我只中止了一次。 为什么会这
当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到
当我按下 Activity 中的按钮时,会弹出一个 DialogFragment。在对话框 fragment 中,有一个看起来像普通 ListView 的 RecyclerView。 我想要的行为是当
我是一名优秀的程序员,十分优秀!