简体   繁体   English

do..while 的另一个问题

[英]Another issue with do..while

I rewrote my kinda' first program in Java and now it looks like this:我用 Java 重写了我的第一个程序,现在看起来像这样:

import java.util.*;
import static java.lang.Math.*;

public class FirstApp {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    double a = 0;
    double b = 0;
    double c = 0;
    double delta;
    double x1 = 0, x2 = 0;
    boolean repeat = false;
    boolean decision = false;


    System.out.println("Calculator 2.0");
    System.out.println("Bartosz Kubacki 6.10.2016");
    System.out.println("-------------------------------");
    System.out.println("Welcome in Calculator 2.0 which helps you solve Ax^2 + Bx + C = 0.");

    do
    {
        do 
        {
            System.out.println("Enter A param (different from 0: ");

            if(input.hasNextDouble())
            {
                a = input.nextDouble();
                if(a == 0)
                {
                    System.out.println("Param A needs to be different from 0!");
                }   
            } else
            {
                input.nextLine();
                System.out.println("Param A must be a number!");
            }
        } while(a == 0);

        do
        {
            System.out.println("Enter B param: ");

            if(input.hasNextDouble())
            {
                b = input.nextDouble();
                repeat = false;
            } else
            {
                input.nextLine();
                System.out.println("Param B must be a number!");
                repeat = true;
            }
        } while(repeat);

        do
        {
            System.out.println("Enter C param: ");

            if(input.hasNextDouble())
            {
                c = input.nextDouble();
                repeat = false;
            } else
            {
                input.nextLine();
                System.out.println("Param C must be a number!");
                repeat = true;
            }
        } while(repeat);

        // Counting.. and showing results

        delta = pow(b, 2) - (4 * a * c);
        if(delta == 0)
        {
            x1 = (0 - b) / 2 * a;
            System.out.printf("There is one solution: %.2f\n", x1);
        } else if(delta > 0)
        {
            x1 = ((0 - b) - sqrt(delta)) / 2 * a;
            x2 = (b + sqrt(delta)) / 2 * a;
            System.out.printf("There are two solutions: %.2f and %.2f\n", x1, x2);
        } else
        {
            System.out.println("There is no solution.");
        }

        // Decyzja o kolejnych działaniach

        do
        {
            System.out.println("Want make another eqation? [Y/N] ");
            String dec = null;

            if(input.hasNext("Y") || input.hasNext("N") || input.hasNext("y") || input.hasNext("n"))
            {
                dec = input.nextLine();
                if(dec.equalsIgnoreCase("Y"))
                    decision = true;
                else
                    decision = false;

                repeat = false;
            } else
            {
                input.nextLine();
                repeat = true;
            }
        } while(repeat);
    } while(decision);

    input.close();
}
}

Everything goes well actually except:实际上一切都很顺利,除了:

  1. When I put a non-numeric type while entering values for A, B and C a double warning about that shows and then it works normally.(only 1 warning)当我在输入 A、B 和 C 的值时输入非数字类型时,会显示一个关于该值的双重警告,然后它正常工作。(只有 1 个警告)

  2. When I answer Y or N then program terminates, BUT if I will enter anything else like "g" "2" "w" etc., then program asks me again (twice also at the first time) and then when I enter Y or N it works great.当我回答 Y 或 N 时,程序终止,但如果我输入“g”“2”“w”等其他内容,则程序再次询问我(第一次也两次),然后当我输入 Y 或N 效果很好。

I really dunno what's that about becouse there are no warnings or errors during compilation.我真的不知道那是什么,因为编译期间没有警告或错误。

Thanks for all answers.感谢所有的答案。 :) :)

// Decyzja o kolejnych działaniach
            String dec = null;
            input.nextLine();
            do
            {
                System.out.println("Want make another eqation? [Y/N]: ");

                dec = input.nextLine();

                if(dec.equalsIgnoreCase("Y")) {
                    decision = true;
                    repeat = false;
                }
                else if(dec.equalsIgnoreCase("n")){
                    decision = false;
                    repeat = false;
                } else
                    repeat = true;
            } while(repeat);

I've edited your code as needed.我已根据需要编辑了您的代码。 I personally wouldn't code it this way, but I'd rather make minimal changes.我个人不会这样编码,但我宁愿做最小的改变。

You last loop was making a new String dec every time it looped.上次循环每次循环时都会创建一个新的String dec Which is okay, but not recommended.哪个好,但不推荐。 I moved it to above the loop.我将它移到循环上方。

I also had to grab the input before the loop, to stop it from messing up.我还必须在循环之前获取输入,以防止它搞砸。 There must be some leftover bits in it.里面肯定有一些剩余的东西。

I then removed your hasNext() conditional, and just read the input because it doesnt matter what they are going to enter, you only care if it is a Y or an N , I added the appropriate if statements.然后我删除了你的hasNext()条件,并只读取输入,因为它们将输入什么并不重要,你只关心它是Y还是N ,我添加了适当的if语句。

That'll do it.这样就可以了。 Works like it should.像它应该的那样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM