Saturday, 24 August 2013

Creating A Method Assistance Needed

Creating A Method Assistance Needed

So basically I'm trying to create a method that asks the user if they want
to continue or not. I'm very noob at java, and any help would be
appreciated. I have no clue why but I keep getting errors with the title
of the method. I also have an error with the while statement. Says cont
cannot be resolved, but in the do statement the variable cont works. Here
is my code:
I added parenthesis and get a void error "Syntax error on token "void", @
expected". also the cont variable in the while statement is still messed
up.
public void toContinue1(){
do{
Scanner input = new Scanner(System.in);
System.out.println("Continue? Y/N: ");
String cont = input.next();
if (cont.equalsIgnoreCase("n")){
break;
}else if(!cont.equalsIgnoreCase("y")){
System.out.println("Please enter 'y' or 'n'");
}
}while(!cont.equalsIgnoreCase("y"));
}
Whole Class file for calculator:
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
public static toContinue(){
do{
Scanner input = new Scanner(System.in);
System.out.println("Continue? Y/N: ");
String cont = input.next();
if (cont.equalsIgnoreCase("n")){
break;
}else if(!cont.equalsIgnoreCase("y")){
System.out.println("Please enter 'y' or 'n'");
}
}while(!cont.equalsIgnoreCase("y"));
}
while(true){
System.out.println("First Number: ");
int number1 = input.nextInt();
System.out.println("Second Number: ");
int number2 = input.nextInt();
System.out.println("Operator (+, -, /, *)");
String operation = input.next();
int total;
if(operation.equals("+")){
total = number1 + number2;
System.out.println(total);
}
else if(operation.equals("-")){
total = number1 - number2;
System.out.println(total);
}
else if(operation.equals("*")){
total = number1 * number2;
System.out.println(total);
}
else if(operation.equals("/")){
total = number1 / number2;
System.out.println(total);
}
else{
System.out.println("Invalid input!");
}
toContinue1();
}
public void toContinue1(){
do{
Scanner input = new Scanner(System.in);
System.out.println("Continue? Y/N: ");
String cont = input.next();
if (cont.equalsIgnoreCase("n")){
break;
}else if(!cont.equalsIgnoreCase("y")){
System.out.println("Please enter 'y' or 'n'");
}
}while(!cont.equalsIgnoreCase("y"));
}
}
}

No comments:

Post a Comment