我的百分比值是在c#

时间:2018-09-08 11:32:35

标签: c# asp.net visual-studio class

问题定义:设计一个网页,输入学生的详细信息,例如学生ID,姓名,性别,信息流,标记 任意三门科目中的任何一门,根据大学规定计算总分,百分比和成绩并显示。

对于该程序,我制作了1类Student.cs来初始化所有变量,但是我的显示percen()无法正常工作

学生

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
   public class Student
 {
int id;
string name;
string gender;


string stream;


int[] marks;

public int[] Marks1
{
    get { return marks; }
    set { marks = value; }
}
public Student( int id,string name,string gender,string stream,int[] 
marks)
{
    this.id = id;
    this.name = name;
    this.gender = gender;
    this.stream = stream;
    this.marks = marks;
}
public int Id
{
    get { return id; }
    set { id = value; }
}

public string Name
{
    get { return name; }
    set { name = value; }
}
public int[] Marks
{
    get { return marks; }
    set { marks = value; }
}
public string Gender
{
    get { return gender; }
    set { gender = value; }
}
public string Stream
{
    get { return stream; }
    set { stream = value; }
}
public string toString()
{

    return "name = "+name+"Id - "+id+"gendre = "+gender+"stream = 
"+stream+"marks - "+marks;
}

public int calc_total(int []marks)
{int arr;
      arr = marks[0] + marks[1] + marks[2];

    return arr;


}

 public int persent(int total)
 {
     int p = (total / 300) * 100;
     return p;
   }
 }

StudentDetails.aspx.cs文件:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;

public partial class StudentDetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void TextBox6_TextChanged(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    string gender = "";
    if (RadioButton1.Checked == true)
    {
        gender="Male";
    }
    else{
        gender="Female";
    }

    int id=int.Parse(TextBox1.Text);
    string name = TextBox2.Text; ;
   string stream=TextBox4.Text;

   int []marks=new int [3];
    marks[0]=int.Parse(TextBox5.Text);
    marks[1] = int.Parse(TextBox6.Text);
    marks[2] = int.Parse(TextBox7.Text);
    Student s = new Student(id,name,gender,stream,marks);
    s.Id = id;
    s.Name = name;
    s.Gender = gender;
    s.Marks = marks;

    int total = s.calc_total(marks);
    TextBox10.Text = total.ToString();
    int per = s.persent(total);
    TextBox8.Text = per.ToString();
    TextBox8.Text+= "%";
    }
     }   

1 个答案:

答案 0 :(得分:0)

只需修改代码:

 public int persent(int total)
 {
     int p = (total * 100) / 300;
     return p;
 }