if-else Statement (Exercise 1)

if-else Statement (Exercise 1)

Neso Academy

4 года назад

54,120 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Purba Lingga
Purba Lingga - 14.11.2023 07:40

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

Ответить
Bakit ka nandito
Bakit ka nandito - 08.10.2023 12:40

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

Ответить
Absalat
Absalat - 07.10.2023 22:57

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

Ответить
Proto Shorts
Proto Shorts - 24.09.2023 16:33

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



}


}

Ответить
jradlr
jradlr - 18.08.2023 16:59

done

Ответить
kavyaa
kavyaa - 13.08.2023 19:10

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

Ответить
zainab fatima
zainab fatima - 18.07.2023 01:01

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?

Ответить
Caso Arcano
Caso Arcano - 04.07.2023 02:35

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

Thank you

Ответить
SIMP
SIMP - 28.04.2023 13:42

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

Ответить
Marcin Nalborczyk
Marcin Nalborczyk - 02.02.2023 02:16

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

Ответить
Isha Keshri
Isha Keshri - 08.01.2023 18:20

very very nice explained thnkuuu

Ответить
Akriti Singh
Akriti Singh - 22.11.2022 09:47

Thank you ,Sir

Ответить
Bhaskar Putta
Bhaskar Putta - 30.10.2022 08:18

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

Ответить
Yas Computing
Yas Computing - 21.05.2022 14:41

Wow, you are a machine thinking

Ответить
Give Ur Heart
Give Ur Heart - 01.04.2022 15:23

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

Ответить
ayushi mukherjee
ayushi mukherjee - 16.03.2022 13:45

package ayushi;

import java.util.Scanner;

public class Main {
public static void main(String[] arg) {
   Scanner intput = new Scanner(System.in);
   System.out.println("enter your number:");
   int a =intput.nextInt();
   int b =intput.nextInt();
   char op= intput.next().charAt(0);
   if (op=='+')
    System.out.println(a+b);
   else if (op=='-')
       System.out.println(a-b);
    else if (op=='*')
       System.out.println(a*b);
    else if (op=='/')
       System.out.println(a/b);

}
}

Ответить
Mohamed baqer Al-German
Mohamed baqer Al-German - 22.02.2022 20:29

double d1 =you.nextDouble();
char type = you.next().charAt(0);
double d2 =you.nextDouble();
if(type=='+'){
System.out.println(d1+d2);
}else if (type=='/'){
System.out.println(d1*d2);
}else if (type=='-') {
System.out.println(d1 - d2);
} else if (type=='%'){
System.out.println(d1%d2);
}
else if (type=='='){
System.out.println(1==1);

Ответить
BULLI RAJU
BULLI RAJU - 18.11.2021 09:54

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

Ответить
Jui Patil
Jui Patil - 13.09.2021 19:09

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

Ответить
badmood111
badmood111 - 05.03.2021 21:39

Hello, there is my solution, for anyone interested :D

public class Main {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number, an operator and another number (with spaces in between): ");
int x = scan.nextInt();
String o = scan.next();
int y = scan.nextInt();

switch (o){
case "+":
System.out.println(x + y);
break;
case "-":
System.out.println(x - y);
break;
case "*":
System.out.println(x * y);
break;
default:
double d1 = x;
double d2 = y;
System.out.println(d1 / d2);
}

}
}

Ответить
Catriel Torrez
Catriel Torrez - 04.08.2020 02:08

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

Ответить
HARSH
HARSH - 19.07.2020 09:32

not working

Ответить
Whatsap Whatsapp
Whatsap Whatsapp - 03.05.2020 18:58

thank you. am really enjoying this java programming

Ответить
Uttam Soren
Uttam Soren - 14.03.2020 15:36

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

Ответить