#how to make calculator console
1 messages ยท Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
do u know any java or is hello world all you've done?
i know python
and c#
its all
yes
so like you just want to type in 3+7 and have it output 10?
yes
so if u only have + - * / then its not as bad; unless u want to have 'nested' operations; then that typically requires a parse tree
ok
i just want like that
enter operation: +
enter first number: 5
enter second number: 4
results: 9
Detected code, here are some useful tools:
LOL
its code??
frrr
what is that language
i want to use it
its cause u put it in a block quote
ohhh
hmm
it didnt that time; maybe if it has + : or numbers; i dont recall the bot's code off the top of my head
sosig
bla bla
var test = "hello"
bruh
here code
and it dont detect
stupid bot
i dont know lol
You'll need to use a Scanner sc = new Scanner(System.in); to read console input
omg java stole "new" from c#!!!!!!
And then sc.nextLine(); to get a String of the next sent line from the console
new is a language construct in most things, especially C languages
c languages
what
derivations of the programming language C
C, C++, C#, Java are examples
the C family
the scanner isnt the operator
frrrrrrr
you'd have java Scanner sc = new Scanner(System.in); String operator = sc.nextLine();
oh that
idk what first number and second number are; and + concatenates strings
you'd have to write the if else or switch logic based on the operator
import java.util.Scanner;
class main{
public static void main(String args[]){
Scanner sc1 = new Scanner(System.in);
System.out.println("choose operator");
String operator = sc1.nextLine();
Scanner sc2 = new Scanner(System.in);
System.out.println("choose first number");
String firstnumber = sc2.nextLine();
Scanner sc3 = new Scanner(System.in);
System.out.println("choose first number");
String secondnumber = sc3.nextLine();
System.out.println(operator+firstnumber+secondnumber);
}
}
how to have int
thing
so if u had numbers a and b then a start would be
double result = switch(operator) {
case "+" -> a + b;
// TODO
default -> throw new RuntimeException("Invalid operator " + operator);
}```
but you'll have to parse the input, unless u want to ask them in that specific order; which i suppose you should get working first
I could explain every step of every bit of java there is to accomplish these tasks, but you should probably read introduction to java articles/websites online to learn the basics of java; particularly console IO operations in this 'project'
Integer.parseInt(stringvariable)
but you probably want to use doubles
floating points
so Double.parseDouble
note that there is no Double.tryParse(string, out x) in java, or other "try" methods. you'd have to make your own
i just realized u made a new scanner each time. you can re-use the same one
AAAAAAAAAAAAAAAAAAAAAAAAAAA
freacking java!!!!!
can i dont use cases
can i use if statement
๐ฅบ
yes but thats a bit longer to write for 4 discrete cases
and for java string comparisons you need to use .equals()
wth
import java.beans.PropertyEditorSupport;
import java.util.Scanner;
class main{
public static void main(String args[]){
Scanner sc1 = new Scanner(System.in);
System.out.println("choose operator");
String operator = sc1.nextLine();
Scanner sc2 = new Scanner(System.in);
System.out.println("choose first number");
String firstnumberstr = sc2.nextLine();
Double firstnumber = Double.parseDouble(firstnumberstr);
Scanner sc3 = new Scanner(System.in);
System.out.println("choose first number");
String secondnumberstr = sc3.nextLine();
Double secondnumber = Double.parseDouble(secondnumberstr);
if(operator=="+"){
Double result = firstnumber+secondnumber;
System.out.println(result);
}
}
}
here my code
also why it imports something
useless
like this beans
no operator.equals("+")
operator.equals("+");
Double result = firstnumber + secondnumber;
System.out.println(result);
like that
no the .equals expression goes in the if condition
@verbal sphinxcan you show the code when you finish just curious ๐
๐
hi admin
import java.util.Scanner;
class main{
public static void main(String args[]){
Scanner sc1 = new Scanner(System.in);
System.out.println("choose operator");
String operator = sc1.nextLine();
Scanner sc2 = new Scanner(System.in);
System.out.println("choose first number");
String firstnumberstr = sc2.nextLine();
Double firstnumber = Double.parseDouble(firstnumberstr);
Scanner sc3 = new Scanner(System.in);
System.out.println("choose first number");
String secondnumberstr = sc3.nextLine();
Double secondnumber = Double.parseDouble(secondnumberstr);
if(operator.equals("+")){
Double result = firstnumber + secondnumber;
System.out.println(result);
}
}
}
Detected code, here are some useful tools:
import java.util.Scanner;
class main {
public static void main(String args[] ) {
Scanner sc1 = new Scanner(System.in);
System.out.println("choose operator");
String operator = sc1.nextLine();
Scanner sc2 = new Scanner(System.in);
System.out.println("choose first number");
String firstnumberstr = sc2.nextLine();
Double firstnumber = Double.parseDouble(firstnumberstr);
Scanner sc3 = new Scanner(System.in);
System.out.println("choose first number");
String secondnumberstr = sc3.nextLine();
Double secondnumber = Double.parseDouble(secondnumberstr);
if (operator.equals("+")) {
Double result = firstnumber + secondnumber;
System.out.println(result);
}
}
}
Please use this format for posting code:
```java
// Example java program
int value = 5;
System.out.println(value);
```
Which results in:
// Example java program
int value = 5;
System.out.println(value);
For syntax highlighting, you have to add the name of the language after the three backticks, like ```java. Please make sure to use exactly this format, so no space between the backticks and the language name, and a newline before the code starts. If done right, the syntax highlighting will even be applied to your text as you type, before sending.
@quaint locust can you make the user repeat the input if its not + - / or *?
you could have that instead of crashing 
matt
helppppppp
ok no help need
switch(operator){
case "+":
System.out.println(firstnumber+secondnumber);
case "-":
System.out.println(firstnumber-secondnumber);
}
it doing every action
even if i choose +
it doing -
now go learn the basics of java before u continue :p
ok
you should use the newer switch expressions btw
idc
switch(operator) {
case "+" -> System.out.println(firstnumber+secondnumber);
case "-" -> System.out.println(firstnumber-secondnumber);
default -> throw new RuntimeException();
};
or better
int result = switch(operator) {
case "+" -> firstnumber+secondnumber;
case "-" -> firstnumber-secondnumber;
default -> throw new RuntimeException();
};
System.out.println(result);
ok