0

学校の課題なので、このコードは無意味です。char を使用しようとするたびに、常にこのエラーが発生するようです

LetsGoShop.java:14: error: cannot find symbol
                       item = input.nextChar();
                                   ^
  symbol:   method nextChar()
  location: variable input of type Scanner
  1 error

Heres 実際のコード:

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

これを解決するためにできることは、スキャナから文字列を取得し、長さが 1 に等しいかどうか (つまり、文字が 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 itemとに置き換える必要がありString 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 に答える