java - 计算 : How to add the operator before each number like a real calc

标签 java java.util.scanner

我只是想构建一个计算器,但我不知道如何在每个数字之前提示运算符。我正在尝试构建一个计算器并具有指定数量的数字输入,但他们可以通过按 -1 取消它,并且它仍然会返回最终值。

但是我尝试使用字符串语句,但我是编码新手,不知道从哪里开始。

    int operator;
    int howmany;
    int i=0;
    int all = 0;
    double div;

    int scan;
    System.out.println("Press 1 for addition, 2 for subtraction, 3 for \n 
multiplication 4 for division or 5 for modules");
    operator=scanner.nextInt();
    if(operator < 1 || operator >5) {System.out.println("Restart 
calculator");System.exit(0);}
    System.out.println("How many numbers will you be using?");
    howmany = scanner.nextInt() -1 ;


    if (operator == 1){

        System.out.println("Press -1 to cancel ");

    while(i <=howmany) {

        i++;
        System.out.println("Enter number" + i);
        System.out.println("So far your numbers is " + all);
        scan = scanner.nextInt();   
        all += scan;

        if(i-1 == howmany ) {
            System.out.println("Your final answer is " + all);
        }

        if(scan == -1) {            
            System.out.println("No more inputs, final answer was  " + all);
            break;
            }
        }
    }

if (operator == 2){

        System.out.println("Press -1 to cancel ");

    while(i <=howmany) {

        i++;
        System.out.println("Enter number" + i);
        System.out.println("So far your numbers is " + all);
        scan = scanner.nextInt();   
        all -= scan;

        if(i-1 == howmany ) {
            System.out.println("Your final answer is " + all);
        }

        if(scan == -1) {            
            System.out.println("No more inputs, final answer was  " + all);
            break;
            }
        }
    }
if (operator == 3){
all = 1;
System.out.println("Press -1 to cancel ");

while(i <=howmany) {

i++;
System.out.println("Enter number" + i);
System.out.println("So far your numbers is " + all);
scan = scanner.nextInt();   
all *= scan;

if(i-1 == howmany ) {
    System.out.println("Your final answer is " + all);
}

if(scan == -1) {            
    System.out.println("No more inputs, final answer was  " + all);
    break;
    }
}
}   
if (operator == 4){

System.out.println("Press -1 to cancel ");

while(i <=howmany) {

i++;
System.out.println("Enter number" + i);
System.out.println("So far your numbers is " + all);
scan = scanner.nextInt();
all = scan;


if(i-1 == howmany ) {
    System.out.println("Your final answer is " + all);
}

if(scan == -1) {            
    System.out.println("No more inputs, final answer was  " + all);
    break;
    }
}
}   
if (operator == 5){

System.out.println("Press -1 to cancel ");

while(i <=howmany) {

i++;
System.out.println("Enter number" + i);
System.out.println("So far your numbers is " + all);
scan = scanner.nextInt();   
all %= scan;

if(i-1 == howmany ) {
    System.out.println("Your final answer is " + all);
}

if(scan == -1) {            
    System.out.println("No more inputs, final answer was  " + all);
    break;
    }
}
}
}
}

最佳答案

当你编码时不知道从哪里开始时,先把算法写成句子,然后再转换成代码会有所帮助。例如,对于计算器程序,我将从以下基本结构开始:

// need a data structure to hold all numbers
// need a data structure to hold all operations

while(the user wishes to continue) {
    while(the user has not hit =) {
        // use the scanner to get the next number (if it's -1, exit program)
        // use the scanner to get the next operator (if it's = then exit inner 
        // loop to report result)
    }
    // need variable to hold calculation result, instantiate to first number
    for(loop through numbers data structure) {
        // apply next operator to the running result and next number
    }
    // report result to user
}

其他需要考虑的事情:计算器应该能够在计算中使用 -1。我不确定这是否是您正在从事的工作的要求,所以我将其保留为您所描述的那样,但是更符合计算器精神的做法是在每次之后询问用户如果他们想继续,则计算成功(y/n)。

关于java - 计算 : How to add the operator before each number like a real calc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54139928/

相关文章:

java - 数字格式和输入不匹配异常有何不同

java - 无法读取字符串中的字符 (Java)

java - 尝试将一个类中的信息刷新到java中的另一个类中

java - 如何解析格式类似于成绩簿的文本文件?

java - 扫描仪问题

java - 未定义的行为

java - 我如何获得单词的音素以与 sphinx 一起使用

java - ROW_NUMBER() 没有正确排序记录

java - 代码从顺序到线程

java - 找不到符号方法 getSupportFragmentManager()