可能重复:
如何将数字四舍五入到C#中的两位小数?
我感兴趣的是如何将变量舍入到两位小数.在下面的示例中,奖金通常是带小数点后四位的数字.有没有办法确保薪酬变量总是四舍五入到小数点后两位?
pay = 200 + bonus;
Console.WriteLine(pay);
Run Code Online (Sandbox Code Playgroud) 我似乎无法找到答案 -
我需要使用for循环绘制一个简单的三角形.
*
***
*****
*******
*********
Run Code Online (Sandbox Code Playgroud)
我可以制作一个半三角形,但我不知道如何添加到我当前的循环以形成一个完整的三角形.
*
**
***
****
*****
for (int i=0; i<6; i++)
{
for (int j=0; j<i; j++)
{
System.out.print("*");
}
System.out.println("");
}
Run Code Online (Sandbox Code Playgroud)
谢谢-
我试图在有人试图输入非整数字符的实例中抛出格式异常.
Console.WriteLine("Your age:");
age = Int32.Parse(Console.ReadLine());
Run Code Online (Sandbox Code Playgroud)
我不熟悉C#语言,可以使用帮助为此实例编写try catch块.
非常感谢.
我试图将以下方法的结果写入网页.我认为这是可能的,我只是在弄清楚究竟该怎么做.任何帮助将非常感激.谢谢.
...
System.out.println("Final Register");
for (int i=0; i < ch.length; i++)
{
System.out.println(cd.getDenomLiteralAt(i)+" "+cd.getDenomNumAt(i));
}
}
...
Run Code Online (Sandbox Code Playgroud) 为什么购买是不兼容的类型scan.next()?(我正在尝试获取用户输入以进行购买和招标,然后使用方法来计算更改).
public static void makeChange() //one method of a class
{
double purchase;
double tendered;
Scanner scan = new Scanner (System.in);
System.out.println ("How much was the Purchase?");
purchase = scan.next(); //why would this be an incompatible type?
System.out.println ("Amount Tendered");
tendered = scan.next();
System.out.println("Processing Transaction");
int ch[] = cd.makeChange(purchase, tendered);
.... continued
Run Code Online (Sandbox Code Playgroud) 我熟悉循环,但循环一个过程让我感到困惑:
如果用户输入非整数,我希望再次提示"你的年龄"这个问题,直到用户输入一个整数.
Console.WriteLine("Your age:");
string line = Console.ReadLine();
if (!int.TryParse(line, out age))
{
Console.WriteLine("{0} is not an integer", line);
}
Run Code Online (Sandbox Code Playgroud) 可能的重复:
C# 查找最大数
根据用户输入确定最大和最小数字。
Console.WriteLine ("For the Numbers " + (firstNo) + "," + (secondNo) + "," + (thirdNo));
Console.WriteLine("Largest is ") ;
Console.WriteLine("Smallest is ");
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我很难使用 Math.Max 方法。
我真的被困在这一个 - 我正在尝试创建一个if语句并在文本框中显示分配给变量的值.我一直遇到一个"无法将imp int转换为字符串"的构建错误.谢谢.
int n1;
int userInput = int.Parse(textBox1.Text);
if (userInput == 4)
{
n1 = 60;
}
else if (userInput ==3)
{
n1=40
}
{
textBox2.Text = (n1); //"Cannot implicity convert int to string"
}
Run Code Online (Sandbox Code Playgroud) 我对这段代码有两个问题.我遇到了麻烦,因为提交按钮事件无法识别在文本框事件中计算的变量,并且因为文本框事件未将我的if语句识别为语句.你可以在下面的评论中看到我遇到麻烦的地方.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
int points;
int userInput = int.Parse(textBox1.Text);
if (userInput == 0)
{
points == 5; //I CANNOT COMPILE BECAUSE APPARENTLY I AM NOT ALLOWED TO USE
THIS AS A STATEMENT?
}
if (userInput == …Run Code Online (Sandbox Code Playgroud)