Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Lately I've worked on simple calculator that can add, substract, multiply and divide.
public static void main(String arr[]){
double num1, num2;
String ans = null;
System.out.println("Calculator manu:");
System.out.println("\n"+"a for adding");
System.out.println("b for substracting");
System.out.println("c for multiplying");
System.out.println("d for dividing");
Scanner NumInput = new Scanner(System.in);
System.out.println("\n"+"Enter number one: ");
num1 = NumInput.nextDouble();
System.out.println("\n"+"Enter number two: ");
num2 = NumInput.nextDouble();
while(true)
{
Scanner AnsInput = new Scanner(System.in);
System.out.println("\n"+"Choose operation.");
String answer = AnsInput.next();
if(AnsInput.equals("1"))
{
answer = Double.toString(num1+num2);
System.out.println("\n"+"The sum of the first and second number is: "+answer);
}
else if(AnsInput.equals("2"))
{
answer = Double.toString(num1-num2);
System.out.println("\n"+"Subtraction of the first and the second number is: "+answer);
}
else if(AnsInput.equals("3"))
{
answer = Double.toString(num1*num2);
System.out.println("\n"+"The product of the first and second number is: "+answer);
}
else if(AnsInput.equals("4"))
{
answer = Double.toString(num1/num2);
System.out.println("\n"+"Ratio of the first and the second number is: "+answer);
}
}
}
But what if I want to wright program that works like an ordinary calculator; it adds, subtracts, multiplies, divides, ... ,but not only whit two but multiple numbers.
This can certainly be done! Although it requires substantial framework. However, if you want to use this type of simplistic code, what you need to do is store the numbers somewhere else, outside this function, and every two numbers you apply an operation to, you store it and use it as the first argument along the third you wanted to use. And so on. Does this make sense?
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm in college and am trying to learn Java but I'm having a bit of trouble with some code. The "if (totCreditsEarned >= 180)" is erroring in my Eclipse, specifically the part in parentheses, and it's unable to give any suggestions to fix it.
import java.util.Scanner;
public class CSC161lab6_1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Credits Earned: ");
String totCreditsEarned = scanner.nextLine();
System.out.println("Credits Earned is: " + totCreditsEarned);
if (totCreditsEarned >= 180) {
System.out.println("Wow!");
} else {
System.out.println("Oof!");
}
}
}
180 is an Integer. totCreditsEarned is a String.
You will need to convert totCreditsEarned to an Integer
if (Integer.valueOf (totCreditsEarned) >= 180)
...
Of course if you enter a non-integer value it will fail
The full compilation error is.
CSC161lab6_1.java:13: error: bad operand types for binary operator '>='
if (totCreditsEarned >= 180) {
^
first type: String
second type: int
1 error
It's telling you that you can't compare an int and a String using >=. You need two ints, so totCreditsEarned must be an int or converted to an int before you can compare them.
You can read it from the Scanner as an int. So no need to convert after.
int totCreditsEarned = scanner.nextInt();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Edit: I have found a way to work with CompareTo to help with this problem, but for some reason I cannot get the count down to work.
It's a negative number that needs to get more negative to meet the requirements, but I am missing something here. When I execute the down section it closes the program. So to me this means that I have something messed up and the program isnt seeing the problem and closing.
We are supposed to:
Ask the user for an integer then ask the user if he/she wants to count
up or down. Display a table of numbers where the first column contains
the counter, the second column contains the counter plus 10, and the
third column contains the counter plus 100. Make it so each number
takes up 5 spaces total.
If counting up, the first column should contain numbers 1 through the
user input; If counting down, the first column should contain numbers
-1 through the the negative of the user input;
Do user input validation on the word "up" and "down". Allow for any
case.
import java.util.Scanner;
public class ps1 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//Comparision string already declared
String up = "up";
String down = "down";
//initialize the counters sum
int sum = 0;
//ask the user for a number
System.out.println("Enter an ending value");
int num1 = keyboard.nextInt();
keyboard.nextLine();
System.out.println("Count up or down?");
String input = keyboard.nextLine();
while (input.equalsIgnoreCase(up) || input.equalsIgnoreCase(down)) {
System.out.println("Count up or down?");
input = keyboard.nextLine();
}
if (input.compareToIgnoreCase(up) == 0) {
if (num1 >= 0)
for (int c = 1; c <= num1; c++) {
sum = sum + c;
System.out.printf("%5d%5d%5d\n", c, c + 10, c + 100);
else
System.out.println("Up numbers must be positive");
if (input.compareToIgnoreCase(down) == 0) {
for (int c1 = -1; c1 <= num1; c1--) {
sum = sum + c1;
System.out.printf("%5d%5d%5d\n", c1, c1 + 10, c1 + 100);
}
}
}
}
}
I see you have figured out core logic. BTW, your code will not compile, there is a syntax error.
Your code would look like this:
print(a a+10 a+100)
I know that it's not valid syntax but you would be able to figure out the correct way to write the code.
To print data properly, you will need following:
https://dzone.com/articles/java-string-format-examples
I would recommend visualizing the output first. In your case, it would look like following: (_are spaces)
Enter an ending value: 2
Direction: Up
____1___11__101
____2___12__102
Also, think about error cases. What will happen in following:
Enter an ending value: -10
Direction: Up
Error: Improper data
You are allowing user to enter a positive num1 and count down using for (int counter1 = -1; counter1 >= num1; counter1--). This makes no sense as counter1 >= num1 resolves to -1 >= 1 which is never true. When direction is down the number must be negative and when direction is up the number must be positive.
You might need to loop until user provides a valid direction. Currently you go down for any input that is not up. A possible solution would be to:
String input;
do {
input = keyboard.nextLine();
} while (!input.equalsIgnoreCase("up") && !input.equalsIgnoreCase("down"));
Please use shorter variable names. counter1 is scoped just to the for loop block so call it i. It's easier to read.
Whichever editor you are using configure auto formatting :)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am currently taking a HS course on java, so I am a novice at java to say the least. Right now, for my own use, I am writing a program to take a 2 digit number, and then add up it and all the odd numbers before it till 1. I have the Scanner, calculating whether the number is odd or even, and the runner methods done already(the basic bit), but am a bit confused on the logic. I am trying to use a recursion, and do this code, but am a bit stuck. It would be helpful if you could point me in the right direction, without giving the whole code away. Thanks, - A Novice Programmer
public static void main(String[] args)
{
MathRecursion tester = new MathRecursion();
tester.Method1Runner();
}
public void Method1Runner()
{
GetIntM1();
OddOrEven();
System.out.println("\n\n");
}
public void GetIntM1()
{
Scanner kb = new Scanner(System.in);
System.out.print("\n\n\nEnter a 2 digit integer: ");
twoDig = kb.nextInt();
}
public void OddOrEven()
{
if (twoDig % 2 == 0)
{
//This is even method
Method1a(twoDig);
}
else
{
//This is odd method
Method1b(twoDig);
}
}
public int Method1a(int a)
{
//if (a = 1)
int result = 0;
while (a<=b)
{
result+=a;
a++;
}
System.out.println("The sum of all numbers is "+result);
}
You don't need recursion. The sum of the first n odd numbers is n*n.
The number of odd numbers before a number x is floor(x/2) or in Java (int) x/2 or if x is an int, just x/2.
So the expression in Java that gives you "a 2 digit number, and then add up it and all the odd numbers before it till 1" where the number is stored in int x is:
x + (x/2) * (x/2)
or simplified:
x + x*x/4
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is the code, where are the run time errors? I tried and tried to find it but I just could not, does anyone else see where these errors could be within the code?
public class HasErrors
{
public static void main(String [] args)
{
int x = 0;
int y = 0;
Scanner in = new Scanner("System.in");
x = in.readInt();
System.out.print("Please enter another integer: ");
x = in.readInt();
System.out.println("The sum is " + x + y);
}
}
If you're instantiating a new Scanner with System.in it shouldn't have quotes around it:
Scanner in = new Scanner(System.in);
See here.
Well if a user enters in somethign other than a number, say Hello World, then readInt() will throw an InputMismatchException. Also the Scanner class does not have a readInt function. I think you meant to put nextInt.
Along with the readInt errors already mentioned, X and Y will be concatenated in the println statement rather than added. This could be fixed by putting the x + y in parentheses.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I run it in JCreater but I keep getting compile errors. What did I do wrong? I saw someone set all the values to 0.0, but I don't see why I would need that.
import java.util.Scanner;
public class C2p3 {
public static void main(String[] args) {
double num1, num2, sum, difference, product; //setting variables
Scanner scan = new Scanner(System.in);
System.out.println("Enter first number:");
num1 = scan.nextDouble(); // not really sure what it does but I always use it and it works fine
System.out.println("Enter second number:");
num2 = scan.nextDouble();
sum = num1 + num2;
product = num1 * num2;
difference = num1 - num2;
System.out.println("Sum = " + sum);
System.out.println("Product = " + product);
System.out.println("Difference =" + difference);
}
}
Look at the Import statement carefully.
The statement
import java.util*;
should be
import java.util.*;
Extra info
If you really want to start writing good programs. I would suggest you make following changes to your code.
the statement import java.util.*; means that you are importing all the classes that are there inside the util package, But you are not using all of them in your code but the Scanner Class. So, I would suggest that replace the current import statement with import java.util.Scanner;.
Whenever you create an object of Scanner class. Call the method scan.close() when you dont need the object anymore. Else this might lead to resource leakage.
Please start using a proper IDE like eclipse, netbeans etc