#how to make calculator console

1 messages ยท Page 1 of 1 (latest)

verbal sphinx
#

^

gaunt lotusBOT
#

<@&987246399047479336> please have a look, thanks.

gaunt lotusBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

quaint locust
#

do u know any java or is hello world all you've done?

verbal sphinx
#

and c#

#

its all

quaint locust
#

ok C# is similar

#

python is LUL

verbal sphinx
quaint locust
#

so like you just want to type in 3+7 and have it output 10?

verbal sphinx
#

yes

quaint locust
#

so if u only have + - * / then its not as bad; unless u want to have 'nested' operations; then that typically requires a parse tree

verbal sphinx
#

ok

#

i just want like that

#
enter operation: +
enter first number: 5
enter second number: 4
results: 9
gaunt lotusBOT
verbal sphinx
#

its code??

#

frrr

#

what is that language

#

i want to use it

quaint locust
#

its cause u put it in a block quote

verbal sphinx
#

ohhh

quaint locust
#

hmm

verbal sphinx
#

detected code

#
sausag
quaint locust
#

it didnt that time; maybe if it has + : or numbers; i dont recall the bot's code off the top of my head

verbal sphinx
#
sosig
bla bla
#
var test = "hello"
#

bruh

#

here code

#

and it dont detect

#

stupid bot

quaint locust
#

i dont know lol

verbal sphinx
#

ok so

#

how to make calculator

quaint locust
#

You'll need to use a Scanner sc = new Scanner(System.in); to read console input

verbal sphinx
quaint locust
#

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

quaint locust
#

derivations of the programming language C

#

C, C++, C#, Java are examples

#

the C family

verbal sphinx
#

like that

#

like that

quaint locust
#

the scanner isnt the operator

verbal sphinx
quaint locust
#

you'd have java Scanner sc = new Scanner(System.in); String operator = sc.nextLine();

verbal sphinx
#

isnt it storing thing

#

that it got

#

what i done not right

quaint locust
#

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

verbal sphinx
#

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

quaint locust
#

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'

verbal sphinx
#

how to get int

#

java hard ๐Ÿ˜ญ

#

c# easier

quaint locust
#

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

quaint locust
verbal sphinx
#

freacking java!!!!!

#

can i dont use cases

#

can i use if statement

#

๐Ÿฅบ

quaint locust
#

yes but thats a bit longer to write for 4 discrete cases

#

and for java string comparisons you need to use .equals()

verbal sphinx
#

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

quaint locust
#

i said u gotta use .equals on strings

#

strings are objects

verbal sphinx
#

Double result = firstnumber+secondnumber.equals();

#

this

quaint locust
#

no operator.equals("+")

verbal sphinx
#

operator.equals("+");
Double result = firstnumber + secondnumber;
System.out.println(result);

#

like that

quaint locust
#

no the .equals expression goes in the if condition

verbal sphinx
#

OMG

#

THXXXXXX

#

MY FIRST CALCULATORRRRRRRRRRRRRRR

#

OMG

#

OMG

#

GOMGGG

#

g

#

g

#

g

grave hedge
#

@verbal sphinxcan you show the code when you finish just curious ๐Ÿ˜„

uncut lark
#

๐Ÿ‘Œ

verbal sphinx
verbal sphinx
# grave hedge <@598858786949824542>can you show the code when you finish just curious ๐Ÿ˜„

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

gaunt lotusBOT
# verbal sphinx import java.util.Scanner; class main{ public static void main(String args[]...

Detected code, here are some useful tools:

Formatted code
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.

verbal sphinx
#

ok bye

#

gys

grave hedge
#

@quaint locust can you make the user repeat the input if its not + - / or *?

quaint locust
#

you could have that instead of crashing LUL

verbal sphinx
#

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 -

rose trail
#

no breaks were used :p

#

case <condition> :
do something;
break;

#

@verbal sphinx

verbal sphinx
#

ok me try

#

OMG THXXXXXXXXXX

rose trail
#

now go learn the basics of java before u continue :p

magic shore
verbal sphinx
#

idc

magic shore
# verbal sphinx switch(operator){ case "+": System.out.println(first...
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);
verbal sphinx
#

ok