Java无与伦比的数据类型char和String

时间:2022-04-10 16:36:24

When I run the Javac, it tells me that i have an incomparable data types char and String in the

当我运行Javac时,它告诉我,我有一个无与伦比的数据类型char和String

while(responseChar == "y")

not sure what to change to fix this error

不知道要修改什么来修复此错误

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();
    }
}
}

3 个解决方案

#1


16  

To define a character literal, use a single quote: '. Double quotes define string literals.

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

while(responseChar == 'y')

#2


3  

Optionally you could use the big C Character class to convert your char to a String:

您可以选择使用big C Character类将char转换为String:

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

But this version is much more verbose than the literal comparison suggested by Jeffrey

但是这个版本比Jeffrey提出的字面比较要冗长得多

#3


1  

Instead of "y" do this 'y'. "" Represents string.

而不是“y”做这个'y'。 “”代表字符串。

#1


16  

To define a character literal, use a single quote: '. Double quotes define string literals.

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

while(responseChar == 'y')

#2


3  

Optionally you could use the big C Character class to convert your char to a String:

您可以选择使用big C Character类将char转换为String:

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

But this version is much more verbose than the literal comparison suggested by Jeffrey

但是这个版本比Jeffrey提出的字面比较要冗长得多

#3


1  

Instead of "y" do this 'y'. "" Represents string.

而不是“y”做这个'y'。 “”代表字符串。