需要有关错误的帮助. [英] Need help with errors.

查看:85
本文介绍了需要有关错误的帮助.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

namespace MySavings
{
    class PiggyBank
    {

        private double numQuarters, numDimes, numNickels, numPennies;

        public PiggyBank()
        {
            numQuarters = 0;
            numDimes = 0;
            numNickels = 0;
            numPennies = 0;
            
        }

        /* Precondition: None.
         * showMenu just prints the menu to the screen.
         * Postcondition: The menu is printed to the screen.
         */
        public void showMenu()
        {
            Console.WriteLine("1. Show total in bank.");
            Console.WriteLine("2. Add a penny.");
            Console.WriteLine("3. Add a nickel.");
            Console.WriteLine("4. Add a dime.");
            Console.WriteLine("5. Add a quarter.");
            Console.WriteLine("6. Take all money out of bank.");
            Console.WriteLine("Enter 0 to quit.");            
        }

        /* Precondition: None.
         * addPenny increases the number of pennies in this bank by 1.
         * Postcondition: There is another penny in the bank.
         */
        public void addPenny()
        {
            numPennies++;
        }

        /* Precondition: None.
         * addNickel increases the number of nickels in this bank by 1.
         * Postcondition: There is another nickel in the bank.
         */
        public void addNickel()
        {
            numNickels++;
        }
        
        /* Precondition: None.
         * addDime increases the number of dimes in this bank by 1.
         * Postcondition: There is another dime in the bank.
         */
        public void addDime()
        {
            numDimes++;
        }
        
        /* Precondition: None.
         * addQuarter increases the number of quarters in this bank by 1.
         * Postcondition: There is another quarter in the bank.
         */
        public void addQuarter()
        {
            numQuarters++;
        }

        /* Precondition: None
         * getTotal returns the total amount of money in the bank currently.
         * The total is calculated exactly like it was in your AddCoins application.
         * Postcondition: The total is returned.
         */
        public double getTotal()
        {
            double Total = numQuarters + numPennies + numNickels + numDimes;
            return Total; //CHANGE THIS TO RETURN THE ACTUAL TOTAL
            
        }

        /* Precondition: None.
         * removeMoneyFromBank prints how much money is in the bank, and changes all
         * of the values of the coins in the bank to 0
         * Postcondition: The total is printed and the bank is now empty.
         */
        public void removeMoneyFromBank()
        {
            Console.Write("Your available money in the bank is:" + getTotal());
            numQuarters = 0;
            numPennies = 0;
            numNickels = 0;
            numDimes = 0;

        }
    }
}

推荐答案

您所有的if语句都应为==而不是=

例如:

如果(i = 6)

应该是

如果(i == 6)
All of your if statements should be == not =

For example:

if (i = 6)

should be

if (i == 6)


只需将其添加到零条件即可.

just add this to zero condition.

class Program
    {
        static void Main(string[] args)
        {
            //You need a loop to keep showing the menu and asking for a response
            //until the user enters a 0.
            //Assume the person will always enter a valid option.
            //Main MUST be the place that an option is entered!

 
            PiggyBank pb = new PiggyBank();
            Console.WriteLine("1. Show total in bank.");
            Console.WriteLine("2. Add a penny.");
            Console.WriteLine("3. Add a nickel.");
            Console.WriteLine("4. Add a dime.");
            Console.WriteLine("5. Add a quarter.");
            Console.WriteLine("6. Take all money out of bank.");
            Console.WriteLine("Enter 0 to quit.");
 
            int i = Convert.ToInt32(Console.ReadLine());

 
                if (i == 1)
                {
                    Console.WriteLine("Total: " + pb.getTotal());
                }
                else if (i == 2)
                {
                    pb.addPenny();
                    Console.WriteLine("Penny added!");
                }
 

                else if (i == 3)
                {
                    pb.addNickel();
                    Console.WriteLine("Nickel added!");
                }
 
                else if (i == 4)
                {
                    pb.addDime();
                    Console.WriteLine("Dime added!");
                }
 
                else if (i == 5)
                {
                    pb.addQuarter();
                    Console.WriteLine("Quarter added!");
                }
                else if (i == 6)
                {
                    pb.removeMoneyFromBank();
                    Console.WriteLine("All money is removed!");
                }
                else if (i == 0)
                {
                    Console.ReadKey();
                }
                else
                {
                    Console.WriteLine("Invalid Code");
                }

 
            }
 
        }
    }



问候,
爱德华(Eduard)



Regards,
Eduard


我认为您的代码在这里是如此混乱,以至于您需要从规范开始,然后从中开始.

您需要:

1.启动应用程序的方法

2.菜单项的初步介绍

3.建立一个条件,该条件将继续应用程序内的循环,并为该条件创建一个布尔变量:

//最初必须为true,这样while循环才能开始
私人布尔ContinueWhile = true;

一个.同样,您想定义哪些条目将终止应用程序中的循环:

int TerminateKeyValue = 0;

4.然后进入一个while循环,该循环从控制台读取一个键开始:

一个.检查该键以查看其是否为有效的条目:如果不使用''继续,则重新启动while循环.

b.如果密钥有效,并且其值等于您希望终止应用程序的密钥,则设置''continueWhile = false;并使用''continue导致while循环再执行一次并退出.

C.在触发PiggyBank方法的可接受值范围内给定有效键:做正确的事情以触发正确的方法:我建议使用Switch语句来处理不同的有效情况.

因此您的伪代码可能如下所示:
I think your code is so confused here, that you need to start over with a specification, and work from that.

You need:

1. a way to start the application

2. an initial presentation of the menu items

3. establish a condition that will continue the loop within the application, and create a boolean variable for that condition:

// it has to be ''true, initially, so the while loop can start
private bool continueWhile = true;

a. similarly, you want to define what entry will terminate the loop in the application:

int terminateKeyValue = 0;

4. then you enter a while loop which starts with reading a key from the console:

a. check that key to see if it a valid entry: if not use ''continue to restart the while loop.

b. if the key is valid, and it equals in value the key you wish to terminate the application, then set ''continueWhile = false; and use ''continue to cause the while loop to execute one more time and exit.

c. given a valid key in your acceptable range of values that trigger PiggyBank methods: do the right thing to trigger the right method: I''d suggest a Switch statement to handle the different valid cases.

So your pseudo-code might look like this:
private bool continueWhile = true;

int terminateKeyValue = 0;

while(continueWhile)
{
   // get a console keypress
   // convert to integer
   int i = // console key => integer

   // call a validation keypress function
   // if invalid keypress: loop again
   // optionally you could write a message to the console
   // saying ''invalid entry'' or whatever
   if(iIsNotOkay(i)) continue;

   // check for termination of the app
   // and quit the loop immediately
   if(i == terminateKeyValue)
   {
     continueWhile = false;
     continue;
   }

   // now your code for handling the
   // different cases of valid entries
   // that trigger PiggyBank Methods
   //
   // I''d suggest using a switch/case statement

}


这篇关于需要有关错误的帮助.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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