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;
}
}
}
}
}
編輯: 這是所有花了,顯然! :)
如果這是功課,請添加「家庭作業」標籤。 – 2012-01-17 23:09:56