如何在循环后不跳过用户输入

时间:2014-07-15 08:01:25

标签: java user-input

我试图创建一个java程序,要求用户输入项目的名称,数量和价格

这是我的代码

public static void main(String args[])
{
    Scanner input=new Scanner(System.in);
    String desc;
    List<String> description=new ArrayList<String>();
    int qty;
    List<Integer> quantity=new ArrayList<>();
    double prc;
    List<Double> price=new ArrayList<Double>();
    double amt;
    List<Double> amount=new ArrayList<Double>();

    String choice;
    int ctr=0;
    do
    {
        System.out.print("Description: ");
        desc=input.nextLine();
        description.add(desc);
        System.out.print("Qty: ");
        qty=input.nextInt();
        quantity.add(qty);
        System.out.print("Price: ");
        prc=input.nextDouble();
        price.add(prc);
        amt=qty*prc;
        amount.add(amt);
        System.out.print("Add another item?[Y/N]");
        choice=input.next();
        ctr+=1;
    }while(choice.equals("Y")||choice.equals("y"));
}

这就是输出中显示的内容

Description: item
Qty: 2
Price: 35
Add another item?[Y/N]: Y
Description:Qty:

在另一个循环之后跳过第一个问题 我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

最后添加input.nextLine();似乎有效

       do {
            System.out.print("Description: ");
            desc = input.nextLine();
            description.add(desc);
            System.out.print("Qty: ");
            qty = input.nextInt();
            quantity.add(qty);
            System.out.print("Price: ");
            prc = input.nextDouble();
            price.add(prc);
            amt = qty * prc;
            amount.add(amt);
            System.out.print("Add another item?[Y/N]");
            choice = input.next();
            input.nextLine();
            ctr += 1;
        } while (choice.equals("Y") || choice.equals("y"));

输出:

Description: Item 1
Qty: 2
Price: 23
Add another item?[Y/N]Y
Description: Item 2
Qty: 3
Price: 14
Add another item?[Y/N]N

答案 1 :(得分:0)

您可以使用Console类来获取用户的输入并处理空格。 这按预期工作(包括描述中的空格):

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public class Test{
public static void main(String args[]) {        

    Console c = new Console();

    String desc;
    List<String> description=new ArrayList<String>();
    int qty;
    List<Integer> quantity=new ArrayList<Integer>();
    double prc;
    List<Double> price=new ArrayList<Double>();
    double amt;
    List<Double> amount=new ArrayList<Double>();

    String choice;
    int ctr=0;
    do
    {                      
        desc=c.readLine("Description: ");
        description.add(desc);        
        qty=Integer.parseInt(c.readLine("Qty: " ));
        quantity.add(qty);         
        prc=Double.parseDouble(c.readLine("Price: "));
        price.add(prc);
        amt=qty*prc;
        amount.add(amt);            
        choice=c.readLine("Add another item?[Y/N]");
        ctr+=1;
    }while(choice.equals("Y")||choice.equals("y"));
}
}

class Console {
BufferedReader br;
PrintStream ps;

public Console(){
    br = new BufferedReader(new InputStreamReader(System.in));
    ps = System.out;
}

public String readLine(String out) {
    ps.format(out);
    try {
        return br.readLine();
    } catch(IOException e){
        return null;
    }
}   
}