2012-06-18 27 views
1

该程序运行得很好,但由于某种原因,当我要求输入名称时,它只会保存响应中的第一个单词。例如,如果我稍后使用它输出时输入“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
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(); 
+0

感谢您的帮助。但我仍然有一个问题。每当我像这样编译它时,它将最后一个名称保存为“item”字符串。 – user1462585

+0

诀窍!谢谢一吨:) – user1462585

+0

太棒了!玩得开心学习! – vellvisher

0

代码试试这个,

使用next()会给你1st Word,但如果你想在whole line use nextLine()

name = input.nextLine(); 

如:

Scanner scan = new Scanner(System.in); 
    name = input.nextLine(); 
+0

句子不是用来形容它的正确词汇。 – nhahtdh

相关问题