gpt4 book ai didi

java - For循环在第二次后不执行第一条命令

转载 作者:搜寻专家 更新时间:2023-11-01 01:45:15 26 4
gpt4 key购买 nike

这是我的代码:

import java.util.Scanner;

public class BankAccount1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

char pickup;
char size;
char type;
String name;

double cost=0.0;
int i;


for (i=0; i<2; i++)
{
System.out.println("What is your name?");
name = input.nextLine();

System.out.println("size of pizza: ");
size = input.next().charAt(0);

System.out.println("type of pizza: ");
type = input.next().charAt(0);

System.out.println("pick up or delivery?");
pickup = input.next().charAt(0);


if(size == 'S' || type == 'V') {cost = 10.0;}
else if(size == 'M' || type == 'V') {cost = 12.25;}
else if(size == 'L' || type == 'V') {cost = 14.50;}
else if(size == 'S' || type == 'C') {cost = 7.0;}
else if(size == 'M' || type == 'C') {cost = 8.0;}
else if(size == 'L' || type == 'C') {cost = 9.0;}


if(pickup == 'D'){
cost=cost+1.50;
System.out.println("Name: "+name+". Your cost is: "+cost);}
else {
System.out.println("Name: "+name+". Your cost is: "+cost);}


}

}

}

我的代码中有一个 for 循环,它询问名称、尺寸、类型和取件。第一次运行时,它要求输入所有字段,但第二次,名称字段由于某种原因不允许我输入名称,它打印“你的名字是什么?”但它不让我输入名字...?有人能告诉我为什么吗?谢谢。

这是它的样子:http://i.stack.imgur.com/eb1BA.jpg

最佳答案

因为

input.next()

不使用换行符,这些字符会传递回您的循环并且不允许输入值。您需要添加:

for (i = 0; i < 2; i++) {
// existing input.next() statements in here
...

input.nextLine();
}

for 循环的末尾修复。

关于java - For循环在第二次后不执行第一条命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12925660/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com