This question already has answers here:
Format a message using MessageFormat.format() in Java
                                
                                    (5个答案)
                                
                        
                                3年前关闭。
            
                    
该程序运行正常,但是当最后一行打印出来时,它将像这样:
java - 使用MessageFormat在Java中进行打印时遇到麻烦-LMLPHP

我尝试这样做,因为它是一个符号:

System.out.println(MessageFormat.format("The rectangle\'s area is {0}", area));


但是结果还是一样。如果我删除符号->“'”,它只会起作用。

而且,我不建议我编写代码。只问我的错误在哪里。谢谢

import java.text.MessageFormat;
import java.util.Scanner;

/*4. Rectangles

        Write an expression that calculates rectangle’s perimeter and area by given width and height.*/
public class Rectangles {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Please enter width:");
        double width = scan.nextDouble();
        System.out.print("Please enter height:");
        double height = scan.nextDouble();
        double area = 2 * width + 2* height;
        double perimeter = width*height;
        System.out.println(MessageFormat.format("Perimeter {0}",perimeter));
        System.out.println(MessageFormat.format("The rectangle's area is {0}", area));

    }
}

最佳答案

每当使用MessageFormat时,您都应该注意
  单引号字符(')满足消息中的特殊目的
  模式。单引号用于表示
  不会格式化的消息模式。单引号本身必须
  使用两个单引号('')进行转义。


Messageformat

System.out.println(MessageFormat.format("The rectangle'' area is {0}", area));

10-06 09:21