C# ASP.NET中来自控制台应用程序的贷款计算器

C# ASP.NET中来自控制台应用程序的贷款计算器,c#,asp.net,.net,visual-studio-2010,visual-studio,C#,Asp.net,.net,Visual Studio 2010,Visual Studio,我刚刚在witch中完成了一个控制台应用程序的工作,我创建了一个贷款计算器。我的代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LoanCalculator { public class LoanCalculator { public static

我刚刚在witch中完成了一个控制台应用程序的工作,我创建了一个贷款计算器。我的代码如下:

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

namespace LoanCalculator
{
    public class LoanCalculator
    {

        public static void Main(string[] args)
        {
            // declare variables
            double principle = 0; 
            double years = 0;
            double interest = 0;
            string principleInput, yearsInput, interestInput;


            // User input for Principle amount in dollars
            Console.Write("Enter the loan amount, in dollars(0000.00): ");
            principleInput = Console.ReadLine();
            principle = double.Parse(principleInput);
            //Prompt the user to reenter any illegal input
            if (principle < 0)
            {
                Console.WriteLine("The value for the mortgage cannot be a negative value");
                principle = 0;
            }


            // User input for number of years
            Console.Write("Enter the number of years: ");
            yearsInput = Console.ReadLine();
            years = double.Parse(yearsInput);
            //Prompt the user to reenter any illegal input
            if (years < 0)
            {
                Console.WriteLine("Years cannot be a negative value");
                years = 0;
            }

            // User input for interest rate
            Console.Write("Enter the interest rate(%): ");
            interestInput = Console.ReadLine();
            interest = double.Parse(interestInput);
            //Prompt the user to reenter any illegal input
            if (interest < 0)
            {
                Console.WriteLine("The value for the interest rate cannot be a negative value");
                interest = 0;
            }

            //Calculate the monthly payment
            //ADD IN THE .Net function call Math.pow(x, y) to compute xy (x raised to the y power). 
            double loanM = (interest / 1200.0);
            double numberMonths = years * 12;
            double negNumberMonths = 0 - numberMonths;
            double monthlyPayment = principle * loanM / (1 - System.Math.Pow((1 + loanM), negNumberMonths));

            //double totalPayment = monthlyPayment;


            //Output the result of the monthly payment
            Console.WriteLine(String.Format("The amount of the monthly payment is: {0}{1:0.00}", "$", monthlyPayment));
            Console.WriteLine();
            Console.WriteLine("Press the Enter key to end. . .");
            Console.Read();

        }
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
     <title>Monthly Mortgage Calculator</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Monthly Payment Loan Calculator</h1>
    </div>

     <asp:Label ID="Label1" runat="server" Text="Please enter the principle amount">     </asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtPrinciple" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label2" runat="server" 
        Text="Please enter the loan duration in years"></asp:Label>
    <br />
    <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True">
        <asp:ListItem>15 Years</asp:ListItem>
        <asp:ListItem>30 Years</asp:ListItem>
        <asp:ListItem>Other</asp:ListItem>
    </asp:RadioButtonList>
    <br />
  nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n    bsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtYears" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label3" runat="server" Text="Please select the interest rate">    </asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
        <asp:ListItem>4</asp:ListItem>
        <asp:ListItem>5</asp:ListItem>
        <asp:ListItem>6</asp:ListItem>
        <asp:ListItem>7</asp:ListItem>
        <asp:ListItem>8</asp:ListItem>
        <asp:ListItem>9</asp:ListItem>
        <asp:ListItem>10</asp:ListItem>
    </asp:DropDownList>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Monthly Payment" />

    </form>
</body>
</html>
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间借用计算器
{
公共类贷款计算器
{
公共静态void Main(字符串[]args)
{
//声明变量
双重原理=0;
两年=0;
双倍利息=0;
字符串主输入、年输入、利息输入;
//用户输入的原则金额(美元)
控制台。填写(“输入贷款金额,单位为美元(0000.00):”;
principleInput=Console.ReadLine();
principle=double.Parse(principleInput);
//提示用户重新输入任何非法输入
如果(原则<0)
{
Console.WriteLine(“抵押的价值不能是负值”);
原理=0;
}
//用户输入的年数
Console.Write(“输入年份:”);
yearsInput=Console.ReadLine();
年份=double.Parse(yearsInput);
//提示用户重新输入任何非法输入
如果(年<0)
{
Console.WriteLine(“年不能是负值”);
年=0;
}
//用户输入利率
控制台。写入(“输入利率(%):”;
interestInput=Console.ReadLine();
利息=double.Parse(利息投入);
//提示用户重新输入任何非法输入
如果(利息<0)
{
Console.WriteLine(“利率值不能为负值”);
利息=0;
}
//计算每月的付款
//添加.Net函数调用Math.pow(x,y)来计算xy(x提升为y的幂)。
双倍贷款额=(利息/1200.0);
双倍数月=年*12;
双负数个数=0-数个数;
双月付款=原则*loanM/(1-系统数学功率((1+loanM),负数个月));
//双倍总付款=每月付款;
//输出每月付款的结果
Console.WriteLine(String.Format(“每月付款金额为:{0}{1:0.00}”,“$”,monthlyPayment));
Console.WriteLine();
Console.WriteLine(“按Enter键结束…”);
Console.Read();
}
}
}
以上一切都按计划进行。我现在正试图使用Visual Studio将其转换为ASP.NET web应用程序,我被卡住了。我当前的UI设置了3个标签,分别用于原则输入(带文本框)、贷款期限(带单选按钮列表)和利率(带下拉列表)

我遇到的问题是,我试图在我的贷款期限(15、30或其他)内获得一个单选按钮列表作为我的选择。如果用户选择Other,我将尝试实现一个文本框,让他们在几年内输入一个值。在用户选择其预期的持续时间后,我希望我的利率是一个下拉列表项,带有1%-10%的选项

我还有一个名为“计算”的按钮,它根据用户输入计算解决方案。如果有人能引导我找到正确的方向来解决这个问题。我对使用ASP.NET是一个全新的概念,对于如何成功地将控制台应用程序转换为ASP.NET项目感到困惑。谢谢你的帮助

我的页面如下所示:

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

namespace LoanCalculator
{
    public class LoanCalculator
    {

        public static void Main(string[] args)
        {
            // declare variables
            double principle = 0; 
            double years = 0;
            double interest = 0;
            string principleInput, yearsInput, interestInput;


            // User input for Principle amount in dollars
            Console.Write("Enter the loan amount, in dollars(0000.00): ");
            principleInput = Console.ReadLine();
            principle = double.Parse(principleInput);
            //Prompt the user to reenter any illegal input
            if (principle < 0)
            {
                Console.WriteLine("The value for the mortgage cannot be a negative value");
                principle = 0;
            }


            // User input for number of years
            Console.Write("Enter the number of years: ");
            yearsInput = Console.ReadLine();
            years = double.Parse(yearsInput);
            //Prompt the user to reenter any illegal input
            if (years < 0)
            {
                Console.WriteLine("Years cannot be a negative value");
                years = 0;
            }

            // User input for interest rate
            Console.Write("Enter the interest rate(%): ");
            interestInput = Console.ReadLine();
            interest = double.Parse(interestInput);
            //Prompt the user to reenter any illegal input
            if (interest < 0)
            {
                Console.WriteLine("The value for the interest rate cannot be a negative value");
                interest = 0;
            }

            //Calculate the monthly payment
            //ADD IN THE .Net function call Math.pow(x, y) to compute xy (x raised to the y power). 
            double loanM = (interest / 1200.0);
            double numberMonths = years * 12;
            double negNumberMonths = 0 - numberMonths;
            double monthlyPayment = principle * loanM / (1 - System.Math.Pow((1 + loanM), negNumberMonths));

            //double totalPayment = monthlyPayment;


            //Output the result of the monthly payment
            Console.WriteLine(String.Format("The amount of the monthly payment is: {0}{1:0.00}", "$", monthlyPayment));
            Console.WriteLine();
            Console.WriteLine("Press the Enter key to end. . .");
            Console.Read();

        }
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
     <title>Monthly Mortgage Calculator</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Monthly Payment Loan Calculator</h1>
    </div>

     <asp:Label ID="Label1" runat="server" Text="Please enter the principle amount">     </asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtPrinciple" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label2" runat="server" 
        Text="Please enter the loan duration in years"></asp:Label>
    <br />
    <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True">
        <asp:ListItem>15 Years</asp:ListItem>
        <asp:ListItem>30 Years</asp:ListItem>
        <asp:ListItem>Other</asp:ListItem>
    </asp:RadioButtonList>
    <br />
  nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n    bsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtYears" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label3" runat="server" Text="Please select the interest rate">    </asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
        <asp:ListItem>4</asp:ListItem>
        <asp:ListItem>5</asp:ListItem>
        <asp:ListItem>6</asp:ListItem>
        <asp:ListItem>7</asp:ListItem>
        <asp:ListItem>8</asp:ListItem>
        <asp:ListItem>9</asp:ListItem>
        <asp:ListItem>10</asp:ListItem>
    </asp:DropDownList>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Monthly Payment" />

    </form>
</body>
</html>

每月按揭计算器
每月还款贷款计算器



15年 30年 其他
nbsp&n bsp;

1. 2. 3. 4. 5. 6. 7. 8. 9 10


以下是转换项目的一些一般步骤。如果您需要代码,请告诉我是哪一位

1) 创建空的Asp.net应用程序(不是网页)

2) 向标记中添加一些asp.net标记(而不是代码隐藏) 看起来像

          <asp:TextBox ID="tbYears" runat="server" />
          <asp:RadioButtonList ID="rblDuration" runat="server" >
                 <asp:ListItem Text="15" Value="15" />
                  ...
          </asp:RadioButtonList>
          <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
4) 写下你的按钮点击事件

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
          double years = double.parse(tbYears.text); // This will throw an exception if a user doesn't input a number
          var duration = rblDuration.SelectedValue // Needs to be cast too as it comes back as a string
    } 
那应该让你开始

编辑:

另一个提示:改变

           <asp:ListItem>15 Years</asp:ListItem>
15年
进入



使解析值更容易一般来说,您将能够使用相同的逻辑,只是我将把它移到一个类中

1) 在有控制台输入的地方,可以使用文本框。 2) 如果在WriteLine()上有结果,则可能需要使用标签来显示结果

ASP.NET页面将有一个.aspx文件,用于保存标记(HTML),还有一个.cs文件,用于存放代码。页面元素可以是调用codebehind中的方法的事件

例如,在“设计”视图中选择“提交”按钮,然后在“属性”窗格中查找“事件”按钮。您应该能够为按钮单击添加新事件。这将把点击按钮映射到codebehind中的方法:

protected void importBtn_Click(object sender, EventArgs e)
{
    Textbox1.Text = "Editing the Textbox contents";
}
您可以访问控件的属性,如图所示


祝你好运,希望这有帮助。

发布一些asp标记代码,页面看起来怎么样like@Mikey鼠标代码已经更新看起来不错。您需要将OnClick方法添加到按钮标记中,或者在代码中使用“+=”在我的解决方案中有一个示例,请在其上放置一个断点以查看它是否正确works@DaBulls33看起来不错,在标记代码中,尝试命名控件,以便查看后面代码的人可以看到发生了什么。因此,与其说它是“DropDownList1”,不如说它是“ddlInterestRate”(只是一个提示)@Mickey我只是个骗子