C# for 循环问题

标签 c#

谁能告诉我为什么这不起作用。当我进入循环时,它会打印所有内容而不是一行并获取用户输入。它打印 Enter integer the account numberEnter integer the account balanceEnter the account holder lastname

谢谢大家,但现在搜索帐户不起作用

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



namespace ConsoleApplication1

{

class accounts

{

private int[] accountnum = new int[5]; //private accountnum array of five integer account numbers        

private int[] accountbal = new int[5];//private balance array of five balance amounts

private string[] accountname = new string[5];//private accntname array to hold five last names


        public void fillAccounts()
        {
            int bal;
            int accountnumber;
            string name;

            for (int x = 0; x < 5; ++x)
            {

                Console.Write("Enter the integer the account number");
                accountnumber = Console.Read();
                Console.Write("Enter the integer the account balance");
                bal = Console.Read();
                Console.Write("Enter the account holder lastname");
                name = Console.ReadLine();

                accountnum[x] = accountnumber;
                accountbal[x] = bal;
                accountname[x] = name;
            }




        }
        public void searchAccounts()
        {
            Console.WriteLine("Enter the account number");
            int acctnum = Console.Read();
            for (int x = 0; x < 6; ++x)
            {
                if (x < 5)
                {
                    if (accountnum[x] == acctnum)
                    {
                        Console.WriteLine("Account #{0} has a balance of {1} for customer {2}", acctnum, accountbal[x].ToString("C"), accountname[x]);
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("You entered invalid account number");
                }
            }

        }
        public void averageAccounts()
        {
            int sum = 0;
            int avg;


            for (int x = 0; x < 5; ++x)
            {
                sum = accountbal[x] + sum;
            }
            avg = sum / 5;
            Console.WriteLine("The average dollar amount is {0}", avg.ToString("c"));
        }
    }
    class assignment3_alt
    {
        static void Main(string[] args)
        {
            accounts myclass = new accounts();
            string userin;


            myclass.fillAccounts();
            int i = 0;
            while (i != 1)
            {//use the following menu:            
                Console.WriteLine("*****************************************");
                Console.WriteLine("enter an a or A to search account numbers");
                Console.WriteLine("enter a b or B to average the accounts");
                Console.WriteLine("enter an x or X to exit program");
                Console.WriteLine("*****************************************");
                Console.Write("Enter option-->");
                userin = Console.ReadLine();
                if (userin == "a" || userin == "A")
                {
                    myclass.searchAccounts();
                }
                else if (userin == "b" || userin == "B")
                {
                    myclass.averageAccounts();
                }
                else if (userin == "x" || userin == "X")
                {
                    break;
                }
                else
                {
                    Console.WriteLine("You entered an invalid option");
                }
            }
        }
    }
}

最佳答案

Console.Read只读取一个字符。您需要使用 Console.ReadLine .

Console.Write("Enter the integer the account number");
accountnumber = int.Parse(Console.ReadLine());
Console.Write("Enter the integer the account balance");
bal = int.Parse(Console.ReadLine());
Console.Write("Enter the account holder lastname");
name = Console.ReadLine();

您可能还想考虑使用 int.TryParse而不是 int.Parse 以便您可以更好地处理无效输入。

关于C# for 循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3713978/

相关文章:

c# - 为什么本地数组的读/写速度比静态数组快?

c# - Application.DoEvents() 是我唯一的选择(在这种情况下)吗?

c# - 如何将 SQL 转录为 LINQ

c# - 将 XML 解析为列表

c# - 如何使用 linq 初始化重复元素数组?

c# - 复制字典没有给我预期的结果

c# - 从 winform 使用 URI 启动应用程序

c# - 如何在 C# 中确定 Windows 进程使用的 tcp 端口

c# - EF Core .net 6 Cosmos 迁移问题

c# - PageMethods 和 session