這是我的情況:循環距離,並退出
我需要的代碼在一個循環中運行,一遍又一遍的問同樣的問題(一個或多個)用戶,直到用戶鍵入任意點的「q」來終止/退出循環,從而退出程序。
問題是我試圖使用do-while/while循環,並且只有當條件成立時才執行這些循環。但我需要條件(「q」)爲假,以便它可以繼續循環。如果條件爲真(input.equals(「q」)),那麼它並不是什麼都不是,因爲它不會使用整數/雙精度來使用字符串(「q」)來計算距離。
我已經想出瞭如何獲得距離,代碼效果很好,但有沒有解決方法,我可以讓循環繼續而條件爲假?
順便說一句,我只是勉強學習java以防萬一......
」。
import java.*;
public class Points {
public static void main(String[] args){
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter the first X coordinate: ");
double x1 = input.nextDouble();
System.out.print("Enter the first Y coordinate: ");
double y1 = input.nextDouble();
System.out.print("Enter the second X coordinate: ");
double x2 = input.nextDouble();
System.out.print("Enter the second Y coordinate: ");
double y2 = input.nextDouble();
System.out.println("(" + x1 + ", " + y1 + ")" + " " + "(" + x2 + ", " + y2+ ")");
double getSolution = Math.sqrt(((x2-x1) * (x2-x1)) + ((y2-y1) * (y2-y1)));
System.out.println(getSolution);
}
}'
最後一門功課的問題在用戶實際嘗試:) – 2010-10-18 20:47:55
你知道'!'操作符嗎? – nmichaels 2010-10-18 20:48:02