JAVA上的计算器

-2 java for-loop character calculator switch-statement

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;
        }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

Abd*_*_98 5

你失踪了,但break;对于所有case使用 inswitch

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;
            }
            
            }
            
            

        }
    }

Run Code Online (Sandbox Code Playgroud)

笔记:

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.
Run Code Online (Sandbox Code Playgroud)