如何在继续之前等待输入? import java.util.Scanner; 阶级集市 { 无效计算() { 整数和=0; 扫描仪sc=新的扫描仪(System.in); System.out.println(“嗨!欢迎使用out高级计算器”); System.out.println(“输入您希望计算的项目数”); int c=sc.nextInt(); 字符串项[]=新字符串[c]; 整数价格[]=新整数[c]; 对于(int i=1;i

如何在继续之前等待输入? import java.util.Scanner; 阶级集市 { 无效计算() { 整数和=0; 扫描仪sc=新的扫描仪(System.in); System.out.println(“嗨!欢迎使用out高级计算器”); System.out.println(“输入您希望计算的项目数”); int c=sc.nextInt(); 字符串项[]=新字符串[c]; 整数价格[]=新整数[c]; 对于(int i=1;i,java,input,Java,Input,在nextLine()调用后添加对nextLine()的调用,以清除nextLine()在管道中留下的剩余“\n”。Scanner nextLine()函数(及类似函数)不会读取在用户输入字符串后输入的换行符。因此它保留在缓冲区中。但是,当收到新行字符时,nextLine()函数会立即返回。 要解决此问题,请确保缓冲区中没有任何换行符。最快的方法是在每次调用nextInt()和类似函数后使用nextLine()调用。 或者避免使用nextLine()和next()函数,但它不能读取多字字符串 i

在nextLine()调用后添加对nextLine()的调用,以清除nextLine()在管道中留下的剩余“\n”。

Scanner nextLine()函数(及类似函数)不会读取在用户输入字符串后输入的换行符。因此它保留在缓冲区中。但是,当收到新行字符时,nextLine()函数会立即返回。 要解决此问题,请确保缓冲区中没有任何换行符。最快的方法是在每次调用nextInt()和类似函数后使用nextLine()调用。 或者避免使用nextLine()和next()函数,但它不能读取多字字符串

import java.util.Scanner;
class bazar
{
    void calculate ()
    {
        int sum=0;
        Scanner sc = new Scanner (System.in);
        System.out.println("Hi ! welcome to out advance calculator");
        System.out.println("Enter the number of items that you wish to compute");
        int c = sc.nextInt();
        String item[] = new String[c];
        int price[] = new int[c];
        for (int i=1; i<=c; i++)
        {
          System.out.println( "please enter the item name : " );
          item[i] = sc.nextLine();
          System.out.println();**// i need to wait for input before proceeding**
          System.out.println();
          System.out.println();
          System.out.println( "please enter the price of " +item[i]+":");
          price[i] = sc.nextInt();
          sum=sum+price[i];
        }
        //display part 
        for (int k=1; k<=c; k++)
        {
            System.out.println(  "ITEM                       PRICE");
            System.out.println (item[k]+"                  "+price[k]); 
        }
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("YOUR BILL TOTAL HAS COME TO----------------->"+sum);
    }
}