This question already has answers here:
What does a “Cannot find symbol” or “Cannot resolve symbol” error mean?

(13个回答)


3年前关闭。




学校作业,因此此代码毫无意义。每当我尝试使用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();

10-06 02:22