Question

I was wanting to know how would I call my add class through my if statement....

I was wanting to know how would I call my add class through my if statement.

Here is my class:

import java.util.Scanner;

public class Main
{
   public static void main(String[] args)
   {
       int num1 = 0;
       int num2 = 0;
       int answer = 0;
       int userInput = 0;
       Scanner sc = new Scanner(System.in);
      
      
       System.out.println("Hello, this is a program where you can choose to add or subtract two numbers.");
      
       System.out.println("Would you like to subtract or add?");
       System.out.println("1 for Add || 2 for Subtract.");
      
       if(userInput == 1)
       {
           add(num1, num2, sc, answer);
       }
      
  
  
  
   }

   public static void add(int num1, int num2, Scanner sc, int answer)
   {
       System.out.println("Please input your first number to add.");
       num1 = sc.nextInt();
      
       System.out.println("Please input your second number to add.");
       num2 = sc.nextInt();
      
       answer = num1 + num2;
       System.out.println("Your answer is " + answer);
   }

}

(You can see where i tried lol)

Nevermind i have solved this issue. When i would run it just let me keep on typing and i forgot to read from the user.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int num1 = 0;
        int num2 = 0;
        int answer = 0;
        int userInput = 0;
        Scanner sc = new Scanner(System.in);


        System.out.println("Hello, this is a program where you can choose to add or subtract two numbers.");

        System.out.println("Would you like to subtract or add?");
        System.out.println("1 for Add || 2 for Subtract.");
        userInput = sc.nextInt();
        if (userInput == 1) {
            add(num1, num2, sc, answer);
        }


    }

    public static void add(int num1, int num2, Scanner sc, int answer) {
        System.out.println("Please input your first number to add.");
        num1 = sc.nextInt();

        System.out.println("Please input your second number to add.");
        num2 = sc.nextInt();

        answer = num1 + num2;
        System.out.println("Your answer is " + answer);
    }

}
Add a comment
Know the answer?
Add Answer to:
I was wanting to know how would I call my add class through my if statement....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • package rectangle; public class Rectangle {    private int height;    private int width;    public...

    package rectangle; public class Rectangle {    private int height;    private int width;    public Rectangle(int aHeight, int aWidth) {    super();    height = aHeight;    width = aWidth;    }    public int getHeight() {    return height;    }    public int getWidth() {    return width;    }    public void setHeight(int aHeight) {    height = aHeight;    }    public void setWidth(int aWidth) {    width = aWidth;    }    public int...

  • JAVA I need a switch so that users can input one selection then another. These selections...

    JAVA I need a switch so that users can input one selection then another. These selections are for a simple calculator that uses random numbers 1. + 2. - 3. * 4./ 5. RNG. This has to be simple this is a beginners class. Here is what I have import java.util.Scanner; import java.util.Random; import java.util.*; public class You_Michael02Basic_Calculator {    public static void main(String[] args) {        // Generate random numbers        int num1,num2;        num1 =(int)(Math.random()*100+1);...

  • DEBUGGING. JAVA. Identify the errors in the following. There are no logical errors in this one...

    DEBUGGING. JAVA. Identify the errors in the following. There are no logical errors in this one just syntax issues. //start import java.util.Scanner; public class NumbersDemo {    public static void main (String args[])    {       Scanner kb = new Scanner(System.in);       int num1, num2;             System.out.print("Enter an integer >> ");       num1 = kb.nextInt();       System.out.print("Enter another integer >> ");       num2 = kb.nextDouble();       displayTwiceTheNumber(num1);       displayNumberPlusFive(num1);       displayNumberSquared(num1)       displayTwiceTheNumber(num2);       displayNumberPlusFive(num2);       displayNumberSquared(num2);    }   ...

  • please help me I need two methods that add and subtract two very large numbers in...

    please help me I need two methods that add and subtract two very large numbers in java created in a class called number and it runs on the demo below class below. Please don't use parseInt or BigIntger, they are not allowed. Thank you so much public class demoClass { public static void main(String[] args) { number num1; //number is class that we need to create to the add and subtract methods num1 = new number("11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"); number num2; //number is...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • Please put both of this loops into one program. Set it up so that there is...

    Please put both of this loops into one program. Set it up so that there is one main function and both loops are within it. Thank You. import java.util.Scanner; public class WhileDavidMungomaLab12 { public static void main(String[] args) { int aScore = -1; while (aScore < 0 || aScore > 100) { Scanner sc = new Scanner(System.in); System.out.println("Please enter a valid number that is within the specified range(0 and 100): "); aScore = sc.nextInt(); } } } AND import java.util.Scanner;...

  • java create java class "nameperson" the example of output would be this: //if the user inputs...

    java create java class "nameperson" the example of output would be this: //if the user inputs sophia 206999109 2 //then the output shows this My name is None and my studentid is 0 My name is sophia and studentid is 0 My name is sophia and studentid is 206999109 My grade is 2 so i have to create two classes   class nameperson{ } class studentid extends person{ } class grade extends studentid{ } my main method is this class Main...

  • Create a java class that user put two inputs and first input generate n length array...

    Create a java class that user put two inputs and first input generate n length array of randomly generated numbers. and the second input changes that number of multiples in the array into zero. for example, if the user puts 3 and 5 then it generates 34 60 10 and since the second input is 5 then the multiple of 5 eliminates so it generates 34 0 0 here is the main method that I'm going to use class Main...

  • How would I edit this Java program to end if the user inputs 0 or less?...

    How would I edit this Java program to end if the user inputs 0 or less? public static void main(String[] args) { Scanner sc = new Scanner(System.in); // to read input String Message; int m=0; int i=1; System.out.print("Please enter the message you would like displayed: "); Message=sc.nextLine(); while(true) { System.out.print("How many times would you like your message displayed?"); m = sc.nextInt(); break; } do { System.out.println(Message); //When I enter 0 or a negative number it still runs at least once,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT