首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java输入在完成Do/While时停止工作

Java输入在完成Do/While时停止工作
EN

Stack Overflow用户
提问于 2018-09-27 08:44:07
回答 1查看 45关注 0票数 0

我正在尝试创建一个运行循环,用户可以在其中输入进入/离开oyster bar的oyster,直到达到500的容量。

当容量达到500,我希望用户仍然能够输入负数(因为顾客仍然可以离开),但不能输入任何正数。不幸的是,一旦满足这个条件,我就不能输入任何东西。

代码语言:javascript
复制
    Scanner input = new Scanner(System.in);
        int maxCapacity = 500;
        int oystersIn = 0;
        int oystersOut;
        int currentCapacity = 0;
    System.out.println("Welcome to the Half Shell Dive Bar, Capacity is 500, and there are currently "+ currentCapacity + " oysters in the bar" );
    currentCapacity = 0;
    do {
        oystersIn=0;
        System.out.println("How many oysters are coming in?");
        oystersIn = input.nextInt();        
        currentCapacity += oystersIn;
        System.out.println("Now there are " + currentCapacity + " oysters in the bar");
    }
    while (oystersIn + currentCapacity <= 500);

    }

}

任何意见都将不胜感激,谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-27 08:50:48

假设您想要永远循环,则需要检查容量是否不足和过大

代码语言:javascript
复制
do {
    oystersIn=0;
    System.out.println("How many oysters are coming in?");
    oystersIn = input.nextInt(); 
    if (oystersIn + currentCapacity >= 500) {
        System.out.println("Sorry full - waiting for some one to leave");
        continue;
    }
    if (oystersIn + currentCapacity < 0) {
        System.out.println("Can't go negative");
        continue;
    }
    currentCapacity += oystersIn;
    System.out.println("Now there are " + currentCapacity + " oysters in the bar");
}
while (true);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52527860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档