Тёмный

if-else Statement (Exercise 1) 

Neso Academy
Подписаться 2,7 млн
Просмотров 56 тыс.
50% 1

Java Programming: if-else program in Java Programming
Topics Discussed:
1. Developing a calculator using the if-else statement.
Follow Neso Academy on Instagram: @nesoacademy (bit.ly/2XP63OE)
Contribute: www.nesoacademy...
Memberships: bit.ly/2U7YSPI
Books: www.nesoacademy...
Website ► www.nesoacademy...
Forum ► forum.nesoacad...
Facebook ► goo.gl/Nt0PmB
Twitter ► / nesoacademy
Music:
Axol x Alex Skrindo - You [NCS Release]
#JavaByNeso #JavaProgramming #ifelseStatement

Опубликовано:

 

1 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 53   
@marcinnalborczyk8576
@marcinnalborczyk8576 Год назад
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);"
@enginermis4626
@enginermis4626 Год назад
thanks
@mukeshprajapat4609
@mukeshprajapat4609 Год назад
It will not work because "==" operator checks if two variables refer to same object in memory. It doesn't check the content of string. For this you have to use .equals() method to check the content of two strings is equal or not. Eg. if (op.equals("+")) System.out.println(d1+d2); ....
@protoshorts7579
@protoshorts7579 Год назад
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("-")) System.out.println("Subtration of "+num1+" from "+num2+" is equal to "+(num1-num2)); else if (operator.equals("*")) System.out.println("Multiplication 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)); } }
@axienator1
@axienator1 11 месяцев назад
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!"); }
@juipatil9168
@juipatil9168 3 года назад
Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextDouble(Scanner.java:2564) at com.company.Calculator.main(Calculator.java:10) why???
@shahidaibrahim2917
@shahidaibrahim2917 3 года назад
enter the values using spaces in one line.like 5 + 7 5space+space7
@prakashp5348
@prakashp5348 2 года назад
@@shahidaibrahim2917 thanks dude'👍
@nickolecampbell9340
@nickolecampbell9340 2 года назад
@@shahidaibrahim2917 was literally going mad
@wissalelamoury473
@wissalelamoury473 Год назад
@@shahidaibrahim2917 thank you~
@bulliraju5439
@bulliraju5439 2 года назад
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
@bhaskarputta9343
@bhaskarputta9343 Год назад
can anyone plese explain why we are giving spaces between the numbers i.e exmaple 2_+_3 why not we are entering 2+3
@uttamsoren4819
@uttamsoren4819 4 года назад
Being in class 12 This video reminds why I need to start working ..
@NakibAkash
@NakibAkash 4 года назад
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? 1. Take operator as String and use switch statement(drive.google.com/file/d/1bqoD1KUEN_j6usCww1pudJ4A0eV92qFT/view?usp=sharing) 2. Take operator as Character and use switch statement-(drive.google.com/file/d/1XJzhM0q3iWDSVK3w8YzYh3G7pYTwpkqF/view?usp=sharing)
@whatsapwhatsapp662
@whatsapwhatsapp662 4 года назад
THX TOO FOR UR INTERVENTION. BLESS
@Sjot2416
@Sjot2416 2 года назад
Can u display output too
@suswithcherry9252
@suswithcherry9252 8 месяцев назад
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); } } }
@casoarcano830
@casoarcano830 Год назад
Somebody please explain char op = s.next().charAt(0) Thank you
@injectred5014
@injectred5014 7 месяцев назад
You can't read characters by using the s.next() method, so you have to use the string methods to get the first character from the string. Also, you can't use == in an if function if you are equalling something to a string but you can do it for characters, boolean, and numbers.
@EricsonFolio
@EricsonFolio 21 день назад
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(); } }
@Noivad
@Noivad 2 месяца назад
I wanted to know how to add the, “The result is: “ part 😩
@BuhleHadebe-rc4xt
@BuhleHadebe-rc4xt Месяц назад
Thanks Please do more exercises
@catrieltorrez2450
@catrieltorrez2450 4 года назад
why can he enter the n1+op+n2 in only one input?
@whatsapwhatsapp662
@whatsapwhatsapp662 3 года назад
sure! here you can even separate the inputs line by line
@whatsapwhatsapp662
@whatsapwhatsapp662 3 года назад
// Scanner scan= new Scanner(System.in); // System.out.println("enter number 1"); // int number1 = scan.nextInt(); // System.out.println("enter number 2"); // int number2 = scan.nextInt(); // // System.out.println(" Enter your operator of choice + - / * " ); // char operator = scan.next().charAt(0); if(operator== '+') { results=number1+number2; System.out.println(results); } else if (operator == '-') { results=(number1-number2); System.out.println(results); } else if(operator== '/') { results=(number1/number2); System.out.println(results); } else if(operator== '*'){ results=(number1*number2); System.out.println(results); } else System.out.println("INVALID OPERATOR : PLEASE TRY AGAIN"); } }
@swarnenduchakraborty3388
@swarnenduchakraborty3388 3 года назад
i think then we cant type cast the values of double when required
@Absalatreal
@Absalatreal Год назад
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"); } }
@lulululu1206
@lulululu1206 2 месяца назад
im sry
@zainabfatima6186
@zainabfatima6186 Год назад
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?
@kavyaa3163
@kavyaa3163 Год назад
I get an error like inputmismatch exception ,what to do same code I have typed but it shows error
@simponly0
@simponly0 Год назад
Bro which app do you use for java this is not bluej
@yasina63
@yasina63 2 года назад
Wow, you are a machine thinking
@ishakeshri4849
@ishakeshri4849 Год назад
very very nice explained thnkuuu
@purbalingga4924
@purbalingga4924 10 месяцев назад
Sorry, what is the reason using charAt(0)? Can anyone explain it?
@Ngr80-qy1od
@Ngr80-qy1od 6 месяцев назад
there's no nextChar() method in the Scanner class to read a single character in java. that's why we use string, and since charAt() returns the character at a specified index in the string, we put (0) to return the first character.
@jayapradeesh7204
@jayapradeesh7204 2 года назад
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=='+') System.out.println((int)(d1+d2)); else if(op=='-') System.out.println((int)(d1-d2)); else if(op=='*') System.out.println((int)(d1*d2)); else if(op=='/') System.out.println((d1/d2)); } } for me it show some error like input mismatch
@dharuni2536
@dharuni2536 Год назад
for me also
@zainabfatima6186
@zainabfatima6186 Год назад
Your public static void main method is written wrong. brackets are with the word string not args
@jradlr
@jradlr Год назад
done
@AkritiSingh-sj2tv
@AkritiSingh-sj2tv Год назад
Thank you ,Sir
@harshtetGaming
@harshtetGaming 4 года назад
not working
@muhammadismail4779
@muhammadismail4779 4 года назад
after each entry of the number press enter for example.. 2 * 3 6
@catrieltorrez2450
@catrieltorrez2450 4 года назад
@@muhammadismail4779 why can he enter the n1+op+n2 in only one input?
@deerureddy6520
@deerureddy6520 4 года назад
@@catrieltorrez2450 if we do so as you say, java interprets it as string concatenation
@Rektlele
@Rektlele 3 года назад
you guys need to leav space between num1 and operator and num2
@marwanmagdy4820
@marwanmagdy4820 3 года назад
@@Rektlele why ?
@whatsapwhatsapp662
@whatsapwhatsapp662 4 года назад
thank you. am really enjoying this java programming
Далее
if-else Statement (Exercise 2)
6:40
Просмотров 43 тыс.
If Else Statement In Java Tutorial #17
7:39
Просмотров 327 тыс.
switch vs. if-else in Java
4:45
Просмотров 86 тыс.
Java if statements 🚧【6 minutes】
6:12
Просмотров 153 тыс.
Nested if-else Statements in Java
5:26
Просмотров 73 тыс.
#12 If else in Java
12:59
Просмотров 156 тыс.