0

プログラムに少し問題があります。基本的に、窓 (ガラス窓など) の領域を見つけるプログラムを作成する必要があります。数式は、面積 = 高さ * 幅 であり、それが私が入力したものです。ただし、結果は実際には高さ * 幅に答えていません。2 つの数字を同じもの (たとえば、3 * 3) として入力すると、答えは正しい (9) になります。一方、2 つの異なる数値 (たとえば、4 * 5) を入力すると、答えは正しくありません (前の例に対する答えは 25 であると表示されますが、20 のはずです)。なぜこれを行っているのかを理解し、問題を解決するのを手伝ってくれる人はいますか?

PS 学校でのコンピューティングに Microsoft Visual C# 2010 Express を使い始めたばかりです。そのため、かなり単純です。

using System;

namespace FindTheArea
{
    class Program
    {
        static void Main(string[] args)
        {               
            string temporary;
            double Height;
            double Width;
            double Area;

            Console.WriteLine("Find The Area");
            Console.WriteLine("Please enter the height below");
            temporary = Console.ReadLine();
            Console.WriteLine("Please enter the Width below");
            temporary = Console.ReadLine();

            Console.Clear();

            Height = double.Parse(temporary);
            Width = double.Parse(temporary);

            Area = (Height * Width);

            Console.WriteLine("The area is...");
            Console.WriteLine();
            Console.WriteLine(Area+"cm2");

            Console.Read();
        }
    }
}

私は今、どこが間違っていたのかを理解しています。しかし、どうすれば修正できますか?

4

6 に答える 6

6
Height = double.Parse(temporary);
Width = double.Parse(temporary);

ここには明らかな問題があります。の動作は決定論的であるため、とdouble.Parse()に同じ値を割り当てています。HeightWidth

于 2012-09-07T21:24:22.760 に答える
2
Console.WriteLine("Please enter the height below");

temporary = Console.ReadLine();

Console.WriteLine("Please enter the Width below");

temporary = Console.ReadLine();

の最初の値を上書きしていますtemporary。入力した値を 2 つの異なる変数に割り当てる必要があります。

string temporaryHeight;
string temporaryWidth;

Console.WriteLine("Please enter the height below");

temporaryHeight= Console.ReadLine();

Console.WriteLine("Please enter the Width below");

temporaryWidth= Console.ReadLine();
于 2012-09-07T21:24:58.407 に答える
0

一時的な値は同じです。1 つを一時 1 に、もう 1 つを一時 2 に変更します。

于 2012-09-07T21:24:57.860 に答える
0
 Console.WriteLine("Please enter the height below");

        temporary = Console.ReadLine();

        Console.WriteLine("Please enter the Width below");

        temporary = Console.ReadLine();

最初の値を保存していません。

于 2012-09-07T21:25:22.073 に答える
0

システムを使用する;

namespace FindTheArea
{

    class Program
    {

        static void Main(string[] args)
        {
            double Height;
            double Width;
            double Area;

            Console.WriteLine("Find The Area");
            Console.WriteLine("Please enter the height below");
            Height = double.Parse(Console.ReadLine());
            Console.WriteLine("Please enter the Width below");
            Width = double.Parse(Console.ReadLine());

            Console.Clear();

            Area = (Height * Width);

            Console.WriteLine("The area is...");
            Console.WriteLine();
            Console.WriteLine(Area+"cm2");

            Console.ReadKey();
        }

    }

}
于 2012-09-07T21:48:30.623 に答える
0

このコードを見てください:

    Console.WriteLine("Please enter the height below");

    temporary = Console.ReadLine();

    Console.WriteLine("Please enter the Width below");

    temporary = Console.ReadLine();

高さの値を一時的に割り当ててから、幅の値を一時的に割り当てています。これは、上書きしたため、高さの値が失われたことを意味します。

于 2012-09-07T21:27:34.687 に答える