将负面转为正面 [英] convert negative to positive

查看:73
本文介绍了将负面转为正面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。



我有一个问题,我希望你可以帮我。



我将从这个数组获得所有负数int [] num = {-5,-4,-3,-2,-1,1,2,3,4,5,6};

然后将negitive更改为positive,然后在屏幕上输入已更改为positive的negetive。

Hello.

I have a problem that i hope you can help me with.

I going to get all the negative from this array int[] num = {-5,-4,-3,-2,-1,1,2,3,4,5,6};
and then change the negitive in to positive and then type it out on the screen the negetive that have changed to positive.

推荐答案





要从数组中获取所有负整数,请尝试以下方法:

Hi,

To get all negative integers from the array, try this:
int[] neg = num.Where(x => x < 0).ToArray();



更改中的负数转为正数,试试这个:


To change the negative numbers in neg into positive numbers, try this:

int[] negToPos = neg.Select(x => Math.Abs(x)).ToArray();



如果你只是想改变所有负数在 num 中转为正数,试试这个:


And if you just want to change all negative numbers in num into positive numbers, try this:

int[] pos = num.Select(x => Math.Abs(x)).ToArray();



希望这有帮助。


Hope this helps.


当你得到一个负值时,反转这样的符号:

When you get a negative value, reverse the sign like this:
result = -ValueFromArray;


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

namespace Cs_Scratch_Console
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = new int[] {-1,-2,0,1,2};
            for (int val = 0; val <= numbers.Length - 1; val++)
            {
                if (numbers[val] < 0)
                {
                    Console.WriteLine(numbers[val]);
                    numbers[val] = Math.Abs(numbers[val]);
                }
            }
        }
    }
}


这篇关于将负面转为正面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆