Basisrekenmachine in Java

Ik probeer een eenvoudige calculator te maken in Java. Ik ben vrij nieuw in programmeren, dus ik probeer er aan te wennen.

import java.util.Scanner;
import javax.swing.JOptionPane;
public class javaCalculator 
{
    public static void main(String[] args) 
    {
        int num1;
        int num2;
        String operation;
        Scanner input = new Scanner(System.in);
        System.out.println("please enter the first number");
        num1 = input.nextInt();
        System.out.println("please enter the second number");
        num2 = input.nextInt();
        Scanner op = new Scanner(System.in);
        System.out.println("Please enter operation");
        operation = op.next();
        if (operation == "+");
        {
            System.out.println("your answer is" + (num1 + num2));
        }
        if  (operation == "-");
        {
            System.out.println("your answer is" + (num1 - num2));
        }
        if (operation == "/");
        {
            System.out.println("your answer is" + (num1 / num2));
        }
        if (operation == "*")
        {
            System.out.println("your answer is" + (num1 * num2));
        }
    }
}

Dit is mijn code. Het vraagt ​​om de cijfers en bediening, maar geeft de antwoorden allemaal samen weer ?


Antwoord 1, autoriteit 100%

Here is simple code for calculator so you can consider this
import java.util.*;
import java.util.Scanner;
public class Hello {
    public static void main(String[] args)
    {
        System.out.println("Enter first and second number:");
        Scanner inp= new Scanner(System.in);
        int num1,num2;
        num1 = inp.nextInt();
        num2 = inp.nextInt();
        int ans;
        System.out.println("Enter your selection: 1 for Addition, 2 for substraction 3 for Multiplication and 4 for division:");
        int choose;
        choose = inp.nextInt();
        switch (choose){
        case 1:
            System.out.println(add( num1,num2));
            break;
        case 2:
            System.out.println(sub( num1,num2));
            break;      
        case 3:
            System.out.println(mult( num1,num2));
            break;
        case 4:
            System.out.println(div( num1,num2));
            break;
            default:
                System.out.println("Illigal Operation");
        }
    }
    public static int add(int x, int y)
    {
        int result = x + y;
        return result;
    }
    public static int sub(int x, int y)
    {
        int result = x-y;
        return result;
    }
    public static int mult(int x, int y)
    {
        int result = x*y;
        return result;
    }
    public static int div(int x, int y)
    {
        int result = x/y;
        return result;
    }
}

Antwoord 2, autoriteit 93%

Verwijder de puntkomma’s uit uw if-instructies, anders is de code die volgt vrijstaand en wordt altijd uitgevoerd:

if (operation == "+");
                     ^

Gebruik ook .equals voor Strings, == vergelijkt Object-referenties:

 if (operation.equals("+")) {

Antwoord 3, autoriteit 30%

VergelijkStrings met equals(..) niet met ==

if (operation.equals("+")
{
    System.out.println("your answer is" + (num1 + num2));
}
if (operation.equals("-"))
{
    System.out.println("your answer is" + (num1 - num2));
}
if (operation.equals("/"))
{
    System.out.println("your answer is" + (num1 / num2));
}
if (operation .equals( "*"))
{
    System.out.println("your answer is" + (num1 * num2));
}

En de ; na de voorwaarden was een lege instructie, dus de voorwaarde had helemaal geen effect.

Als je java 7 gebruikt, kun je de if-statements ook vervangen door een switch.
In java <7 kun je testen of operation lengte 1 heeft en dan een switch maken voor de char [switch (operation.charAt(0))]


Antwoord 4, autoriteit 10%

import java.util.Scanner;
public class AdditionGame {
public static void main(String[] args) {
    // TODO Auto-generated method stub
    int num1;
    int num2;
    String operation;
    Scanner input = new Scanner(System.in);
    System.out.println("Please Enter The First Number");
    num1 = input.nextInt();
    System.out.println("Please Enter The Second Number");
    num2 = input.nextInt();
    Scanner op = new Scanner (System.in);
    System.out.println("Please Enter The Operation");
    operation = op.next();
    if (operation.equals("+"))
    {
        System.out.println("Your Answer is "+(num1 + num2));
    }
    else if (operation.equals("-"))
    {
        System.out.println("Your Answer is "+(num1 - num2));
    }       
    else if (operation.equals("*"))
    {
        System.out.println("Your Answer is "+(num1 * num2));
    }   
    else if (operation.equals("/"))
    {
        System.out.println("Your Answer is "+(num1 / num2));
    }
}

}


Antwoord 5, autoriteit 10%

Voorbeeld van Java-programma voor het maken van een eenvoudige rekenmachine:

import java.util.Scanner;
public class Calculator
{
public static void main(String args[])
{
    float a, b, res;
    char select, ch;
    Scanner scan = new Scanner(System.in);
    do
    {
        System.out.print("(1) Addition\n");
        System.out.print("(2) Subtraction\n");
        System.out.print("(3) Multiplication\n");
        System.out.print("(4) Division\n");
        System.out.print("(5) Exit\n\n");
        System.out.print("Enter Your Choice : ");
        choice = scan.next().charAt(0);
        switch(select)
        {
            case '1' : System.out.print("Enter Two Number : ");
                a = scan.nextFloat();
                b = scan.nextFloat();
                res = a + b;
                System.out.print("Result = " + res);
                break;
            case '2' : System.out.print("Enter Two Number : ");
                a = scan.nextFloat();
                b = scan.nextFloat();
                res = a - b;
                System.out.print("Result = " + res);
                break;
            case '3' : System.out.print("Enter Two Number : ");
                a = scan.nextFloat();
                b = scan.nextFloat();
                res = a * b;
                System.out.print("Result = " + res);
                break;
            case '4' : System.out.print("Enter Two Number : ");
                a = scan.nextFloat();
                b = scan.nextFloat();
                res = a / b;
                System.out.print("Result = " + res);
                break;
            case '5' : System.exit(0);
                break;
            default : System.out.print("Wrong Choice!!!");
        }
    }while(choice != 5);       
}
}

Antwoord 6

misschien is het beter om de case te gebruiken in plaats van if weet niet of dit de fout elimineert, maar het is schoner denk ik.. switch (operation){case +: System.out.println("your answer is" + (num1 + num2));break;case -: System.out.println("your answer is" - (num1 - num2));break; ...


Antwoord 7

java.util.Scanner importeren;
import javax.swing.JOptionPane;

openbare klasse javaCalculator
{

public static void main(String[] args) 
{
    int num1;
    int num2;
    String operation;
    Scanner input = new Scanner(System.in);
    System.out.println("please enter the first number");
    num1 = input.nextInt();
    System.out.println("please enter the second number");
    num2 = input.nextInt();
    Scanner op = new Scanner(System.in);
    System.out.println("Please enter operation");
    operation = op.next();
    if (operation.equals("+"))
    {
        System.out.println("your answer is" + (num1 + num2));
    }
   else if  (operation.equals("-"))
    {
        System.out.println("your answer is" + (num1 - num2));
    }
  else if (operation.equals("/"))
    {
        System.out.println("your answer is" + (num1 / num2));
    }
   else if (operation.equals("*"))
    {
        System.out.println("your answer is" + (num1 * num2));
    }
   else 
    {
       System.out.println("Wrong selection");
    }
}

}


Antwoord 8

public class SwitchExample {
    public static void main(String[] args) throws Exception {
        System.out.println(":::::::::::::::::::::Start:::::::::::::::::::");
        System.out.println("\n\n");
        System.out.println("1. Addition");
        System.out.println("2. Multiplication");
        System.out.println("3. Substraction");
        System.out.println("4. Division");
        System.out.println("0. Exit");
        System.out.println("\n");
        System.out.println("Enter Your Choice :::::::  ");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        int usrChoice = Integer.parseInt(str);
        switch (usrChoice) {
            case 1:
                doAddition();
                break;
            case 2:
                doMultiplication();
                break;
            case 3:
                doSubstraction();
                break;
            case 4:
                doDivision();
                break;
            case 0:
                System.out.println("Thank you.....");
                break;
            default:
                System.out.println("Invalid Value");
        }
        System.out.println(":::::::::::::::::::::End:::::::::::::::::::");
}
public static void doAddition() throws Exception {
    System.out.println("******* Enter in Addition Process ********");
    String strNo1, strNo2;
    System.out.println("Enter Number 1 For Addition : ");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    strNo1 = br.readLine();
    System.out.println("Enter Number 2 For Addition : ");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
    strNo2 = br1.readLine();
    int no1 = Integer.parseInt(strNo1);
    int no2 = Integer.parseInt(strNo2);
    int result = no1 + no2;
    System.out.println("Addition of " + no1 + " and " + no2 + " is ::::::: " + result);
}
public static void doSubstraction() throws Exception {
    System.out.println("******* Enter in Substraction Process ********");
    String strNo1, strNo2;
    System.out.println("Enter Number 1 For Substraction : ");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    strNo1 = br.readLine();
    System.out.println("Enter Number 2 For Substraction : ");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
    strNo2 = br1.readLine();
    int no1 = Integer.parseInt(strNo1);
    int no2 = Integer.parseInt(strNo2);
    int result = no1 - no2;
    System.out.println("Substraction of " + no1 + " and " + no2 + " is ::::::: " + result);
}
public static void doMultiplication() throws Exception {
    System.out.println("******* Enter in Multiplication Process ********");
    String strNo1, strNo2;
    System.out.println("Enter Number 1 For Multiplication : ");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    strNo1 = br.readLine();
    System.out.println("Enter Number 2 For Multiplication : ");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
    strNo2 = br1.readLine();
    int no1 = Integer.parseInt(strNo1);
    int no2 = Integer.parseInt(strNo2);
    int result = no1 * no2;
    System.out.println("Multiplication of " + no1 + " and " + no2 + " is ::::::: " + result);
}
public static void doDivision() throws Exception {
    System.out.println("******* Enter in Dividion Process ********");
    String strNo1, strNo2;
    System.out.println("Enter Number 1 For Dividion : ");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    strNo1 = br.readLine();
    System.out.println("Enter Number 2 For Dividion : ");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
    strNo2 = br1.readLine();
    int no1 = Integer.parseInt(strNo1);
    int no2 = Integer.parseInt(strNo2);
    float result = no1 / no2;
    System.out.println("Division of " + no1 + " and " + no2 + " is ::::::: " + result);
}

}


Antwoord 9

we kunnen eenvoudig in.next().charAt(0); gebruiken om + – * / bewerkingen als tekens toe te wijzen door bewerking als een char.

java.util.* importeren;
openbare klasse Calculator {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char operation;
int num1;
int num2;
 System.out.println("Enter First Number");
num1 = in.nextInt();
System.out.println("Enter Operation");
operation = in.next().charAt(0);
System.out.println("Enter Second Number");
num2 = in.nextInt();
if (operation == '+')//make sure single quotes
{
    System.out.println("your answer is " + (num1 + num2));
}
if (operation == '-')
{
    System.out.println("your answer is " + (num1 - num2));
}
if (operation == '/')
{
    System.out.println("your answer is " + (num1 / num2));
}
if (operation == '*')
{
    System.out.println("your answer is " + (num1 * num2));
}
}

}


Antwoord 10

import java.util.Scanner;
public class JavaApplication1 {
    public static void main(String[] args) {
         int x,
         int y;
         Scanner input=new Scanner(System.in);
         System.out.println("Enter Number 1");
         x=input.nextInt();
         System.out.println("Enter Number 2");
         y=input.nextInt();
         System.out.println("Please enter operation + - / or *");
         Scanner op=new Scanner(System.in);
         String operation = op.next();
         if (operation.equals("+")){
             System.out.println("Your Answer: " + (x+y));
         }
         if (operation.equals("-")){
             System.out.println("Your Answer: "+ (x-y));
         }
         if (operation.equals("/")){
             System.out.println("Your Answer: "+ (x/y));
         }
         if (operation.equals("*")){
            System.out.println("Your Answer: "+ (x*y));
         }
    }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Other episodes