0

我对编码很陌生,似乎无法弄清楚如何让它打印我知道的烘焙名称,因为它试图识别一个单数字符,但我不知道如何让它识别多个。

public static void main(String[] args) {

    int ingredients = 5+4+9+8+201+200+202+2+1+203;
    char bake;

    if (ingredients >= 835) {
        bake = 'Cookies';
    } else if (ingredients >= 80) {
        bake = 'C';
    } else if (ingredients >= 70) {
        bake = 'B';
    } else if (ingredients >= 60) {
        bake = 'D';
    } else {
        bake = 'W';
    }
    System.out.println("You can make " + bake);
}
4

2 回答 2

4

Java你区分charString

  • char是一个字符,在单引号之间,例如'a'
  • String是一对多字符套件,在双引号之间,例如"Cookies"

要持有Cookies你需要一个String

int ingredients = 5+4+9+8+201+200+202+2+1+203;
String bake;

if (ingredients >= 835) {
    bake = "Cookies";
} else if (ingredients >= 80) {
    bake = "C";
} else if (ingredients >= 70) {
    bake = "B";
} else if (ingredients >= 60) {
    bake = "D";
} else {
    bake = "W";
}
System.out.println("You can make " + bake);
于 2020-12-28T18:07:11.800 回答
2

使用String代替char变量,bake例如

String bake = "some value";

数据类型char旨在保存单个字符。要保存多个字符,您需要使用 data type String

于 2020-12-28T18:08:37.350 回答