if-else Statement (Exercise 1)

preview_player
Показать описание
Java Programming: if-else program in Java Programming
Topics Discussed:
1. Developing a calculator using the if-else statement.

Music:
Axol x Alex Skrindo - You [NCS Release]

#JavaByNeso #JavaProgramming #ifelseStatement
Рекомендации по теме
Комментарии
Автор

If anyone wondered you don't have to use "char" type for operator. You can also use "String". Just put operators with double quotes instead of single ones in your conditions. This way you can simply write:
"String op = s.next();"
instead of
"char op = s.next().charAt(0);"

marcinnalborczyk
Автор

Being in class 12 This video reminds why I need to start working ..

uttamsoren
Автор

I am finally done with making the calculator with switch statements and also use character and string also here are the two codes Could anyone check if it is right or wrong?

NakibAkash
Автор

Please answer : Why did he write (int) in sout method? If he wanted to convert double into integer why didn't the answer showed in decimal?

zainabfatima
Автор

Bro a doubt when we intialize multiple scanner objects like s1, s2 s3 and use it for methods like s1.nextInt(), s2.nextInt()
Then it is not stopping at spaces bro .
Why is this happening

bulliraju
Автор

Exception in thread "main"
at
at
at
at

why???

juipatil
Автор

can anyone plese explain why we are giving spaces between the numbers i.e exmaple 2_+_3
why not we are entering 2+3

bhaskarputta
Автор

package challenge1;
import java.util.Scanner;

public class Challenge1 {

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

System.out.print("Enter num1 operator num2 (example: 1 + 2): ");
double num1 = s.nextDouble();
char c = s.next().charAt(0);
double num2 = s.nextDouble();

if (c == '+'){
System.out.print("The result is: " +(num1 + num2));
}else if(c == '-'){
System.out.print("The result is: " +(num1 - num2));
}else if(c == '*'){
System.out.print("The result is: " +(num1 * num2));
}else if(c == '/'){
System.out.print("The result is: " +(num1 / num2));
}else{
System.out.print("Error!");
}

axienator
Автор

I get an error like inputmismatch exception, what to do same code I have typed but it shows error

kavyaa
Автор

Bro which app do you use for java this is not bluej

simponly
Автор

why can he enter the n1+op+n2 in only one input?

catrieltorrez
Автор

Sorry, what is the reason using charAt(0)? Can anyone explain it?

purbalingga
Автор

Somebody please explain char op = s.next().charAt(0)

Thank you

casoarcano
Автор

I wanted to know how to add the, “The result is: “ part 😩

Noivad
Автор

package General;
import java.util.Scanner;

public class if_else_exe_one {
public static void main(String args[]){
System.out.println("enter num 1, op, num2. ");

Scanner sc=new Scanner(System.in);
double d1=sc.nextDouble();
char op=sc.next().charAt(0);
double d2=sc.nextDouble();

if(op=='+')

else if(op=='-')

else if(op=='*')

else if(op=='/')

}

}
for me it show some error like input mismatch

jayapradeesh
Автор

import java.util.*;

public class Logical {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter num1 operator num2 (example : 1 + 2) ");
double num1 = input.nextInt();
char symbol = input.next().charAt(0);
double num2 = input.nextInt();

if (symbol == '+')
System.out.println("The result is : " + (num1 + num2));

else if (symbol == '-')
System.out.println("The result is : " + (num1 - num2));
else if (symbol == '/')
System.out.println("The result is : " + (num1 / num2 ));

else if (symbol == '*')
System.out.println("The result is : " + (num1 * num2));

else
System.out.println("invalid input");
}
}

Absalatreal
Автор

import java.util.Scanner;

class Check {
public static void main(String[] args) {

System.out.print("Enter num1 operator num2 (example: 2 * 3): ");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
char op = sc.next().charAt(0);
int b = sc.nextInt();

if(op == '+') {
System.out.println("The result is: " + (int) (a + b));
}
else if(op == '-') {
System.out.println("The result is: " + (int) (a - b));
}
else if(op == '*') {
System.out.println("The result is: " + a * b);
}
else {
System.out.println("The result is: " + (double) a / (double) b);
}

}
}

suswithcherry
Автор

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
System.out.print("Enter First Number: ");
int num1 = num.nextInt();
System.out.print("Choose Operator(+, -, *, /):");
String operator = num.next();
System.out.print("Enter Second Number: ");
int num2 = num.nextInt();


if (operator.equals("+"))
System.out.println("Addition of "+num1+" and "+num2+" is equal to "+(num1+num2));

else if (operator.equals("-"))
of "+num1+" from "+num2+" is equal to "+(num1-num2));

else if (operator.equals("*"))
of "+num1+" and "+num2+" is equal to "+(num1*num2));

else if (operator.equals("/"))
System.out.println("Division of "+num1+" by "+num2+" is equal to "+(num1/num2));



}


}

protoshorts
Автор

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("This are the operators (+, -, *, /) ");
System.out.print("Enter num1 operator num2 (example: 1 + 2): ");
int num1 = input.nextInt();
String operator = input.next();
int num2 = input.nextInt();

if (operator.equals("+"))
System.out.println("The result is: " + (num1 + num2));
else if (operator.equals("-"))
System.out.println("The result is: " + (num1 - num2));
else if (operator.equals("*"))
System.out.println("The result is: " + (num1 * num2));
else
if (num2 != 0 )
System.out.println("The result is: " + ((double)num1 / (double)num2));
else
System.out.println("Can't divide by zero");

input.close();
}

}

EricsonFolio