2

このプログラムは問題なく動作しますが、何らかの理由で名前の入力を要求すると、応答の最初の単語のみが保存されます。たとえば、後で出力に使用するときに「Jon Snow」と入力すると、「Jon」のみが表示されます。

import java.util.Scanner;

public class help 
{
public static void main (String[] args)
{
    Scanner input = new Scanner(System.in); //object for user input

    //constant
    final double tax = 0.08; //Sales tax

    //variables
    int choice;                 //menu selection/case switch
    String name;                //customer's name   
    String address;             //customer's address
    String email;               //customer's email address
    String item;                //item purchased
    double itemsPurchased;      //number of items purchased
    double itemPrice;           //price of item purchased
    double total;               //total for purchase
    double grandTotal;          //total + tax
    double taxAmount;           //tax of the purchase

    //Menu
    System.out.println("1. Enter Customer Information");
    System.out.println("2. Display Total Bill");
    System.out.println("3. Quit\n");                            
    System.out.print("Enter 1, 2, or 3 to make your selection: ");
    choice = input.nextInt();

    switch(choice){

    //Customer info

    case 1:

        //inputting info
        System.out.println("\nPlease enter the customers information.");

        //name
        System.out.print("Name: "); 
        name = input.next();
        input.nextLine();                                           

        //address
        System.out.print("Address: ");
        address = input.nextLine();

        //email
        System.out.print("Email Adress: ");
        email = input.next();
        input.nextLine();                                           

        //reading info back to user
        System.out.println("\nThe customer has successfully been added to our list with the following information: ");
        System.out.println("Name: " + name);
        System.out.println("Address: " + address);
        System.out.println("Emal: " + email);
        break;                      

    //Customer receipt  

    case 2:

        //name
        System.out.println("");                                     
        System.out.print("Enter customer's name: ");
        name = input.next();
        input.nextLine();                                           

        //name of item purchased
        System.out.print("Enter item purchased: ");
        item = input.next();
        input.nextLine();                                           

        //number of items purchased
        System.out.print("Number of items purchased: ");
        itemsPurchased = input.nextDouble();
        input.nextLine();                                           

        //price of item
        System.out.print("Price of item: ");
        itemPrice = input.nextDouble();
        input.nextLine();                                           

        //defining taxAmount, total, and grandTotal
        total = (itemPrice * itemsPurchased);
        taxAmount = (total*tax);
        grandTotal = total + taxAmount;

        //bill

        System.out.println("");                                                                         
        System.out.println(name);
        System.out.println("");                                                                         
        System.out.printf("%-20s %-15s %-15s\n", "Product Purchased", "Quantity", "Total Cost");            
        System.out.println("");
        System.out.printf("%-20s %-15s %-15s\n", item, itemsPurchased, total);
        System.out.println("");                                                                         
        System.out.printf("%-20s %-15s %-15s\n", "Tax(@8%):", "", taxAmount);
        System.out.printf("%-20s %-15s %-15s\n", "Total Cost:", "", grandTotal);

        break;                      

    //Quit

    case 3:

        System.out.println("End Program");
        break; //void

    //Default statement

    default:
        System.out.print("Invalid value entered.");

    }//end case

}//end main

}//end class
4

2 に答える 2

7
name = input.nextLine()

あなたは行全体を読んでいるのではなく、最初の単語だけを読んでいます。

Scannerクラスの場合、関数next()は次のトークン化された入力を読み取りnextLine()、キャリッジリターン(\n)まで行全体を取得します。

Eg. "happy days again"
next() // "happy"
next() // "days"
next() // "again"

or,

nextLine() // "happy days again"

編集:以下のコードを試してください

input.nextLine(); // IMP: To get the carriage return after choice is typed
//name
System.out.println("");                                     
System.out.print("Enter customer's name: ");
name = input.nextLine();                                           

//name of item purchased
System.out.print("Enter item purchased: ");
item = input.nextLine(); 
于 2012-06-18T02:19:43.137 に答える
0

これを試して、

を使用next()すると が得られますが1st Word、必要な場合はwhole line use nextLine().

name = input.nextLine();

例えば:

   Scanner scan = new Scanner(System.in);
   name = input.nextLine();
于 2012-06-18T02:32:58.720 に答える