0

学校作业,所以这段代码没有意义。每当我尝试使用 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);

    }

}

我刚刚开始编码,所以如果答案很明显,我不会猜到的。

4

4 回答 4

2

由于nextChar 不存在,我会建议您考虑尝试以下方法:

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();
于 2016-02-08T16:58:39.240 回答
0

这里的问题是该类Scanner不包含nextChar()方法。

解决此问题的方法是从 Scanner 获取字符串并检查长度是否等于 1(这意味着它仅包含一个字符),然后获取该字符。如果您不想在输入有多个字符的情况下显示错误,则可以避免长度检查。

例子:

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 :- ) 

祝你好运。

于 2016-02-08T17:01:37.433 回答
0

使用input.next()而不是input.nextChar(). nextChar()扫描仪不存在 该方法。input.next()将返回一个字符串。

您必须替换char itemString item和。item = Character.toUpperCase(item)item = item.toUpperCase()

您也可以+ " " +在数量和项目之间输入以将值与项目分开。

于 2016-02-08T17:02:00.287 回答
0
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 ()) != '\n' && !eof) if (c != '\r') s += c;
    if (s.length () != 0) eof = false;
    return s;
  }

  public void getNLF ()      
    { while (c!='\n' && !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 类:)

于 2016-02-08T17:06:07.317 回答