检查数学函数是否有效

时间:2012-01-17 23:05:58

标签: java java.util.scanner

我获得了{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 个答案:

答案 0 :(得分:2)

最简单的方法是:

1)遍历所有域名。

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

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

相关问题