学校作业,所以这段代码没有意义。每当我尝试使用 char 时,我似乎总是收到此错误
LetsGoShop.java:14: error: cannot find symbol
item = input.nextChar();
^
symbol: method nextChar()
location: variable input of type Scanner
1 error
下面是实际代码:
import java.util.Scanner;
public class LetsGoShop {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
char item ;
int price;
int quantity;
System.out.println(" Enter the name of the item : ");
item = input.nextChar();
System.out.println(" Enter the price of said item : ");
price = input.nextInt();
System.out.println(" Enter how much of said item you want to buy : ");
quantity = input.nextInt();
double total = price * quantity ;
item = Character.toUpperCase(item);
System.out.println(" You owe " +total+ " for " +quantity + item);
}
}
我刚刚开始编码,所以如果答案很明显,我就不会猜到。
最佳答案
自 nextChar
does not exist ,我建议您考虑尝试以下操作:
char item;
item = input.next().charAt(0);
编辑:据我了解,您想要这个:
String item = input.next();
String newItem = input.substring(0, 1).toUpperCase() + input.substring(1);
这将从用户处获取一个String
(项目名称),并将第一个字母设为大写。
如果您想确保所有其他字母均为小写,请使用:
String newItem = input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
编辑#2:将整个单词大写:
String item = input.next().toUpperCase();
关于java - 找不到 char 的符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35274977/