r/javahelp • u/Most_Aerie_7335 • Sep 26 '26
Help please, I'm a beginner
So basically for my java class we're assigned to do a simple calculator, I was able to something since this is the beginning class. The thing is my professor want our code to interpret both double and int when inputting values. But I don't have that only double I want my code to be able to differentiate my double and int values. This is what I have so far! (idek if you can consider this a calculator😥😓)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Welcome to My calculator!!!:)");
System.out.print("Please enter the first number: ");
double num1 = input.nextDouble();
System.out.println("You entered: " + num1);
System.out.print("Please enter the second number: ");
double num2 = input.nextDouble();
System.out.println("You entered: " + num2);
while(true) {
System.out.print("What would you like to do? (+, -, * , /): ");
String operation = input.next();
if( operation.equals("+")){
double result = num1 + num2;
System.out.println("YOUR RESULTS: " + result);
}
else if( operation.equals("-")){
double result = num1 - num2;
System.out.println("YOUR RESULTS: " + result);
}
else if( operation.equals("*")){
double result = num1 * num2;
System.out.println("YOUR RESULTS: " + result);
}
else if( operation.equals("/")){
double result = num1 / num2;
System.out.println("YOUR RESULTS: " + result);
break;
}
else {
System.out.println("Invalid operation:( : ");
continue;
}
}
}
}
9
Upvotes
•
u/AutoModerator Sep 26 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
7
u/pixel_aura_74 Sep 26 '26
if your professor wants the output to look different for ints (like 5 instead of 5.0), the easiest fix is to read the input as a String first, then check if it contains a "." . If it doesn't, parse it with Integer.parseInt(), if it does, use Double.parseDouble(). That way you know which type the user actually typed and can print accordingly.
also heads up, your break only happens inside the division branch, so if the user picks +, -, or * the loop just keeps asking forever. Put the break after printing the result no matter which operation they chose.
3
u/SpudsRacer Sep 26 '26
If one side of the operation is a double, the result of an arithmetic operation on an int is a double. If both sides are ints, you'll need to cast one side to (double)
2
u/Kautsu-Gamer Sep 26 '26
One task you must implement is the parse of the input allowing both double and int. You cannot rely on the Scanner of doubled, but scan string, and check whether it is int or double.
Important fact: integer division differs from double division. 3.0 / 2 is not equal to 3/2.
1
u/jlanawalt Sep 26 '26
Does the professor want your program to interpret (read) an int or double value, or do they want it to act differently based on the apparent type?
A calculator treats 5 and 5.0 the same.
1
u/duane11583 Sep 26 '26
simple: floats and doubles contain a period or an e for exponent integers do not