微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JAVA上的计算器

如何解决JAVA上的计算器

package calculator;

import java.util.Scanner;

public class NTimesRunningTwoInputCalculator {

    public static void main(String[] args) {
        // Todo Auto-generated method stub
        Scanner sc = new Scanner(system.in);
        System.out.println("enter a number = ");
        int a = sc.nextInt();
        System.out.println("enter number of opration = ");
        int n = sc.nextInt();
        int result = 0;
        int remainder = 0;
        for (int i = 1;i<=n;i++) {
            System.out.println("enter another number =");
            int b = sc.nextInt();
            System.out.println("enter operation =");
            sc.nextLine();
            char c = sc.nextLine().charat(0);
             switch(c) {
             case'+' :
                 result = a+b;
             case'-':
                 result = a-b;
             case'*':
                 result = a*b;
             case'/':
                 result = a/b;
                
                 remainder = a%b;
                 
             }
        System.out.println("the result after "+i+ "operation is "+result);
        if(remainder>0) {
            System.out.println("the remainder after"+i+"operation is"+remainder);
            a = result;
        }
        }
    }
}

// 这是java程序 谁能找出其中的错误并改正

我试图在 java 上创建一个运行 n 次的计算器,但我遇到了问题,你能帮我找出错误

解决方法

对于在 break; 中使用的所有 case,您都缺少 switch

import java.util.Scanner;

public class hello {

    public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);

        
        int result = 0;
        int remainder = 0;
        System.out.println("enter number of opration =  ");
        int d = sc.nextInt();

        for (int i = 1;i<=d;i++) {
            System.out.println("enter a first number = ");
            int a = sc.nextInt();
            System.out.println("enter a second number = ");
            int b = sc.nextInt();
            System.out.println("enter number of opration = ");
            char n = sc.next().charAt(0);
           
            
              
                 switch(n) {
                 case'+' :
                     result = a+b;
                     break;
                 case'-':
                     result = a-b;
                     break;
                 case'*':
                     result = a*b;
                     break;
                 case'/':
                     result = a/b;
                     break;
                 case'%':
                     remainder = a%b;
                     break;

                     
                 }
                 
            System.out.println("the result after "+i+ "operation is "+result);
            if(remainder>0) {
                System.out.println("the remainder after"+i+"operation is"+remainder);
                a = result;
            }
            
            }
            
            

        }
    }

注意:

The break statement is used inside the switch to terminate a statement sequence.
The break statement is optional. If omitted,execution will continue on into the next 
case.
,

在您的 switch 块中,case 语句缺少 break; 语句。

 switch(c) {
             case'+' :
                 result = a+b;
                 break;     //<== add these after each case
             case'-':
                 result = a-b;
                 break;
             case'*':
                 result = a*b;
                 break;
             case'/':
                 result = a/b;
                
                 remainder = a%b;
                 
             }

这与通过 switch 语句的行为有关。

1 当被开启的变量等于一个case时,
该 case 之后的语句将一直执行到 break 语句 到达了。
2 当遇到break语句时,switch终止,控制流跳转到switch之后的下一行
陈述。
3 并非每个案例都需要包含休息时间。如果没有出现中断,则控制流将落入后续案例 直到一个 到达休息时间。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。