簡體   English   中英

用Java解決帶有兩個變量的難題方程

[英]Solving a difficult equation with two variables in Java

我的作業要求執行以下操作:
Search2:搜索x * x + y * y-12x -10y + 36 = 0的解。在x和y中從0到10進行搜索,在移動到下一個x之前搜索每個y值。 打印找到的前三個解決方案。 (注意-此處標記為中斷很方便!)

我不知道這樣做的邏輯。 我想我必須使用2個以上的循環,但不確定。
這是我到目前為止的內容(僅重復(6,0)):

for (int j = 0; j <= 10; j++) {
    for (int i = 0; i <= 10; i++) {
        while (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0) {
            System.out.println("(" + i + ", " + j + ")");  
        }  
    }  
}  

更新
解決方法如下:

    int t = 0;

    for (int i = 0; i <= 10; i++) {
        if (t == 3) {
            break;
        }
        for (int j = 0; j <= 10; j++) {
            if (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0) {
                System.out.println("(" + i + ", " + j + ")");
                t++;
            }
        }
    }

不錯的嘗試。 因為您是如此親密,所以我將向您展示一個可行的解決方案。 基本上,您需要做三件事:

  1. 更改whileif
  2. 使用變量來計算找到解決方案的次數,因此您可以在三點停止
  3. 添加標簽,以便您可以從內循環中跳出外循環

為了清楚起見,我還建議您使用與問題相同的變量名-即xy

int count = 0;
outerLoop:
for (int y = 0; y <= 10; y++) {
    for (int x = 0; x <= 10; x++) {
        if (x * x + y * y - 12 * x - 10 * y + 36 == 0) {
            System.out.println("(" + x + ", " + y + ")");  
            if (++count == 3)
                break outerLoop;
        }
    }
}

執行后,此代碼將產生:

(6, 0)
(3, 1)
(9, 1)

很抱歉給您喂湯,但是這里的部​​分課程是好的編碼風格和練習。

您正在使用一個額外的while循環,該循環不確定地運行。

while (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0) {
    System.out.println("(" + i + ", " + j + ")");  
}

第一次評估為true-即達到(6,0)時,它將繼續運行,因為ij在內部未進行修改。

您需要將其替換為if

好好看一下內部的while循環。 一旦找到方程的解, ij永遠不會改變,並且公式的值始終為0 ,從而導致無限循環。

為了清楚起見,將ij重命名為x和y也是明智的。 不過,您基本上都處在正確的軌道上。 別忘了,您只需打印前三個解決方案。

由於這是一個非常簡單的概念,因此我不會為您提供任何幫助,但請考慮一下您需要遍歷的內容,它可能會使使用i或j的x和y替代更為容易。 也僅打印(6,0),因為那正是您告訴它與while循環一起執行的操作。

一個提示,您的while循環應該有一個停止其功能的語句,假設x <4,如果大於或等於,則它將在循環外繼續。

public class EquationSolver {

    public static void main(String[] args) {

        int found = 0;
        searchSolutions:
        for (int y = 0; y <= 10; y++) {
            for (int x = 0; x <= 10; x++) {
                if (((x * x) + (y * y) - (12 * x) - (10 * y) + 36) == 0) {
                    System.out.println("(" + x + ", " + y + ")");
                    found ++;
                    if (found == 3) {
                        break searchSolutions;
                    }
                }
            }

        }

    }

}

很長一段時間我上次寫這種作業……只是為了好玩:)

public class Main {

    public static void main(String[] args) {
        int[] range = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        boolean isSolutionFound = Boolean.FALSE;
        int solCounter = 0;

        searchSolutions:
        for (Integer y : range) {
            for (Integer x : range) {
                isSolutionFound = checkForSolution(x, y);
                if (isSolutionFound) {
                    printSolution(x, y);
                    solCounter++;
                }

                if (solCounter == 3) 
                    break searchSolutions;
            }
        }
    }

    private static void printSolution(Integer x, Integer y) {
        System.out.println(x + "," + y); // use some fancy formatting instead
    }

    private static boolean checkForSolution(int x, int y) {
        if (x * x + y * y - 12 * x - 10 * y + 36 == 0)
            return true;
        else
            return false;
    }

}
for (int j = 0; j <= 10; j++)
{
   for (int i = 0; i <= 10; i++)
   {
      if (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0)
      {
         System.out.println("(" + i + ", " + j + ")");
         return;
      }  
   }  
} 

記住return; 部分,因為您已經找到了解決方案,但您仍然會尋找解決方案。 如果您不需要更多解決方案,則應該省略“ return”語句。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM