2012-01-17 116 views
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; 
       } 
      } 
     } 
    } 
} 

编辑: 这是所有花了,显然! :)

+0

如果这是功课,请添加“家庭作业”标签。 – 2012-01-17 23:09:56

回答

2

做到这一点最简单的方法是这样的:

1)在所有域环路。

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

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

+0

谢谢,你真棒,我感谢你的帮助!我想通了现在如何做:) – 2012-01-18 00:05:30