+ 1
How can i allow user to input any variable??
I made this java calculator..but as it doesn't have any Gui!. People might add either strings or doubles!...so how can i allow the code to add length of any variable he/she inputs https://code.sololearn.com/cQl68ed92a91/?ref=app
3 Réponses
+ 5
Lochlan Sc
Please remove your comment.
Users are expected to be friendly towards other users:
Personal attacks or disparagement of other users or their content is not tolerated. Every community member should be respectful and considerate, and assume that other people are also trying to make SoloLearn a great development resource. This means respecting opposing or differing opinions, beliefs, and conclusions; making an effort to listen to and understand others with whom you may disagree; and encouraging others to also be welcoming and respectful.
Please familiarise yourself with the rest of the guidelines found here:
https://www.sololearn.com/Content-Creation-Guidelines/
+ 3
1)there is a typo at line 14. You have put , after answer
use this - int firstnum, secondnum, answer;
2)don't use nextLine() for String input. Use next()
3)change the statement "Answer = (firststring + secondstring);" to "Answer = String.valueOf(Integer.parseInt(firststring) + Integer.parseInt(secondstring));"
4)In your code user can input either int or string, not double
Find the code below
import java.util.Scanner;
import java.util.*;
public class Program
{
public static void main(String[] args)
{
Scanner enternum = new Scanner(System.in);
double firstnum, secondnum, answer;
String firststring, secondstring,Answer;
System.out.println("enter your first number here");
firstnum = (enternum.nextDouble());
System.out.println("enter your second number here");
secondnum = (enternum.nextDouble());
answer = (firstnum + secondnum );
System.out.println("this is the right integer answer to your calculation " + answer);
firststring = (enternum.next());
secondstring = (enternum.next());
Answer = String.valueOf(Integer.parseInt(firststring) +
Integer.parseInt(secondstring));
System.out.println("this is the right string answer to your calculation " + Answer);
}
}
+ 1
😕😕just asked for help?.. why the language