Комментарии:
Sorry, what is the reason using charAt(0)? Can anyone explain it?
Ответить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!");
}
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");
}
}
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));
}
}
done
ОтветитьI get an error like inputmismatch exception ,what to do same code I have typed but it shows error
Ответить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?
ОтветитьSomebody please explain char op = s.next().charAt(0)
Thank you
Bro which app do you use for java this is not bluej
Ответить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);"
very very nice explained thnkuuu
ОтветитьThank you ,Sir
Ответитьcan anyone plese explain why we are giving spaces between the numbers i.e exmaple 2_+_3
why not we are entering 2+3
Wow, you are a machine thinking
Ответить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
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);
}
}
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);
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
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???
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);
}
}
}
why can he enter the n1+op+n2 in only one input?
Ответитьnot working
Ответитьthank you. am really enjoying this java programming
ОтветитьBeing in class 12 This video reminds why I need to start working ..
Ответить