6

当我运行 Javac 时,它告诉我在

while(responseChar == "y")

不确定要更改什么来修复此错误

import java.util.Scanner;
public class UseOrder
{
public static void main(String[] args)
{

    int answer, num, q;
    String name, response;
    double p;
    char responseChar;
    Scanner keyboard = new Scanner(System.in);

    Order order1 = new Order();
    order1.setCustomerName();
    order1.setCustomerNum();
    order1.setQuantity();
    order1.setUnitPrice();
    order1.computePrice();
    order1.displayInfo();

    keyboard.nextLine(); 
    System.out.println("Do you need the shipping and handling service?");
    System.out.println("Enter y or n :");
    response = keyboard.nextLine();
    responseChar = response.charAt(0);
    while(responseChar == "y")
    {
        ShippedOrder order2 = new ShippedOrder();
        order2.getCustomerName();
        order2.getCustomerNum();
        order2.getQuantity();
        order2.getUnitPrice();
        order2.computePrice();
        order2.displayInfo();
    }
}
}
4

3 回答 3

20

要定义字符文字,请使用单引号:'. 双引号定义字符串文字。

while(responseChar == 'y')
于 2012-08-18T14:43:09.300 回答
3

或者,您可以使用大 CCharacter类将您的转换charString

String converted = Character.toString(responseChar); 
while(converted.equalsIgnoreCase("y"))
{
  // ...
}

但这个版本比 Jeffrey 建议的字面比较详细得多

于 2012-08-18T14:46:01.340 回答
1

而不是"y"这样做'y'""表示字符串。

于 2012-08-18T14:44:09.800 回答