Im trying to make a simple calculator but with 2 files. The first file is for the normal code and the 2nd file is for the switch case. What im trying to do is to use the input from num1 num2 to switch.java My issue is that num1 num2 cannot be resolved to a variable in Switch.java
week1.java
import java.util.Scanner;
public class week1
{
public static void main(String[] args) {
int num1;
int num2;
Scanner input = new Scanner(System.in);
System.out.print("Input a number:");
num1 = input.nextInt();
System.out.print("Input a Math :");
char operator1 = input.next().charAt(0);
System.out.print("Input a number:");
num2 = input.nextInt();
int operator2 = 0;
Switch swish = new Switch();
swish.switchcase();
}
}
Switch.java
import java.util.Scanner;
public class Switch
{
void switchcase()
{
switch(operator1){
case ' ':
operator2 = num1 num2;
System.out.println("Addition: " (week1.num1) " " operator1 " " num2 " = " operator2 );
break;
case '-':
operator2 = num1 - num2;
System.out.println("Subtraction: " num1 " " operator1 " " num2 " = " operator2 );
break;
case '*':
operator2 = num1 * num2;
System.out.println("Multiplication: " num1 " " operator1 " " num2 " = " operator2 );
break;
case '/':
operator2 = num1 / num2;
System.out.println("Division: " num1 " " operator1 " " num2 " = " operator2 );
break;
}
}
}
CodePudding user response:
You must pass parameters to your switchcase
method, and obtain a result back.
Class Week1:
import java.util.Scanner;
public class Week1
{
public static void main(String[] args) {
int num1;
int num2;
Scanner input = new Scanner(System.in);
System.out.print("Input a number:");
num1 = input.nextInt();
System.out.print("Input a Math :");
char operator1 = input.next().charAt(0);
System.out.print("Input a number:");
num2 = input.nextInt();
int result = Switch.switchcase(num1, operator1, num2);
System.out.println(num1 " " operator1 " " num2 " = " result);
}
}
Class Switch:
public class Switch {
public static int switchcase(int num1, char operator1, int num2) {
switch(operator1){
case ' ':
return num1 num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
return num1 / num2;
}
}
}
CodePudding user response:
Try using the below classes. Local variables inside a method will only be accessible inside that method. The variables has to be passed to another method for processing.
It is a best practice to begin class with capital letter. Visit https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html
import java.util.Scanner;
public class Week1
{
public static void main(String[] args) {
int num1;
int num2;
Scanner input = new Scanner(System.in);
System.out.print("Input a number:");
num1 = input.nextInt();
System.out.print("Input a Math :");
char operator1 = input.next().charAt(0);
System.out.print("Input a number:");
num2 = input.nextInt();
int operator2 = 0;
Switch swish = new Switch();
swish.switchcase(num1, operator1, num2);
}
}
public class Switch
{
void switchcase(int num1, char operator1, int num2)
{
int operator2;
switch(operator1){
case ' ':
operator2 = num1 num2;
System.out.println("Addition: " num1 " " operator1 " " num2 " = " operator2 );
break;
case '-':
operator2 = num1 - num2;
System.out.println("Subtraction: " num1 " " operator1 " " num2 " = " operator2 );
break;
case '*':
operator2 = num1 * num2;
System.out.println("Multiplication: " num1 " " operator1 " " num2 " = " operator2 );
break;
case '/':
operator2 = num1 / num2;
System.out.println("Division: " num1 " " operator1 " " num2 " = " operator2 );
break;
}
}
}