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



我正在尝试创建一个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:

在另一个循环之后,它跳过第一个问题我怎样才能解决这个问题?

最后添加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

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

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;
    }
}   
}

相关内容

  • 没有找到相关文章

最新更新