2013-02-02 19 views
0

我一直在尋找這裏,似乎無法找到可以幫助我修復代碼的東西。帶有十進制座標和多個文本的System.out.printf

我想輸出pring(2.0,2.0)時,用戶輸入2和2.我註釋了我需要幫助的代碼。我想用printf來獲得我的結果。我什麼也沒得到,只有錯誤
這是我的假設,問題在於我必須在文本之間打印(2.0,2.0),但是我無法解決我的錯誤。

import java.util.Scanner; 

public class prtest { 

    // checks to see if radom point entered by user is within rectangle 
    // rectangle is centered at (0,0) and has a width of 10 and height of 5 

    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 

     System.out.print("Enter a point with two coordinates: "); 
     int x = input.nextInt(); 
     int y = input.nextInt(); 

     double hDistance = Math.pow(x * x, 0.5f);// heigth distance 

     double vDistance = Math.pow(y * y, 0.5f);// vertical distance 

     if ((hDistance <= 10/2) && (vDistance <= 5.0/2)) 

      System.out 
        .print("Point (" + x + ", " + y + ") is in the rectangle"); 

     // System.out.printf("Point (%1f", ", " + y + ") is in the rectangle"); 

     else 
      System.out.print("Point (" + x + ", " + y 
        + ") is not in the rectangle"); 

     // System.out.printf("Point (%1f", ", " + y + 
     // ") is not in the rectangle"); 

    }// end main 
}// end prtest 

回答

1

您正在以錯誤的方式使用System.out.printf方法。你的方法應該工作使用這樣的:

System.out.printf("Point (%.1f, %.1f) is in the rectangle", x*1.0, y*1.0); 
//... 
System.out.printf("Point (%.1f, %.1f) is not in the rectangle", x*1.0, y*1.0); 

甚至更​​好,你可以處理點作爲整數

System.out.printf("Point (%d, %d) is in the rectangle", x, y); 
//... 
System.out.printf("Point (%d, %d) is not in the rectangle", x, y); 

更多信息,瞭解使用OG System.out.printfFormat String Syntax

+0

非常感謝。 – Lish

+0

@AlishaMcDonald不​​客氣。 –

0

以下似乎爲我工作。 System.out.printf(「Point,('%1.1f','%1.1f')在矩形中,」(double)x,(double)y);

相關問題