I'm having a task to solve in which I have to find the biggest sum between the digits of 3nums integer.
I've decided to do it with "switch case", because I am still a newbie.
But, unfortunately when I run it, it skips directly out after the loop.
Here's my code:
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
int nums = Integer.parseInt(input);
int firstNum, secondNum, thirdNum, sumNums, sumNums2 = 0;
firstNum = Character.getNumericValue(input.charAt(0));
secondNum = Character.getNumericValue(input.charAt(1));
thirdNum = Character.getNumericValue(input.charAt(2));
switch (nums) {
case 1:
sumNums = firstNum + secondNum + thirdNum;
sumNums2 = sumNums;
case 2:
sumNums = firstNum + secondNum * thirdNum;
if (sumNums > sumNums2) {
sumNums2 = sumNums;
}
case 3:
sumNums = firstNum * secondNum + thirdNum;
if (sumNums > sumNums2) {
sumNums2 = sumNums;
}
case 4:
sumNums = firstNum * thirdNum + secondNum;
if (sumNums > sumNums2) {
sumNums2 = sumNums;
}
case 5:
sumNums = firstNum * secondNum * thirdNum;
if (sumNums > sumNums2) {
sumNums2 = sumNums;
break;
}
}
System.out.println(sumNums2);
}
}
Thanks in advance!
The issue is that you are switching on the variable nums, which you are expecting a 3 digit number. Any 3 digit number entered will not match any of your single digit case statements, so it falls out of the switch all together.
It appears that you are intending to have the user first input a singer digit number 1 thru 5 representing an operation to match one of the cases - you never do that.
Also, you should first print out via a System.out.println() statement what input you are asking for before scanning for input.
The code looks odd:
switch (nums) { // 111, 257 wrong input, no match
// suggest to add default, so you can debug the problem
}
Hello there fellow coders. Just a noob here that is currently stuck in a pickle.
I am coding a math training program, and am needing to loop back to my main menu.
There is an infinite loop i am having trouble getting out of. If the user keeps answering the correct answer, it just keeps on asking for another problem. Selecting yes will generate a new answer and will keep doing so (infinite). Selecting no will terminate the program.
How do i make is so if they select no, it takes the user back to the main menu?
Also, how to make it so if they select yes, the problem only loops a new equation at max 10 times before taking the user back to my main menu.
This is my Code:
import java.util.Scanner;
import java.util.Random;
public class MathPracticeProgram
{
public static void main(String[] args)
{
//Scenario: Make a practice program for Addition, Subtraction, Multiplication, and Division. Include exit option as well.
Random rand = new Random ();
Scanner scan = new Scanner (System.in);
int menu = (5);
int num1, num2, user_answer, total;
System.out.println("Math Practice Porgram");
System.out.println("Please Input Your Choice via the number\n");
System.out.println("1. Addition\n2. Subtraction\n 3. Multiplication\n 4. Division\n Exit Program by entering 0");
System.out.println("\nYour choice:");
menu = scan.nextInt();
System.out.println("\n");
//Menu Selection
if (menu <= 5 || menu > 0)
{
switch(menu)
{
//Exit
case 0:
{
System.out.println("Thank you for using my math practice program.");
}
break;
//Addition
case 1:
{
System.out.println("You have selected Addition");
String user_again;
do
{
num1 = rand.nextInt(99);
num2 = rand.nextInt(99);
int counter = 0;
do
{
System.out.println("New solution to solve: ");
System.out.println(num1 + " + " + num2 + " = ");
user_answer = scan.nextInt();
System.out.println("\n");
total = num1 + num2;
if(user_answer != total)
{
System.out.println("Incorrect...");
counter++;
}
}while (user_answer != total && counter < 3);
System.out.println("Correct! Do you want another problem? yes or no: ");
user_again = scan.next();
}while(user_again.equalsIgnoreCase("yes"));
System.out.println("\nChanging to Main Menu\n");
}
break;
//Subtraction
case 2:
{
}
break;
//Multiplication
case 3:
{
System.out.println("Multiplication");
}
break;
//Division
case 4:
{
System.out.println("Division");
}
break;
}
}
}
}
You probably understand that I am a beginner, and I know that we aren't really liked by the community.
I made a multi purpose calculator a while back and now I want to expand it. In this question I will be focusing only on one class.
import java.util.Scanner;
public class Calculator {
public static void calcMenu(Scanner input){
Scanner oper = new Scanner(System.in);
System.out.println("Please input the First number:");
double anum = input.nextDouble();
System.out.println("Please input on of the following operations:");
System.out.println("+");
System.out.println("-");
System.out.println("*");
System.out.println("/");
String equ = oper.nextLine();
System.out.println("Please input the Second number:");
double bnum = input.nextDouble();
switch (equ){
case "+":
System.out.println(anum + bnum);
break;
case "-":
System.out.println(anum - bnum);
break;
case "*":
System.out.println(anum * bnum);
break;
case "/":
System.out.println(anum / bnum);
break;
}
}
}
In this Java class, the program can solve equations only with two numbers. I would like to make it like in a standard calculator, where you can input the numbers as much as you want. I would like to do it until the user types something like "done" and the application will return to the main menu.
This is probably a very nooby question but please help. And if you want to see the whole application: here's the link
This will help you out! :)
import java.util.InputMismatchException;
import java.util.Scanner;
public class Calculator {
private static double answer;
private static boolean done = false;
private static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
try {
new Calculator().calcExpression();
} catch (InputMismatchException e) {
System.out.println("An error occurred. ");
}
}
private void calcExpression() throws InputMismatchException {
System.out.println("Enter Your Expression Here:");
System.out.print("Num: ");
double firstNum = in.nextDouble();
in.nextLine();
while (!done) {
System.out.print("Operator: ");
String operand = in.nextLine();
if (operand.equals("=")) {
break;
}
System.out.print("Num: ");
double secondNum = in.nextDouble();
in.nextLine();
calculate(firstNum, operand, secondNum);
firstNum = answer;
}
System.out.printf("Answer is %.2f", answer);
}
private void calculate(double num1, String equ, double num2) {
switch (equ)
{
case "/":
answer = (num1 / num2);
break;
case "*":
answer = (num1 * num2);
break;
case "+":
answer = (num1 + num2);
break;
case "-":
answer = (num1 - num2);
break;
case "=":
done = true;
break;
}
}
}
first time posting here and I'd like some help , I started learning java some days ago and I tried to make a very simple calculator. Here is the code :
import java.util.Scanner;
public class Calc {
public static void main(String[] args) {
while (true) {
Scanner input = new Scanner(System.in);
double input1, input2;
System.out.println("Type your calculation below");
input1 = input.nextDouble();
String operator = input.next();
input2 = input.nextDouble();
switch (operator) {
case "+":
System.out.println(input1 + input2);
break;
case "-":
System.out.println(input1 - input2);
break;
case "*":
System.out.println(input1 * input2);
break;
case "/":
System.out.println(input1 / input2);
break;
}
}
}
}
When I run this I have to type a number press space and then type the other number like this : 15 + 15. Can I somehow make it so that I don't have to press space every time? So I can type it like this : 15+15.
Also if you have any tips or if you see any mistakes I'd be happy to hear your opinion.
I would recommend using the delimiter for Scanner see below:
Scanner s = new Scanner(foo);
s.useDelimiter(" ");
// do input stuff here
I want to write test case for calculator program in junit. I am new to junit environment. I can write tests for other programs but kind of stuck with testing for switch() case.
I really want to know how to to do it.
thanks in advance
here is my program
import java.util.Scanner;
//This program performs basic math operations such as :- +,-,*,/
public class Calculator
{
public static void main(String[] args)
{
double number1, number2;
String Mathoperation;
Scanner scannerObject = new Scanner(System.in);
System.out.println("Enter first number");
number1 = scannerObject. nextDouble();
System.out.println("Enter second number");
number2 = scannerObject. nextDouble();
Scanner UserInput = new Scanner(System.in);
System.out.println("\nHere are your options:");
System.out.println("\n1. Addition, 2. Subtraction, 3. Divison, 4. Multiplication");
Mathoperation = UserInput.next();
switch (Mathoperation)
{
case "1":
System.out.println("Your answer is " + (number1 + number2));
break;
case "2":
System.out.println("Your answer is " + (number1 - number2));
break;
case "3":
System.out.println("Your answer is " + (number1 / number2));
break;
case "4":
System.out.println("Your asnwer is " + (number1 * number2));
break;
default:
System.out.println("");
}
}
}
The first step to writing unit tests is writing testable code. At this point your code isn't very testable, which is to say you can test it but it won't be the best tests. Lets have a look at why.
The first reason is that you have all your code in your main function. You usually want to avoid this. There are multiple reasons for this, but the two biggest in my opinion are readability and reusability. Normally you would want to put things into different classes, but for the sake of time we will simply put your math switch into its own function.
import java.util.Scanner;
//This program performs basic math operations such as :- +,-,*,/
public class Calculator
{
public static void main(String[] args)
{
double number1, number2;
String Mathoperation;
Scanner scannerObject = new Scanner(System.in);
System.out.println("Enter first number");
number1 = scannerObject. nextDouble();
System.out.println("Enter second number");
number2 = scannerObject. nextDouble();
Scanner UserInput = new Scanner(System.in);
System.out.println("\nHere are your options:");
System.out.println("\n1. Addition, 2. Subtraction, 3. Divison, 4. Multiplication");
Mathoperation = UserInput.next();
doMath(Mathoperation, number1, number2)
}
public double doMath(String Mathoperation, double number1, double number2){
switch (Mathoperation)
{
case "1":
System.out.println("Your answer is " + (number1 + number2));
break;
case "2":
System.out.println("Your answer is " + (number1 - number2));
break;
case "3":
System.out.println("Your answer is " + (number1 / number2));
break;
case "4":
System.out.println("Your asnwer is " + (number1 * number2));
break;
default:
System.out.println("");
}
}
}
Ok cool, so now we have our own method for doing our mathematic operations. One issue tho. This isn't a unit in of itself, this is actually an integration between addition and our math operation. What this means is, to test, we would need to test each combination of arithmetic (add, sub, divide, multiply) as well as if the method selects the right operator. Thats alot more work then we want, and isn't a very good test, so how do we fix this? Well simple, we break it down further
import java.util.Scanner;
//This program performs basic math operations such as :- +,-,*,/
public class Calculator {
//Code for use input somewhere here
public double doMath(String Mathoperation, double number1, double number2){
switch (Mathoperation)
{
case "1":
System.out.println("Your answer is " + add(number1, number2));
break;
case "2":
System.out.println("Your answer is " + sub(number1, number2));
break;
case "3":
System.out.println("Your answer is " + div(number1, number2));
break;
case "4":
System.out.println("Your answer is " + mul(number1, number2));
break;
default:
System.out.println("");
}
}
public double add(double number1, double number2){
return number1 + number2;
}
public double sub(double number1, double number2){
return number1 - number2;
}
public double mul(double number1, double number2){
return number1 * number2;
}
public double div(double number1, double number2){
return number1 / number2;
}
}
Now our units are broken down much more easily. From here you would write test for your add, sub, div, and mul functions. Test these out well to insure that they function as expected. Then write tests for the doMath function to make sure it passes the values to the right operation. In this way when something fails you know exactly where its failing and what the issue is, as oppose to having to guess. You will know if doMath has an issue, or if one of your other math methods is the problem. It also allows you to more easily reuse your methods for other things, so win-win
Consider putting the input, calculation and output into (non static) methods, which will make testing of the logic possible :
//This program performs basic math operations such as :- +,-,*,/
public class Calculator {
double number1, number2;
String Mathoperation;
Calculator()
{
//you need to remove input and calculation logic from constructor,
//to avoid running it when constructing this object for testing.
}
public void run() {
getInput();
double result = doMath(Mathoperation, number1, number2 );
System.out.println("Your answer is " + result );
}
private void getInput() {
Scanner scannerObject = new Scanner(System.in);
System.out.println("Enter first number");
number1 = scannerObject. nextDouble();
System.out.println("Enter second number");
number2 = scannerObject. nextDouble();
Scanner UserInput = new Scanner(System.in);
System.out.println("\nHere are your options:");
System.out.println("\n1. Addition, 2. Subtraction, 3. Divison, 4. Multiplication");
Mathoperation = UserInput.next();
}
/**
*#param mathoperation
*/
public double doMath(String mathoperation, double number1, double number2) {
switch (mathoperation)
{
case "1":
return (number1 + number2);
case "2":
return (number1 - number2);
case "3":
return (number1 / number2);
case "4":
return (number1 * number2);
default:
throw new IllegalStateException("mathoperation is not valid");
}
}
public static void main(String[] args)
{
Calculator calc = new Calculator();
calc.run();
}
}
Here is a very simple JUnit test example:
public class CalculatorTest {
/**
* Test method for {#link Calculator#doMath(java.lang.String, double, double)}.
*/
#Test
public void testDoMath() {
Calculator calc = new Calculator();
assertTrue(calc.doMath("1", 5, 7) == 12.0);
assertTrue(calc.doMath("2", 5, 7) == -2.0);
assertTrue(calc.doMath("3", 5, 7) == (5.0/7.0));
assertTrue(calc.doMath("4", 5, 7) == (5.0*7.0));
}
}
To write unit tests, you usually have to have pieces of the API that you want to test. In your case, everything is in the main method which gathers user input, so it's hard to test. You should extract something like a double performOperation(number, number, operation) method which returns the answer. Then you can write JUnit tests with different inputs to this method.