检查数学函数是否有效

发布于 2024-12-27 18:57:07 字数 1744 浏览 0 评论 0原文

我得到了函数 {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;
                }
            }
        } 
    }
}

编辑: 显然,这就是所需要的一切! :)

I am given the function {1,2,3,4,5}. I have to receive user input on how many ordered pairs he wants, then verify if the function is valid (values for the x-coordinate have to be between 1 and 5, and an x-coordinate CAN'T be repeated). I know how to loop for and check if the value of X is between 1 and 5, however, I am having trouble checking the string for repeating elements. I wrote the conditional expression for x less than 1 and bigger than 5, but I am stumped on how to write an expression that checks for repeating elements. Can somebody help me with that please? This is what I have so far:

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;
                }
            }
        } 
    }
}

Edit :
That is all it took, apparently! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤带着心事 2025-01-03 18:57:07

最简单的方法是:

1)循环所有域。

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

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

The simplest way to do it is this:

1) Loop over all the domains.

2) For each domain, retrieve its value. Then loop over the domains counting the number of domains whose value equals the retrieved value.

3) If that value is anything other than 1 for each domain, report an error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文