if-else Statement (Exercise 1)

Поделиться
HTML-код
  • Опубликовано: 1 ноя 2024

Комментарии • 53

  • @marcinnalborczyk8576
    @marcinnalborczyk8576 Год назад +5

    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 Год назад +4

      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);
      ....

  • @BuhleHadebe-rc4xt
    @BuhleHadebe-rc4xt 2 месяца назад

    Thanks
    Please do more exercises

  • @uttamsoren4819
    @uttamsoren4819 4 года назад +2

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

  • @ishakeshri4849
    @ishakeshri4849 Год назад

    very very nice explained thnkuuu

  • @yasina63
    @yasina63 2 года назад

    Wow, you are a machine thinking

  • @AkritiSingh-sj2tv
    @AkritiSingh-sj2tv Год назад

    Thank you ,Sir

  • @bulliraju5439
    @bulliraju5439 3 года назад +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

  • @NakibAkash
    @NakibAkash 4 года назад +2

    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)

  • @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?

  • @juipatil9168
    @juipatil9168 3 года назад +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???

  • @kavyaa3163
    @kavyaa3163 Год назад

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

  • @axienator1
    @axienator1 Год назад +1

    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!");
    }

  • @bhaskarputta9343
    @bhaskarputta9343 2 года назад +1

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

  • @simponly0
    @simponly0 Год назад

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

  • @jradlr
    @jradlr Год назад

    done

  • @catrieltorrez2450
    @catrieltorrez2450 4 года назад +3

    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 года назад +2

      // 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

  • @casoarcano830
    @casoarcano830 Год назад +1

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

    • @injectred5014
      @injectred5014 8 месяцев назад

      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.

  • @Noivad
    @Noivad 3 месяца назад

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

  • @purbalingga4924
    @purbalingga4924 11 месяцев назад

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

    • @Ngr80-qy1od
      @Ngr80-qy1od 7 месяцев назад +2

      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

  • @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");
    }
    }

  • @protoshorts7579
    @protoshorts7579 Год назад +2

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

  • @suswithcherry9252
    @suswithcherry9252 9 месяцев назад

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

    }
    }

  • @lulululu1206
    @lulululu1206 3 месяца назад

    im sry

  • @harshtetGaming
    @harshtetGaming 4 года назад +1

    not working

    • @muhammadismail4779
      @muhammadismail4779 4 года назад +2

      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 года назад +1

      @@catrieltorrez2450 if we do so as you say, java interprets it as string concatenation

    • @Rektlele
      @Rektlele 3 года назад +2

      you guys need to leav space between num1 and operator and num2

    • @marwanmagdy4820
      @marwanmagdy4820 3 года назад

      @@Rektlele why ?

  • @EricsonFolio
    @EricsonFolio Месяц назад

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