Cannot find Symbol for char
本问题已经有最佳答案,请猛点这里访问。
学校作业,所以这个代码没有意义。每当我尝试使用一个字符时,我总是会得到这个错误。
1 2 3 4 5 6 | LetsGoShop.java:14: error: cannot find symbol item = input.nextChar(); ^ symbol: method nextChar() location: variable input of type Scanner 1 error |
实际代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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); } } |
我刚开始编码,所以如果答案很明显,我就猜不到了。
由于
1 2 | char item; item = input.next().charAt(0); |
编辑:据我所知,你想要这个:
1 2 |
这将从用户那里得到一个
如果要确保所有其他字母都是小写,请使用:
1 |
编辑2:将整个单词大写:
1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | import java.io.*; public class Citaj { private InputStream ul; // File that you are reading from private char c; // Last char that you read private boolean eof; // end of file public Citaj (String ime) throws FileNotFoundException // Open { ul = new FileInputStream (ime); } // file. public boolean eofF () { return eof; } // is it end of file? public char getChF () { // get a next char. try { int i = ul.read (); return c = (eof = i == -1) ? ' ' : (char)i; } catch (Exception g) { eof = true; return c = ' '; } } public char CharF () { // reading one non-white char. while (Character.isWhitespace (c = getChF ())); return !eof ? c : ' '; } public String StringF () { // read one string. String s =""; while ( Character.isWhitespace (c = getChF ()) && !eof); if (eof) return""; s += c; while (!Character.isWhitespace (c = getChF ()) && !eof) s += c; eof = false; return s; } public String LineF () { // read one line String s=""; while ((c = getChF ()) != ' ' && !eof) if (c != ' ') s += c; if (s.length () != 0) eof = false; return s; } public void getNLF () { while (c!=' ' && !eof) c = getChF (); c = '\0'; } public byte ByteF () // read one byte { String s = StringF (); return !eof ? Byte.parseByte (s) : 0; } public short ShortF () // read one short { String s = StringF (); return !eof ? Short.parseShort (s) : 0; } public int IntF () // read one int { String s = StringF (); return !eof ? Integer.parseInt (s) : 0; } public long LongF () // read one long { String s = StringF (); return !eof ? Long.parseLong (s) : 0; } public float FloatF () // read one float { String s = StringF (); return !eof ? Float.parseFloat (s) : 0; } public double DoubleF () // read one double { String s = StringF (); return !eof ? Double.parseDouble (s) : 0; } // public boolean BooleanF() // read one boolean // { String s = StringF (); return !eof ? Boolean.parseBoolean (s) : false; } // Support for reading from console: private Citaj () { ul = System.in; } // private constructor private static Citaj gl = new Citaj (); public static boolean eof () { return gl.eofF (); } // Variations: public static char getCh () { return gl.getChF (); } // public static char Char () { return gl.CharF (); } // public static String String () { return gl.StringF (); } // public static String Line () { return gl.LineF (); } // public static void getNL () { gl.getNLF (); } // public static byte Byte () { return gl.ByteF (); } public static short Short () { return gl.ShortF (); } public static int Int () { return gl.IntF (); } public static long Long () { return gl.LongF (); } public static float Float () { return gl.FloatF (); } public static double Double () { return gl.DoubleF (); } // public static boolean Boolean() { return gl.BooleanF(); } } |
所以一般来说,只需导入给定的类,并像这样使用它:citaj.char()。你也可以根据自己的喜好重新命名citaj类:)
用
你必须用
也可以在数量和项目之间输入
这里的问题是类
要解决这个问题,您可以从扫描仪中获取一个字符串,并检查长度是否等于1(这意味着它只包含一个字符),然后获取这个字符。如果您不想在输入有多个字符的情况下显示错误,可以避免长度检查。
例子:
1 2 3 4 5 6 7 8 | Scanner s = new Scanner(System.in); String inputString = s.next(); if(inputString.length() > 1){ throw new IllegalArgumentException("Only one character can be inputed!"); //Handle however you want. The exeption is thron as an example. } char inputChar = inputString.charAt(0); //Continue your code :- ) |
祝你好运。