SO Im new to java programming and I cannot figure out how to invoke a second met
ID: 3867488 • Letter: S
Question
SO Im new to java programming and I cannot figure out how to invoke a second method in eclipse below is the code im working with its not the entire source code only the spot im trying to invoke a second method and under that the second method code. also it is a void method I am calling so im not sure what to put in the quotations in my main method where I am invoking, I am currently trying to get it to accept the int 1 case since that is the choice for addition on my menu not displayed in this posting.Thanks in advance
int userC = input.nextInt();//user enters their choice
switch (userC){//operation is determined based of off user input
case 1: System.out.println(AdditionP(1.0));
case 2: do{//subtraction program
double n1 = (Math.round(Math.random()*10));
double n2 = (Math.round(Math.random()*10));
System.out.println("What is "+ n1 + "- "+ n2 +"?");
n3 = n1-n2;.....
public static void AdditionP(double n4){
Scanner input = new Scanner(System.in);
n4=1.0;
double n3 = 0;
double uA = 0;
double n1 = 0;
double n2 = 0;
do {//addition program
n1 = (Math.round(Math.random()*10));
n2 = (Math.round(Math.random()*10));
System.out.println("What is "+ n1 + "+ "+ n2 +"?");
n3 = n1+n2;
uA = input.nextDouble();
if (n3== uA){
System.out.println("Correct");
}
else{
System.out.println("Incorrect please try another");
}
}while(uA != n3);
input.close();
}
Explanation / Answer
Ans:
You are asking to call a second method in the same class, to call a method "object" is optinal like you already called a method without Object in switch Cases. we can call a method from anywhere in the class without object.
Generally our main method with String array It should be same, Syntax is
public static void main(String []args){
--- Statements
---Statements
}
you are calling first method in switch, you also call second method also from switch cases in Eclipse also.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.