繁体   English   中英

检查数学函数是否有效

[英]Checking if a math function is valid

我得到了函数{1,2,3,4,5}。 我必须接收有关他想要多少个有序对的用户输入,然后验证该函数是否有效(x坐标的值必须在1和5之间,并且不能重复x坐标)。 我知道如何循环并检查X的值是否介于1和5之间,但是,我无法检查字符串是否有重复元素。 我为x小于1且大于5编写了条件表达式,但我对如何编写检查重复元素的表达式感到困惑。 请问有人帮我吗? 这是我到目前为止:

import java.util.Scanner;

public class Functions
{
    public static void main (String args [])
    {
        Scanner in = new Scanner (System.in);

        int []domain = new int [5];
        int [] range = new int [5];
        int orderedPairs = 0;

        System.out.println ("Enter the number of ordered pairs please: ");
        orderedPairs = in.nextInt();
        while (orderedPairs < 0 || orderedPairs > 5)
        {
            System.out.println ("This input is invalid. Enter a number between 0 and 5 and try   again:");
            orderedPairs = in.nextInt ();
        }

        for (int i = 0; i < orderedPairs; i++)
        {
            System.out.println ("Enter the x-coordinate please: ");
            domain [i][0] = in.nextInt();

            System.out.println ("Enter the y-coordinate please: ");
            range [i][0] = in.nextInt();
        }

        for (int i = 0; i < orderedPairs; i++)
        {
            System.out.println ("f(" + domain [i][0] + "): " + range [i][0]);
        }

        for (int i = 0; i < orderedPairs;i++)
        {
            if (domain [i][0] > 5 || domain [i][0] < 1)
            {
                function = false;
            }

            for (int n = i + 1; n < orderedPairs; n++)
            {
                if (domain[i] == domain [n] && range [n] != range [i])
                {
                    function = false;
                }
            }
        } 
    }
}

编辑:这就是显而易见的一切! :)

最简单的方法是:

1)遍历所有域。

2)对于每个域,检索其值。 然后遍历域,计算其值等于检索值的域的数量。

3)如果每个域的值不是1,则报告错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM