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

时间:2016-08-05 01:36:03

标签: java

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

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 个答案:

答案 0 :(得分:1)

首先,您在第二个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永远不会在此循环中更改(您更改了w1),因此它将始终为0并且您永远不会退出循环。 我没有使用int w1,w2w3,而是不重用它们,而是使用单个(有意义的名称)布尔变量作为样式:

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

您的switch语句看起来很不错,但同样,您永远不会更改w3。我建议再次使用validInput。在您的第一种情况下,您还声明零不能被分割,这是不正确的。 0可以划分(0 /任何== 0),但不能除以零。您还需要处理用户输入无效操作的情况(即符号&lt; 1或sign&gt; 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

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