C# Object-Oriented Programming Course Experiment 1: Experiment Name: C# Language Basics, Program Flow Control

C# Object-Oriented Programming Course Experiment 1: Experiment Name: C# Language Basics, Program Flow Control

insert image description here

Experiment content: C# language foundation, program flow control

insert image description here

1. The purpose of the experiment

(1) Practice C# variable declaration, assignment method, and type conversion method;
(2) Master C# operators and expressions through simple programs; (3
) Preliminary learning of debugging methods;
(4) Familiar with VS.NET environment;
(5) Can Get the correct program running results.
insert image description here

2. Experimental environment

Microsoft Visual Studio 2008

3. Experimental content

(1) Create a console program that randomly generates two integers (or two decimals), calculates their sum, difference, quotient, and product, and displays the results on the console.
1. The experimental procedure is as follows:

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

namespace 实验一
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            Random A1 = new Random();
            double a = A1.Next(100);
            int b = A1.Next(100);
            Console.WriteLine("随机整数两个100以内的整数是:{0},{1}", a, b);
            double a1 = a + b;
            double a2 = a - b;
            double a3 = a / b;
            double a4 = a * b;
            Console.WriteLine("这两个数的和、差、商、积分别为:{0},{1},{2},{3}", a1, a2, a3.ToString("0.00"), a4);
            Console.ReadLine();
        }
    }
}

2. The results of the experiment are as follows:

insert image description here

(2). Design a console application to output the square value of 1~6. The project name is Xt3-1, and the running interface of the program is shown in the figure. (Question 2.1 on page 82 of Chapter 3 of the textbook)

1. The experimental code is as follows:

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

namespace 实验一_2_
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            int i;
            for (i = 1; i < 7; i++)
                Console.WriteLine("{0}的平方值是: {1}", i, i * i);
        }
    }
}

2. The experimental results are as follows:

insert image description here

insert image description here

4. Experimental summary

1. Master the basic knowledge of C# through this experiment summary.
2. Mastered the basic usage of the Random class in this experiment.
3. Master the output statement of C#.
4. In the experiment of running two random numbers, the division of two integers generated for the first time did not produce a two-digit decimal, so after referring to the experiment of the experimental sample, change the output statement to Console.WriteLine("These two numbers The sum, difference, quotient, and product are respectively: {0}, {1}, {2}, {3}", a1, a2, a3.ToString("0.00"), a4); and four variables are defined It is a double type variable. Realized the effect of dividing random numbers to output two decimal places.

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/m0_47419053/article/details/127121411