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

将军

我是一个简单的计算器的字符串,如果有人除以零,它不会破坏代码。老实说,我对我所能做的每一件事都不抱任何理想。我需要让这件事不上学。我的家庭作业读取对两个数字进行加,减,乘和除运算。处理无效的Exception和算术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();
}
}
纳芬

首先,您在第二个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,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

您的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

最后要注意的是,您的代码中可能有很多拼写错误和命名错误的变量,您可能想要修复,这些标记不会因草率的语法或拼写错误而给人留下深刻的印象。尝试基础上,他们如何命名你的变量,让您的标记,任何人阅读它可以很容易地理解你的代码。例如,将名称改为i1 input1。使用boolean validInput代替int w1, w2, w3,因为通过查看它们没有任何意义。在涉及变量的操作中的值之间使用空格以提高可读性,最后,适当地使用缩进,以便读者可以理解循环。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章