使用try时,在循环中执行while循环

[英]stuck in loop do while loop while using try


I am string to make a simple Calculator if some one divides by zero it does not break the code. I honestly have no ideal what to I did every thing that i could.I need to get this don't for school.my home work reads perform addition, subtraction,multiplication and division on two numbers.handle invalid Exception and arithmetic Exception.

我是一个简单的计算器,如果有一个除以零,它不会破坏代码。老实说我不知道​​我能做的每一件事我都可以。我需要得到这个不用于学校。我的家庭工作读取对两个数字执行加法,减法,乘法和除法。句柄无效异常和算术异常。

import java.util.*;
public class Calcultator {

/**
 * @param args
 */
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int w1=0;
    int w2=0;
    int w3=0;
    double i1 = 0;
    double i2=0;
    String sign1 = null;
    double sum=0;
    do {
        try {   
            System.out.println("Enter a your first number");
            i1=scan.nextDouble();
            w1++;
            System.out.println("il="+1l);
            System.out.println("w1="+w1);
        } catch(Exception e) {
            System.out.println("you must enter a number");  
            w1=0;
        }
    } while(w1==0);

    do {
        try {
            System.out.println("Enter a your first number");                
            i2=scan.nextDouble();
            w1++;
        } catch(Exception e) {
            System.out.println("you must enter a number");  
        }
    } while(w2==0);

    do {
        try {
            System.out.println("1)/ 2)* 3)- 4)+");
            int sign=scan.nextInt();
            switch(sign) {
                case 1:
                    if(i1==0 || i2==0){
                        System.out.println("Zero can not be Devided");
                        break;
                    } else {
                        sign1="/";
                        break;
                    }
                case 2:
                    sign1="*";
                    break;
                case 3:
                    sign1="-";
                    break;
                case 4:
                    sign1="+";
                    break;
                default :
                    break;
            }
        } catch(Exception e) {
            System.out.println("you must enter a number");  
        }
    } while(w3==0);

    if(sign1=="/") {
        sum=i1/i2;
        System.out.println(i1 +"/"+i2+"="+sum);
    } else if(sign1=="*") {
        sum=i1*i2;
        System.out.println(i1 +"*"+i2+"="+sum);
    } else if(sign1=="-") {
        sum=i1-i2;
        System.out.println(i1 +"-"+i2+"="+sum);
    } else {
        sum=i1+i2;
        System.out.println(i1 +"+"+i2+"="+sum);
    }

    scan.close();
}
}

1 个解决方案

#1


1  

First of all, you've done a bad copy/paste in the second do-while loop:

首先,你在第二个do-while循环中做了一个糟糕的复制/粘贴:

do {
    try {
        System.out.println("Enter a your first number"); // should say "Enter a your second number"         
        i2=scan.nextDouble(); // correct
        w1++; // should be w2 - but I'd use a bool instead
    } catch(Exception e) {
        System.out.println("you must enter a number");
        //need to set w2 in here - added below
        w2 = 0;
    }
} while(w2==0);

w2 is never changed in this loop (you changed w1 instead), so it will always be 0 and you'll never exit the loop. Instead of using int w1,, w2, and w3, then not reusing them, I'd use a single (meaningfully named) boolean variable as a matter of style:

w2在这个循环中永远不会改变(你改变了w1),所以它总是0,你永远不会退出循环。而不是使用int w1 ,, w2和w3,然后不重用它们,我将使用单个(有意义的名称)布尔变量作为样式:

boolean validInput = false;
do {
    try {
        System.out.println("Enter your first number");
        i1 = scan.nextDouble();
        validInput = true;
    } catch(Exception e) {
        System.out.println("You must enter a number");
        validInput = false;
    }
} while(!validInput);
validInput = false;
// Second loop to follow using validInput instead of w2

Your switch statement looks mostly good, but again, you never change w3. I'd suggest using validInput again. In your first case, you also state that zero cannot be divided, which is incorrect. 0 can be divided (0/anything == 0), but you cannot divide by zero. You also need to handle the case where the user enters an invalid operation (i.e sign < 1 or sign > 4). I'd also do the calculation where you set the signs, eliminating the need for multiple calculations at the end. What I'd suggest:

你的switch语句看起来很好,但同样,你永远不会改变w3。我建议再次使用validInput。在您的第一种情况下,您还声明零不能被分割,这是不正确的。 0可以划分(0 /任何== 0),但不能除以零。您还需要处理用户输入无效操作的情况(即符号<1或符号> 4)。我还会在您设置标志的位置进行计算,最后无需进行多次计算。我建议的是什么:

validInput = false;
double result  = 0;
String operationStr = null; // I'll use operationStr instead of sign1, so the reader knows what it's for
do {
    try {
        // More understandable output for the user (unless it must be in the format you supplied)
        System.out.println("Enter an operation: ");
        System.out.println("Enter 1 for /");
        System.out.println("Enter 2 for *");
        System.out.println("Enter 3 for -");
        System.out.println("Enter 4 for +");
        int inputOperation = scan.nextInt(); // inputOperation instead of sign for readability: + and - are signs, * and / aren't.
        switch(inputOperation) {
            case 1:
                if(i2 == 0){ // only need to worry about dividing BY zero
                    System.out.println("Error: cannot divide by zero - undefined");
                } else {
                    operationStr = "/";
                    validInput = true;
                    result = i1 / i2;
                }
                break; // only really need one break statement, but this is again a trivial matter of style.
            case 2:
                operationStr = "*";
                validInput = true;
                result = i1 * i2;
                break;
            case 3:
                operationStr = "-";
                validInput = true;
                result = i1 - i2;
                break;
            case 4:
                operationStr = "+";
                result = i1 + i2;
                break;
            default:
                // An invalid int was entered, out of the range of our operators
                System.out.println("Error: Please enter a valid operation: 1, 2, 3, or 4 ")
                break;
        }
    } catch(Exception e) {
        System.out.println("you must enter a number");
    }
} while(validInput  = false);

System.out.println(i1 + operationStr + i2 + "=" + result);
scan.close();
// End of program

As a final note, there are many spelling errors and badly named variables in your code that you may want to fix up, the marker won't be too impressed with sloppy grammar or bad spelling. Try to name your variables based on what they do, so that your marker and anyone reading it can easily understand your code. For example, name i1 input1 instead. Use boolean validInput instead of int w1, w2, w3, as these have no meaning by looking at them. Use spaces between values in operations involving variables for readability, and finally, use indentation properly so the reader can make sense of your loops.

作为最后一点,你的代码中有许多拼写错误和命名错误的变量你可能想要修复,标记不会对草率语法或拼写错误印象太深。尝试根据他们的工作来命名变量,以便您的标记和阅读它的任何人都可以轻松理解您的代码。例如,改为命名为i1 input1。使用boolean validInput而不是int w1,w2,w3,因为通过查看它们没有任何意义。在涉及变量的操作中使用值之间的空格以便于阅读,最后,正确使用缩进,以便读者能够理解循环。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2016/08/05/332f543250c540a47968b321c0718bb7.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告