在Java方法的参数中使用printf

在Java方法的参数中使用printf,java,printf,Java,Printf,我有一个名为CustomerTypeApp的主类。它有一个类变量private static float itemPrice=0.0f。主方法中包含以下代码: String promptType = (" To determine a discount for " + nf.format((float) itemPrice) + ", type in the... ...Select => "); char customer = Validation.getChar(promptTyp

我有一个名为CustomerTypeApp的主类。它有一个类变量
private static float itemPrice=0.0f
。主方法中包含以下代码:

String promptType = ("  To determine a discount for " + nf.format((float) itemPrice) + ", type in the... ...Select =>  ");
char customer = Validation.getChar(promptType);
我还有一个名为Validation的类,它有一个getChar方法。代码如下:

public static char getChar(String prompt){
    String input;
    char choice;
    System.out.printf("  " + prompt);
    input = sc.nextLine();
    choice = input.charAt(0);
    return choice;
}
String promptType = ("  To determine a discount for $%.2s, type in the... ...Select => ");
("  To determine...$%.2s...Select => ", itemPrice);
System.out.printf("  " + prompt, itemPrice);
在CustomerTypeApp类中,nf.format显然是指NumberFormat。然而,教授说我们不能使用NumberFormat,而必须使用printf。getChar方法确实使用printf语句。但是,我在传递printf(变量?)的值时遇到问题。我的意思是: 我对CustomerTypeApp的代码进行了如下编辑:

public static char getChar(String prompt){
    String input;
    char choice;
    System.out.printf("  " + prompt);
    input = sc.nextLine();
    choice = input.charAt(0);
    return choice;
}
String promptType = ("  To determine a discount for $%.2s, type in the... ...Select => ");
("  To determine...$%.2s...Select => ", itemPrice);
System.out.printf("  " + prompt, itemPrice);
问题是我必须在$%.2s所在的位置传入itemPrice。我试着把它钉在PrompType的末尾,如下所示:

public static char getChar(String prompt){
    String input;
    char choice;
    System.out.printf("  " + prompt);
    input = sc.nextLine();
    choice = input.charAt(0);
    return choice;
}
String promptType = ("  To determine a discount for $%.2s, type in the... ...Select => ");
("  To determine...$%.2s...Select => ", itemPrice);
System.out.printf("  " + prompt, itemPrice);
然而,我得到一个错误,声明“;”预期的和“;”预期的。我还尝试将其添加到getChar方法中,如下所示:

public static char getChar(String prompt){
    String input;
    char choice;
    System.out.printf("  " + prompt);
    input = sc.nextLine();
    choice = input.charAt(0);
    return choice;
}
String promptType = ("  To determine a discount for $%.2s, type in the... ...Select => ");
("  To determine...$%.2s...Select => ", itemPrice);
System.out.printf("  " + prompt, itemPrice);
然而,由于这个原因,我得到一个错误,声明找不到符号

谁能告诉我我做错了什么?谢谢

使用
String.format(“要确定…%f.2s…选择=>”,itemPrice)

使用
String.format(“要确定…%f.2s…选择=>”,itemPrice)

String promptype=("
教授在原始代码中包含了括号。我想他只是把字符串值放在一起。这可能没有必要,但我保留了它,因为他已经把它放上了。在任何情况下,如果我试图传递$%.2s的值,我会认为我需要paren来处理整件事。找不到符号==variab在哪里le
prompt
声明(它是变量吗?
String promptype=("
教授在原始代码中包含了括号。我想他只是把字符串值放在一起。这可能没有必要,但我保留了它,因为他已经把它放上了。在任何情况下,如果我试图传递$%.2s的值,我会认为我需要paren来处理整件事。找不到符号==variab在哪里le
提示符
声明(它是一个变量吗?)给出了
“确定…0.000000.2s…Select=>”
。你的意思是
确定…%.2f…Select=>
?我在测试代码并修复它时发现了这一点。感谢你提到它。这就给出了
“确定…0.0000002s…Select=>”
。你是说
来确定…%.2f…Select=>
?我在测试代码并修复它时发现了这一点。谢谢你的提醒。