Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我做错了什么?你得到了这个额外的输出吗?_Java - Fatal编程技术网

Java 我做错了什么?你得到了这个额外的输出吗?

Java 我做错了什么?你得到了这个额外的输出吗?,java,Java,下面是它的一些输出: System.out.println("Enter UPC for an item you want, enter -1 when done"); do { System.out.print("\nEnter a UPC "); targetUPC = keyboard.nextLine(); RetailItem temporary = new RetailItem("Default", 0, 0, targetUPC); if(ite

下面是它的一些输出:

System.out.println("Enter UPC for an item you want, enter -1 when done");

do {
    System.out.print("\nEnter a UPC ");
    targetUPC = keyboard.nextLine();
    RetailItem temporary = new RetailItem("Default", 0, 0, targetUPC);

    if(itemList.indexOf(temporary) > -1) {
        RetailItem itemIndex = itemList.get(itemList.indexOf(temporary));
        System.out.println("\n" + itemIndex);
        System.out.print("How many would you like? ");
        numOfItems = keyboard.nextInt();
        itemIndex.setInStock(itemIndex.getInStock() - numOfItems);
        totalCost = numOfItems * itemIndex.getPrice();
    }

    if(itemList.indexOf(temporary) == -1 && !targetUPC.equals("0")) {
        System.out.print(targetUPC + " not found.\n");
    }

我脑子里一直在想这个问题,但想不出来你把
nextLine()
nextLine()混合在一起,这会留下尾随的换行符,而
nextLine()在循环中。读后面的新行,就像

Enter UPC for an item you want, enter -1 when done

Enter a UPC: 999

999 not found.

Enter a UPC: 61835

Description: Corn Crisps
Price: $9.45
Number in stock: 35
UPC: 61835

How many would you like? 5

Enter a UPC  not found.                  //Why am I getting this?

Enter a UPC 0

Total cost: $47.25
或者一开始就把整条线都消耗掉

numOfItems = keyboard.nextInt();
keyboard.nextLine();

您将
nextLine()
nextLine()
混合在一个循环中,这将留下尾随的换行符。读后面的新行,就像

Enter UPC for an item you want, enter -1 when done

Enter a UPC: 999

999 not found.

Enter a UPC: 61835

Description: Corn Crisps
Price: $9.45
Number in stock: 35
UPC: 61835

How many would you like? 5

Enter a UPC  not found.                  //Why am I getting this?

Enter a UPC 0

Total cost: $47.25
或者一开始就把整条线都消耗掉

numOfItems = keyboard.nextInt();
keyboard.nextLine();

您将
nextLine()
nextLine()
混合在一个循环中,这将留下尾随的换行符。读后面的新行,就像

Enter UPC for an item you want, enter -1 when done

Enter a UPC: 999

999 not found.

Enter a UPC: 61835

Description: Corn Crisps
Price: $9.45
Number in stock: 35
UPC: 61835

How many would you like? 5

Enter a UPC  not found.                  //Why am I getting this?

Enter a UPC 0

Total cost: $47.25
或者一开始就把整条线都消耗掉

numOfItems = keyboard.nextInt();
keyboard.nextLine();

您将
nextLine()
nextLine()
混合在一个循环中,这将留下尾随的换行符。读后面的新行,就像

Enter UPC for an item you want, enter -1 when done

Enter a UPC: 999

999 not found.

Enter a UPC: 61835

Description: Corn Crisps
Price: $9.45
Number in stock: 35
UPC: 61835

How many would you like? 5

Enter a UPC  not found.                  //Why am I getting this?

Enter a UPC 0

Total cost: $47.25
或者一开始就把整条线都消耗掉

numOfItems = keyboard.nextInt();
keyboard.nextLine();

当用户为
numOfItems
输入一行输入时,
nextInt()
不会使用该行。因此,当您回到循环的开始并调用
targetUPC=keyboard.nextLine(),则获得已输入行的剩余部分。调用
nextLine()
在您阅读
numOfItems
以使用为下一个循环准备的其余输入后。

当用户为
numOfItems
输入一行输入时,
nextInt()
不会使用该行。因此,当您回到循环的开始并调用
targetUPC=keyboard.nextLine(),则获得已输入行的剩余部分。调用
nextLine()
在您阅读
numOfItems
以使用为下一个循环准备的其余输入后。

当用户为
numOfItems
输入一行输入时,
nextInt()
不会使用该行。因此,当您回到循环的开始并调用
targetUPC=keyboard.nextLine(),则获得已输入行的剩余部分。调用
nextLine()
在您阅读
numOfItems
以使用为下一个循环准备的其余输入后。

当用户为
numOfItems
输入一行输入时,
nextInt()
不会使用该行。因此,当您回到循环的开始并调用
targetUPC=keyboard.nextLine(),则获得已输入行的剩余部分。调用
nextLine()
在读取
numOfItems
后,使用剩余的输入,为下一个循环做好准备。

nextInt()将在输入缓冲区中留下一个未读的换行符

nextLine()调用随后处理该换行符,然后返回一个空字符串,该字符串被分配给targetUPC,并且可能没有匹配的RetailItem…

nextInt()将在输入缓冲区中留下一个未读的换行符

nextLine()调用随后处理该换行符,然后返回一个空字符串,该字符串被分配给targetUPC,并且可能没有匹配的RetailItem…

nextInt()将在输入缓冲区中留下一个未读的换行符

nextLine()调用随后处理该换行符,然后返回一个空字符串,该字符串被分配给targetUPC,并且可能没有匹配的RetailItem…

nextInt()将在输入缓冲区中留下一个未读的换行符


该换行随后由nextLine()调用处理,该调用返回一个空字符串,该字符串被分配给targetUPC,并且可能没有匹配的RetailItem…

您想做什么?请提供无需猜测其余代码即可成功编译的代码keyboard.nextInt()
之后编码>(并丢弃结果),以便使用换行符。否则,下一个
targetUPC=keyboard.nextLine()
将使用换行符,从而产生一个空字符串。谢谢你,ajb,这完全有道理,我应该记得你想做什么?请提供无需猜测其余代码即可成功编译的代码keyboard.nextInt()
之后编码>(并丢弃结果),以便使用换行符。否则,下一个
targetUPC=keyboard.nextLine()
将使用换行符,从而产生一个空字符串。谢谢你,ajb,这完全有道理,我应该记得你想做什么?请提供无需猜测其余代码即可成功编译的代码keyboard.nextInt()
之后编码>(并丢弃结果),以便使用换行符。否则,下一个
targetUPC=keyboard.nextLine()
将使用换行符,从而产生一个空字符串。谢谢你,ajb,这完全有道理,我应该记得你想做什么?请提供无需猜测其余代码即可成功编译的代码keyboard.nextInt()
之后编码>(并丢弃结果),以便使用换行符。否则,下一个
targetUPC=keyboard.nextLine()
将使用换行符,从而产生一个空字符串。谢谢你,这完全有道理,我应该记住这一点