🔒 Closed (C#)Pa check po ng code ko sa c# thanks..

Status
Not open for further replies.

Nero Swallowtail

Honorary Poster
Di ko sure kung tama tong nagawa ko e, pa check nga po ako kung tama , feel ko tama ko yung number 2 sa number 1 lang ako nalilito, salamat po sa sasagot.
Eto yung instruction:

IMG_20210510_181802.JPG

Eto yung code:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuizMethods
{
    class QuizMethods
    {
        static void Main(string[] args)
        {
            QuizMethods obj = new QuizMethods();

            Console.WriteLine("Number 1.");
           
            Console.Write("Enter the first number: ");
            int num1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the second number: ");
            int num2 = Convert.ToInt32(Console.ReadLine());

            int firstNum = obj.getNum(num1, num2);

            Console.WriteLine("Total of the both number: " + firstNum);

            Console.Write("Enter the first number: ");
            int num3 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the second number: ");
            int num4 = Convert.ToInt32(Console.ReadLine());

            int secondNum = obj.getNum(num3, num4);
            Console.WriteLine("Total of the both number: " + secondNum);

            Console.Write("Enter the first number: ");
            int num5 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the second number: ");
            int num6 = Convert.ToInt32(Console.ReadLine());

            int thirdNum = obj.getNum(num5, num6);

            Console.WriteLine("Total of the both number: " + thirdNum);

            Employee newEmployee = new Employee();

            Console.WriteLine("Number 2.");

            Console.Write("Enter your employee ID: ");
            newEmployee.employeeID = Convert.ToInt64(Console.ReadLine());

            Console.Write("Enter your first name: ");
            newEmployee.firstName = Convert.ToString(Console.ReadLine());

            Console.Write("Enter your last name: ");
            newEmployee.lastName = Convert.ToString(Console.ReadLine());

            Console.Write("Enter your position: ");
            newEmployee.postition = Convert.ToString(Console.ReadLine());

            newEmployee.displayInfo();

            Console.ReadKey();
        }
        private int getNum(int firstNum, int secondNum)
        {
            int totalNum = firstNum + secondNum;
            return totalNum;
        }
        private int getNum(double firstNum, double secondNum)
        {
            double total = firstNum + secondNum;
            int newTotal = (int)total;
            return newTotal;
        }
        private int getNum(long firstNum, long secondNum)
        {

            long newTotal = firstNum + secondNum;
            int totalNew = (int)newTotal;
            return totalNew;
        }
        private struct Employee
        {
            public long employeeID;
            public String firstName, lastName, postition;

            private void setInfo(long employeeID, String firstName, String lastName, String postition)
            {
                this.employeeID = employeeID;
                this.firstName = firstName;
                this.lastName = lastName;
                this.postition = postition;
            }
            public void displayInfo()
            {
                Console.WriteLine("Employee ID: " + employeeID);
                Console.WriteLine("Name: " + firstName + " " + lastName);
                Console.WriteLine("Position: " + postition);
            }
        }
    }
}
Hide react ko lang para pam pa elite, charot para di magulo hehe
 
Ganito ba need mo sa number 1? yung magagamit ang 3 types (int, double, long) pero ang return is int?

NOTE: C# 5.0 gamit ko

C#:
  using System;

    Calculator calculator = new();

    Console.WriteLine("Number 1.");

    Console.Write("Enter the first number: ");
    int num1 = Convert.ToInt32(Console.ReadLine());

    Console.Write("Enter the second number: ");
    int num2 = Convert.ToInt32(Console.ReadLine());

    Console.WriteLine($"Total: {calculator.Compute(num1, num2)}\n");

    Console.Write("Enter the first number: ");
    double num3 = Convert.ToDouble(Console.ReadLine());

    Console.Write("Enter the second number: ");
    double num4 = Convert.ToDouble(Console.ReadLine());

    Console.WriteLine($"Total: {calculator.Compute(num3, num4)}\n");

    Console.Write("Enter the first number: ");
    long num5 = Convert.ToInt64(Console.ReadLine());

    Console.Write("Enter the second number: ");
    long num6 = Convert.ToInt64(Console.ReadLine());

    Console.WriteLine($"Total: {calculator.Compute(num5, num6)}\n");

    public class Calculator{
        public int Compute(int n1, int n2) => n2 + n2;
        public int Compute(double n1, double n2) => (int)(n1 + n2);
        public int Compute(long n1, long n2) => (int)(n1 + n2);
    }

Yung sa code mo sa methods like this:
Code:
private int getNum(double firstNum, double secondNum)
{
    double total = firstNum + secondNum;
    int newTotal = (int)total;
    return newTotal;
}

Di mo na need ideclare as double yung firstnum + secondNum.
since yung params nila ay double na. Pwede mo nalang direct convert and retrun to int yung total
 
Status
Not open for further replies.

About this Thread

  • 2
    Replies
  • 460
    Views
  • 2
    Participants
Last reply from:
zackmark29

Trending Topics

Online now

Members online
543
Guests online
3,580
Total visitors
4,123

Forum statistics

Threads
2,332,365
Posts
29,264,296
Members
1,146,240
Latest member
Qokka
Back
Top