而"!b.equals(x)|| !b.equals(y)”是无限循环? [英] While "!b.equals(x) || !b.equals(y)" is an infinite loop?

查看:137
本文介绍了而"!b.equals(x)|| !b.equals(y)”是无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,这是我的代码,它正在做一个循环,但是它要做的是如果用户输入借位,那么它将询问用户它做了多少,然后他们输入一个数字

Hey guys this is my code and what it is doing is going in an loop but what its suppose to do is if the user types in borrow then it will ask the user how much which it does but then they type in a number and it will ask them again would you like to borrow or sell and it is in an infinite loop.

case 3:
            do{
                System.out.println("What would you like to do? Please type borrow to borrow money or sell to sell assets: ");
                    b = scan.nextLine().toLowerCase();
                if(b.equals("borrow")){
                    System.out.print("how much would you like to borrow Remmber if you go over 50000 debt its game over.");
                    try {
                        input = scan.nextInt();
                    } catch (Exception e) {
                        System.err.println("That is not a number!!");
                    }
                    account.setdebt(account.getDebt() + input);
                    account.setBalance(account.getBalance() + input);
                    System.out.println("Your new Balance is " + account.getBalance());
                }
                else if(b.equals("sell")){
                    sellA();
                }else{
                    System.out.println("You didn't input 'borrow' or 'sell'. Reinput please");
                }
            }while(!b.equals("borrow") || !b.equals("sell"));
            break;


推荐答案

您需要更改 | | 内的& ,而,否则该条件始终为true。 b 不等于的两个值中至少会有一个。

You need to change || to && inside while, otherwise the condition is always true. There'll always be at least one of those two values that b is not equal to.

这篇关于而"!b.equals(x)|| !b.equals(y)”是无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆