Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c语言中用户输入的乘法和除法#_C# - Fatal编程技术网

C# c语言中用户输入的乘法和除法#

C# c语言中用户输入的乘法和除法#,c#,C#,这就是问题 代码的上下文: 你洗澡的时间越长,用的水就越多。但到底多少钱?即使你有一个“低流量”淋浴头,你的淋浴也有可能每分钟喷出1.5加仑的水。一加仑是128盎司,所以淋浴每分钟能吐出1.5×128=192盎司的水。一瓶典型的水可能是16盎司。因此,淋浴1分钟就相当于使用192÷16=12瓶水。10分钟的淋浴就像用120瓶水。这些数字有助于透视淋浴用水量! 在~/workspace/pset1目录下一个名为water.c的文件中编写一个程序,提示用户以分钟为单位输入淋浴时间(作为正整数),然后

这就是问题

代码的上下文:

你洗澡的时间越长,用的水就越多。但到底多少钱?即使你有一个“低流量”淋浴头,你的淋浴也有可能每分钟喷出1.5加仑的水。一加仑是128盎司,所以淋浴每分钟能吐出1.5×128=192盎司的水。一瓶典型的水可能是16盎司。因此,淋浴1分钟就相当于使用192÷16=12瓶水。10分钟的淋浴就像用120瓶水。这些数字有助于透视淋浴用水量! 在~/workspace/pset1目录下一个名为water.c的文件中编写一个程序,提示用户以分钟为单位输入淋浴时间(作为正整数),然后根据下面的示例输出打印同等数量的瓶装水(作为整数),其中带下划线的文本表示一些用户的输入

username@ide50:~/workspace/pset1$/水 分钟:10 瓶子:120 为简单起见,您可以假设用户将输入一个正整数,因此这次不需要进行错误检查(或任何循环)!而且不需要担心溢出

我当前的代码:

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

namespace console_water
{
    class water_amount
    {
        static void Main(string[] args)
        {
            /*variable decleration*/
            int multiply, divide;
            int userInput = Console.Read();
            multiply = 192;
            divide = 16;

            /*getting user input*/
            Console.WriteLine("Length of shower in minutes:");

            Console.Read();

            userInput = multiply / divide;

            Console.WriteLine("The amount of water used is:" + userInput);
        }
    }
}
但它不起作用

试试看:

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

namespace console_water
{
    class water_amount
    {
        static void Main(string[] args)
        {
            /*variable decleration*/
            int multiply, divide;
            int userInput;
            multiply = 192;
            divide = 16;

            /*getting user input*/
            Console.WriteLine("Length of shower in minutes:");

            userInput = int.Parse(Console.ReadLine());

            int numBottlesMinute =  multiply / divide;

            Console.WriteLine("The amount of water used is:" + userInput * numBottlesMinute);
        }
    }
}
userInput=int.Parse(Console.ReadLine())这可以读取您的淋浴长度

int numBottlesMinute=乘/除您应该将每公升使用的瓶子数保持在一个变量中(最好是常数…但对于您的示例,可以)


Console.WriteLine(“用水量为:”+userInput*numbottlesmin)
只需打印结果,每分钟瓶数*淋浴长度。

您没有使用
控制台。请正确阅读
<代码>控制台。Read
不会在用户输入控制台时自动存储用户的输入-如果您想使用它,您必须显式捕获并存储它

如果我是你,我会使用
Console.ReadLine()
,它接收用户的所有输入,直到他们按enter键为止

要获得相乘的
值,可以执行以下操作:

Console.WriteLine("Minutes: ");
int input = int.Parse(Console.ReadLine());

请注意,您需要对用户的输入执行
int.Parse
,因为
Console.ReadLine()
返回一个
字符串

,并使用
Console.Read()
错误地返回另一个问题。酷。但是什么不起作用?你得到了什么?你期待什么?说真的,想想那些在这里帮助你的人。我们不读思想之类的东西。给我们尽可能多的必要信息,但不要更多(这么多介绍是怎么回事?),这是非常错误的。在提示用户之前,您会得到一个字符代码,然后您会得到另一个被丢弃的字符代码,然后您只需使用整数除法对两个整数进行除法。如果我使用巴斯会怎么样?删除blablabla,说明您所做的事情并问一个问题。非常感谢您的帮助,我认为它现在正在工作。对于这样的问题,我深表歉意。我只想问最后一个问题,有没有办法让控制台保持打开状态,在末尾添加一个console.ReadKey行吗?正如您所说,只需在末尾编写console.ReadKey()。如果这有助于您解决问题,请将其标记为答案。但这是错误的。如果没有正确地分割数字并给出错误的输入,任何人都可以纠正它吗
public class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine("enter 1 no.");
        float i = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("enter 2 no.");
        float j = Convert.ToInt32(Console.ReadLine());

        if (j % i == 0)
        {
            Console.WriteLine("devide no.is {0}", j);
            Console.ReadLine();
        }
        else
        {
            Console.WriteLine("wrong input");
        }
    }
}