为什么我的程序没有停止?

时间:2015-10-24 10:40:22

标签: java java.util.scanner

任何人都可以告诉我,当我击中你/我 - 为什么我的比赛没有停止,它只会重新开始。我在运行之前调用了我的isDone()方法,但它只是反复运行。

public class Model {
    int numberToGuess = 29;
    int counter;
    boolean isDone;
    Scanner sc = new Scanner(System.in);

    public Model() {
        isDone = false;
        run();
    }

    public void run() {
        welcomeMessage();
        while (true) {
            guessNumber();
        }
    }

    public void welcomeMessage() {
        System.out.println("Welcome to " + '\n' + "***** GUESS THE NUMBER *****");
    }

    public void guessNumber() {
        System.out.println("Please enter number and hit 'Enter'" + '\n');
        if (sc.hasNextInt()) {
            int input = sc.nextInt();
            counter++;
            if (input < numberToGuess) {
                System.out.println('\n' + "Your guess is too low!" + '\n');
            } else if (input > numberToGuess) {
                System.out.println('\n' + "Your guess is too high!" + '\n');
            } else {
                System.out.println("Congratulations, you guessed the number!" + '\n' + "You guessed the number in " + counter + " guess." + '\n');
                isDone();
            }
        } else {
            System.out.println("Invalid input, please enter a number!" + '\n');
            sc.next();
        }
    }

    public void isDone() {
        System.out.println("Do you wanna play again? Enter y/n");
        if (sc.hasNext()) {
            String input = sc.next();
            if ("y".equals(sc))
                if (input.equals("y")) {
                    isDone = false;
                    guessNumber();
                } else if (input.equals("n")) {
                    isDone = true;
                }
        }
        System.out.println("Invalid input, please enter y/n to continue");
        sc.next();
    }
}

4 个答案:

答案 0 :(得分:8)

你有一个无限循环:

 while (true)

没有条件终止程序。

您可以使用全局isDone变量,并将true替换为:

while (!isDone)
{
   guessNumber();
}

答案 1 :(得分:3)

isDone()方法有逻辑缺陷。我纠正了它。

第1步:将while (true)更改为while (!isDone)

步骤2:修正输入的“y”条件,并在使isDone = true之后注释最后两行,即使在输入“n”后导致程序继续

修改后的代码:

public void isDone() {
        System.out.println("Do you wanna play again? Enter y/n");

        if (sc.hasNext()) {
            String input = sc.next();
            if ("y".equals(input)) {
                isDone = false;
                guessNumber();
            } else if (input.equals("n")) {
                isDone = true;
            }
        }
        //System.out.println("Invalid input, please enter y/n to continue");
        //sc.next();
    }

编辑:解释条件中的缺陷。

您正在将扫描仪对象与“y”进行比较,而不是使用“y”从扫描仪读取的输入值

答案 2 :(得分:0)

您需要做的一个小改动

public BasicSubstanceViewModel GetBasicSubstance(string partNumber,  string version, int nodeID, int parentID )
{
    IMDSDataContext dc = new IMDSDataContext();
    var spResult = dc.spGetBasicSubstance( partNumber, version, nodeID, parentID).ToList();

    if(!spResult.Any())
    {
        return null;
    }

    var firstPart = spResult[0];

    var result = new BasicSubstanceViewModel
    {
        NodeID = firstPart.NodeID,
        CASNumber = firstPart.CASNumber,
        EINECSCode = firstPart.EINECSCode,
        EUIndex = firstPart.EINECSCode,
        Duty = firstPart.Duty,
        Prohibited = firstPart.Prohibited,
        Unwanted = firstPart.Unwanted,
        IsReach = firstPart.ISReach,
        SubstanceName = firstPart.SynonymName,
        GroupName = spResult.Select(p => new GroupName { Name = p.GroupName }).ToList(),
        Portion = firstPart.Portion
    };

    return result;
}

答案 3 :(得分:-1)

好的,现在它正在工作:)。解决方案:

公共类模型 {

array_data1 = reshape (data1',[],1);
array_data2 = reshape (data2',[],1);

matchmatrix = zeros(size(data2,1),size(data1,1));
for irow1 = 1: size(data2,1)
    for irow2 = 1: size(data1,1)
        matchmatrix(irow1,irow2) = min(data2(irow1,:) == data1(irow2,:))~= 0;
    end
end