I've run a debugger and everything, but I can't find out whats wrong! I run the code, and it accepts a as an asnwer, but if i put b, it runs the code saying "This is not an A or a B"
import java.util.Scanner;
public class messingAround {
public static void main(String[] args){
Scanner ques = new Scanner(System.in);
String question;
System.out.println("Would you like to:");
System.out.println("A: Solve a math problem");
System.out.println("B: Display Pi");
System.out.println("Type A or B (no caps)");
question = ques.next();
if(!(question.equals("a")) || question.equals("b")){
System.out.println("Sorry that isn't an A or a B");
System.out.println("Try running the code again");
}else if(question.equals("a")) {
double fnum, snum, answer;
String type;
Scanner intString = new Scanner(System.in);
System.out.println("Enter your first number: ");
fnum = intString.nextDouble();
System.out.println("Enter your second number: ");
snum = intString.nextDouble();
System.out.println("What would you like to do? (+ - * /)");
type = intString.next();
if(type.equals("*")){
answer = fnum * snum;
System.out.println("The product is: " + answer);
}else if(type.equals("+")) {
answer = fnum + snum;
System.out.println("The sum is: " + answer);
}else if(type.equals("-")) {
answer = fnum - snum;
System.out.println("The difference is: " + answer);
}else if(type.equals("/")) {
answer = fnum / snum;
System.out.println("The dividend is: " + answer);
}
}else if(question.equals("b")) {
System.out.println("3.14159265359");
}
}
}
if(!(question.equals("a")) || question.equals("b")){
parens are a bit off. Try this:
if(!(question.equals("a") || question.equals("b"))){
Related
i have a calculator class and i want the operation input to be specific to these strings "+, -, /, *" if its not, i want to print a message to enter the operator again.
and please if someone would lessen my code please do it. thanks
here is my code
import java.util.Scanner;
public class Calculator1 {
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
String operation;
Double fnum, lnum, answer;
System.out.println("Enter first Number: ");
while (!num.hasNextDouble())
{
num.next();
System.out.print("Enter first Number: ");
}
fnum = num.nextDouble();
System.out.println("Enter Operation: ");
//HERE IM BEING CONFUSED
while (!num.equals("+, -, /, *"))
{
num.next();
System.out.print("Enter Operator: ");
}
operation = num.next();
//End
System.out.println("Enter Second Number: ");
while (!num.hasNextDouble())
{
num.next();
System.out.print("Enter Second Number: ");
}
lnum = num.nextDouble();
switch (operation) {
case "+":
answer = fnum + lnum;
System.out.print("Equals= " + answer);
break;
case "-":
answer = fnum - lnum;
System.out.print("Equals= " + answer);
break;
case "*":
answer = fnum * lnum;
System.out.println("Equals= " + answer);
break;
case "/":
answer = fnum / lnum;
System.out.println("Equals= " + answer);
break;
default:
System.out.println("wrong operator");
break;
}
}
}
This is a way to use operators with SWITCH sentence.
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
double v1,v2;
String v3;
System.out.print("Enter first number: ");
v1 = num.nextDouble();
System.out.print("Enter second number: ");
v2 = num.nextDouble();
System.out.print("Enter operation [+] [-] [*] [/]: ");
v3 = num.next();
System.out.println( ("+".equals(v3) ) ? Operators.ADD.calculate(v1, v2) : "....");
}
public enum Operators {
ADD;
double calculate(double x, double y) {
switch (this) {
case ADD:
return x + y;
default:
throw new AssertionError("Unknown operations " + this);
}
}
}
I'm trying to create a simple calculator program on Java as a simple first project as I learn Java.
Problem: After I run the program and after doing my calculation, I wanted to give the user the option on whether they want to end the program or do some more calculations. for some reason the program is not using the (if) statements that I have placed in it, it seems to skip it and end my program without allowing the user to input his choice at the end.
I'm really sorry if my question is not clear, I couldn't find a solution for my problem online, and I do apologize if my code looks really messy.
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double fnum, snum, answer;
String choice, name, in;
System.out.println("Hello, whats your name?");
name = input.nextLine();
System.out.println("");
System.out.println("Oh so your name is " + name + "!");
System.out.println("");
System.out.println("This program is a calculator that will do simple calculation of two numbers");
System.out.println("There are four different options to choose from:");
boolean running = true;
CAL:
while (running) {
System.out.println("type a for Addition, b for subtraction, c for multiplication and d for division");
System.out.println("Then press enter!");
choice = input.nextLine();
while (!choice.equals("a") &&
!choice.equals("A") &&
!choice.equals("b") &&
!choice.equals("B") &&
!choice.equals("c") &&
!choice.equals("C") &&
!choice.equals("d") &&
!choice.equals("D")) {
System.out.println("Wrong choice, Please try again");
System.out.println("type a for Addition, b for subtraction, c for multiplication and d for division");
choice = input.nextLine();
}
if (choice.equals("a") || choice.equals("A")) {
System.out.println(name +" You have chosen Addition");
System.out.println("Type the first number:");
fnum = input.nextDouble();
System.out.println("Type the second number:");
snum = input.nextDouble();
System.out.println("your answer is:");
Addition addy = new Addition(fnum,snum);
System.out.println(addy.getans());
}
if (choice.equals("b") || choice.equals("B")) {
System.out.println(name +" You have chosen Subtraction");
System.out.println("Type the first number:");
fnum = input.nextDouble();
System.out.println("Type the second number:");
snum = input.nextDouble();
answer = fnum - snum;
System.out.println("your answer is:"
+ answer);
}
if (choice.equals("c") || choice.equals("C")) {
System.out.println(name +" You have chosen Multiplication");
System.out.println("Type the first number:");
fnum = input.nextDouble();
System.out.println("Type the second number:");
snum = input.nextDouble();
answer = fnum * snum;
System.out.println("your answer is:"
+ answer);
}
if (choice.equals("d") || choice.equals("D")) {
System.out.println(name +" You have chosen Addition");
System.out.println("Type the first number:");
fnum = input.nextDouble();
while (fnum == 0) {
System.out.println("invalid try again!");
fnum = input.nextDouble();
}
System.out.println("Type the second number:");
snum = input.nextDouble();
answer = fnum / snum;
System.out.println("your answer is:"
+ answer);
}
System.out.println("");
System.out.println("Thank you " + name + " for using this simple calculator :)");
System.out.println("If you would like to try again press a the press Enter, if you wish to exit press any botton and then press enter");
in = input.nextLine();
if (in.equals("a")) {
System.out.println("");
System.out.println("Thank you, please choose again");
System.out.println("");
} else {
System.out.println("Thank you and goodbye");
break;
}
}
}
}
It's a simple problem caused by nextInt/Double/etc behaviour.
Those methods don't read the following new-line character so the next nextLine will always return an empty string (the rest of the current line).
Try to change those (e.g. the addition case) with Double.parseDouble(input.nextLine());
if (choice.equals("a") || choice.equals("A")) {
System.out.println(name + " You have chosen Addition");
System.out.println("Type the first number:");
fnum = Double.parseDouble(input.nextLine());
System.out.println("Type the second number:");
snum = Double.parseDouble(input.nextLine());
System.out.println("your answer is:");
Addition addy = new Addition(fnum, snum);
System.out.println(addy.getans());
}
you can notice debugging that the last in = input.nextLine(); called to ask for user input will always be empty string.
I'm writing this program where the user takes a math test. The problem Im now facing and have been trying to fix is how to display the final score at the end of the test.
I have a counter and the score increments for every correct answer, but how do I (at the end) display the final/total score? Any help is appreciated.
This is one of three classes btw.
import java.util.Scanner;
public class Questions {
Scanner scan = new Scanner(System.in);
int answer = 0;
int point = 0;
String correct = "Correct";
public void pointers(){
point++;
System.out.println(point + " point added to total score");
}
public void totalPoints(){
pointers();
}
public void showQuestions(){
System.out.println("\n--------------\nQuestion #1: 1+1 = ? ");
answer = scan.nextInt();
if(answer == 2){
System.out.println(correct);
pointers();
}else{
System.out.println("Wrong!");
}
System.out.println("\nQuestion #2: 340-23 = ? ");
answer = scan.nextInt();
if(answer == 317){
System.out.println("Correct!");
pointers();
}else{
System.out.println("Wrong!");
}
System.out.println("\nQuestion #3: 900/3 = ? ");
answer = scan.nextInt();
if(answer == 300){
System.out.println("Correct!");
pointers();
}else{
System.out.println("Wrong!");
}
System.out.println("\nQuestion #4: 23*2 = ? ");
answer = scan.nextInt();
if(answer == 46){
System.out.println("Correct!");
pointers();
}else{
System.out.println("Wrong!");
}
System.out.println("\nQuestion #5: -4+6 = ? ");
answer = scan.nextInt();
if(answer == 2){
System.out.println("Correct!");
pointers();
}else{
System.out.println("Wrong!");
}
}
}
The problem is that you're trying to get the input first and then you're asking for the age. When you type keyboard.nextInt() it is expecting an input immediately. So the problem is here:
*int age = keyboard.nextInt();
* System.out.println("Age: " + age);
*age = keyboard.nextInt();
My suggestion is to just remove the first keyboard.nextInt():
Example:
System.out.println("\nName: " + name);
name = keyboard.nextLine();
System.out.println("Age: " + age);
final int age = keyboard.nextInt();
and that's about it. Just be careful where you place your method calls, e.g. they should be after your println()'s.
No idea what you try to achieve, but
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
final Scanner keyboard = new Scanner(System.in);
System.out.println("Hello, it is time to take a math test.\nFirst we need you to enter some information about yourself.");
System.out.print("\nEnter name: ");
final String name = keyboard.nextLine();
System.out.print("Enter Age: ");
final int age = keyboard.nextInt();
keyboard.close();
System.out.println("Your personal information: " + name + " of " + age + " years old.");
}
}
output:
C:\Temp\123431>java -classpath .;Main.class Main
Hello, it is time to take a math test.
First we need you to enter some information about yourself.
Enter name: ttt
Enter Age: 321
Your personal information: ttt of 321 years old.
C:\Temp\123431>java -classpath .;Main.class Main
I'm trying to get the user to input two numbers, and if an invalid character is entered I want it the program to ask the user to enter a number again. Why is my method of doing this incorrect?
import java.util.InputMismatchException;
import java.util.Scanner;
public class Calculator {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
double answer = 0;
boolean goodInput = false;
do {
try{
System.out.println("Enter your first number:");
double firstNum = input.nextDouble();
System.out.println("Enter your second number:");
double secondNum = input.nextDouble();
System.out.println("Which operation would you like to use?");
System.out.println("For addition, type add");
System.out.println("For subtraction, type subtract");
System.out.println("For divison, type divide");
System.out.println("For multiplication, type multiply");
String operation = input.next();
goodInput = true;
if (operation.equals("add")) {
answer = firstNum + secondNum;
System.out.println(firstNum + " + " + secondNum + " = "+ answer);
}
if (operation.equals("subtract")) {
answer = firstNum - secondNum;
System.out.println(firstNum + " - " + secondNum + " = "+ answer);
}
if (operation.equals("divide")) {
answer = firstNum / secondNum;
System.out.println(firstNum + " / " + secondNum + " = "+ answer);
}
if (operation.equals("multiply")) {
answer = firstNum * secondNum;
System.out.println(firstNum + " x " + secondNum + " = "+ answer);
}
}catch(InputMismatchException e) {
System.out.println("Thats not a number, enter a number:");
}
}while(goodInput = false);
}
}
} while(goodInput = false);
is an assignment due to the = as opposed to ==. Thus, the loop condition is always evaluated as false and the loop does not continue.
You can either make the condition:
} while (goodInput == false);
which reads as "while goodInput is false"
} while (goodInput != true);
which reads as "while goodInput is not true"
or
} while (!goodInput);
which reads as "while not goodInput"
The last one is generally idiomatic for Java programmers and is preferred.
following means you are assigning false to goodInput and then entire expression gets evaluted as false
so
change
}while(goodInput = false);
to
}while(!goodInput);
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I am new to programming and would appreciate some help. The slightest bit of insight would be highly appreciated.
I have an issue with the following code. The program emulates a calculator but currently my main focus is on if and else if statements. The issue is that no matter what the user selects, the program will always add the two numbers i.e. 'number1' and 'number2' in the code
import java.util.*;
public class Input
{
private Scanner input;
public Input()
{
input = new Scanner(System.in);
}
public void calculation()
{
double number1, number2, answer;
String A, B, C, D, E;
String option;
A = "A"; B = "B"; C = "C"; D = "D"; E = "E"; //initialising the strings
System.out.println("add - option A \t (if your option is A, insert 'A')");
System.out.println("multiply - option B");
System.out.println("subtract - option C");
System.out.println("divide - option D");
System.out.println("power - option E (1st number - 'X' & 2nd number - 'n' following X^n)");
System.out.println("Remember Java is case sensitive, therefore, inserting 'a' as 'A' won't work");
System.out.println();
System.out.println("Insert your first number: ");
number1 = input.nextDouble();
System.out.println("Insert your second number: ");
number2 = input.nextDouble();
System.out.println("Choosing option: ");
option = input.next();
if(A == A)
{
answer = number1 + number2;
System.out.println("Your answer is: " + answer);
}
else if(B == B)
{
answer = number1 * number2;
System.out.println("Your answer is: " + answer);
}else if(C == C)
{
answer = number1 - number2;
System.out.println("Your answer is: " + answer);
}else if(D == D)
{
answer = number1 / number2;
System.out.println("Your answer is: " + answer);
}else if(E == E)
{
answer = Math.pow(number1, number2);
System.out.println("Your answer is: " + answer);
}else
{
System.out.println("Choose a suitable option");
}
}
}
Youre getting the selected option from user input in option = input.next(); line and than your not using it in your if statements.
Instead of if(A == A) use if(option.equals(A)) and so on for other cases.