我收到一个错误。命名空间不能直接包含字段或方法等成员 [英] I am getting an error.a namespace cannot directly contain members such as fields or methods

查看:326
本文介绍了我收到一个错误。命名空间不能直接包含字段或方法等成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建名为BankAccountTester的项目。创建一个名为BankAccount的类。银行帐户包含余额字段。银行账户余额应初始化为0.银行账户类别应包括一笔存款,该金额会将转入的金额加到余额中,并扣除一笔金额。



我尝试过:



Create a project named BankAccountTester. Create a class named BankAccount. A bank account has fields for a balance. The bank account balance should be initialized to 0. The bank account class should include a deposit which adds an amount passed to it to the balance and a withdraw which subtracts the amount.

What I have tried:

using System;


namespace Banking
{
    public static void Main(string []args)
    { }

 //A bank account has a balance
    //that can be changed with 
    // withdraw or deposit.
    public class BankAccount
    {
        private double balance;
        //constructs a bank account 
        // with zero balance
        public BankAccount()
        {
            balance = 0;
        }
        //Constructs a bank account
        //with a given balance 
        //(initalBalance)
        public BankAccount(double initialBalance)
        {
            balance = initialBalance;
        }
        //Deposits money into the bank account.
        //(amount the amount to deposit)
        public void Deposit(double amount)
        {
            double newBalance = balance + amount;
            balance = newBalance;
        }
        //Withdraws money from the bank account
        //(the amount to withdraw)
        public void Withdraw(double amount)
        {
            double newBalance = balance - amount;
            balance = newBalance;
        }
        //Gets the current balance of the bank account.
        //(returns the current balance)
        public double GetBalance()
        {
            Console.ReadLine();
            return balance;
        }
    }
}

推荐答案

你需要移动这一行



You need to move this line

public static void Main(string []args)
    { }



进入


Into the

public class BankAccount


这篇关于我收到一个错误。命名空间不能直接包含字段或方法等成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆