My apologies if an answer to a question like this already exists, I am an extremely novice coder, and I may have failed to locate it.
I'm working on a project to give the distance, perimeter, and area given the x and y values of three points. Said points are supposed to be rounded to the thousandths place, using the printf command.
It is supposed to look like this:
Triangle ={(8.500 ,1.250) ,(2.500 ,1.250) ,(2.500 ,9.250)}
Yet, I can only manage to get:
Triangle = { ( 8.5001.2502.5001.2502.5009.250
Here is my code:
public class TriangleCalculator
{
public static void main(String[] args)
{
System.out.println("Enter the x- and y- coordinates of the first point");
Scanner keyBoard = new Scanner (System.in);
double pointOneX = keyBoard.nextDouble();
double pointOneY = keyBoard.nextDouble();
System.out.println("Enter the x- and y- coordinates of the second point");
double pointTwoX = keyBoard.nextDouble();
double pointTwoY = keyBoard.nextDouble();
System.out.println("Enter the x- and y- coordinates of the third point");
double pointThreeX = keyBoard.nextDouble();
double pointThreeY = keyBoard.nextDouble();
System.out.printf("Triangle = { ( %.3f", pointOneX);
System.out.printf("%.3f", pointOneY);
System.out.printf("%.3f", pointTwoX);
System.out.printf("%.3f", pointTwoY);
System.out.printf("%.3f", pointThreeX);
System.out.printf("%.3f", pointThreeY);
}
}
If anyone could tell or show how to properly write the printf statement or how to align my code properly, I'd appreciate it greatly.
答案 0 :(得分:2)
Your code will not add the parenthesis by itself.
Try:
System.out.printf("%.3f", pointOneY);
System.out.printf("),(");
System.out.printf("%.3f", pointTwoX);
System.out.printf("%.3f", pointTwoY);
System.out.printf("),(");
System.out.printf("%.3f", pointThreeX);
System.out.printf("%.3f", pointThreeY);
System.out.printf(")}");
答案 1 :(得分:0)
I don't see anything about alignment in your question, only rounding and formatting.
You can, and in this case I think you should, handle multiple data items in one /*
Problem 22 Generate Parentheses:
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()"
*/
public List<String> generateParenthesis(int n) {
List<String> list = new ArrayList<String>();
backtrack(list, "", 0, 0, n);
return list;
}
private void backtrack(List<String> list, String str, int open, int close, int max){
if(str.length() == max*2){
list.add(str);
return;
}
if(open < max)
backtrack(list, str + "(", open + 1, close, max);
if(close < open)
backtrack(list, str+")", open, close+1, max);
}
call:
printf
This is for the unusual spacing in your example. For no space before each comma, or space before and after each comma, or space only after each comma, adjust the format string accordingly.
Remember that System.out.printf ("Triangle ={(%.3f ,%.3f) ,(%.3f ,%.3f) ,(%.3f ,%.3f)}",
pointOneX, pointOneY, pointTwoX, pointTwoY, pointThreeX, pointThreeY);
does not terminate the output line. If you want that, in portable code add printf
at the end of the format string, or for Unix or Windows specifically %n
or \n
.