Need some help debugging: Keeps return 0 as a value!
Hello All,
so I'm trying to run this little code to calculate a given value and unfortunately it keeps returning 0 as the value. So I created classes: One that is the main class and the other being where I define my method, etc...
So Here is the one containing the method:
Code:
//Define class
public class TieTheKnot {
public int T;//How many Years have you been dating
public int L;//Number of times a day that something makes you think of other person
public int C;//If families get together, #of times there will be uncomfortable friction
public int S;//How many Shared interests/goals you two have?
public int A;//How many individual or conflicting interests and/or goals do you two have?
public int D;//Average # of disagreements you have with this person in a month
// Constructor-catches variables from the object & passes it on to the variables used in the class containing
// the method declaration
public TieTheKnot (int T, int L, int C, int S, int A, int D) {
this.T = T;
this.L = L;
this.C = C;
this.S = S;
this.A = A;
this.D = D;
}
public int tieKnot() {
return (S/A)*(L/(D+1))+((T^2)/(3*(C+1)));
}
}
and here is my main class
Code:
import java.io.*;
import java.util.Scanner;
public class MarriageEquations{
public static void main (String[] args) throws IOException
{
int question1 = 1;
int question2 = 2;
int question3 = 3;
//Ask users whether they want to know the play Marriage Equations game
//Ask users for his/her name
System.out.println("Welcome and thank you for playing the love equation game. What is your name?");
//This is how you read from the keyboard.
//1st We create the object that will read from keyboard
Scanner myScanner = new Scanner(System.in);
//We can now declare the String that the users will put in
String userName = myScanner.next();
//Greeting
System.out.println("Hello "+ userName +".");
System.out.println("There are three questions, please read them and decide which one you would like to do first.");
System.out.println("1. Should I get married?");
System.out.println("2. What are the chances that my marriage will last?");
System.out.println("3. How many kids hsould I have?");
System.out.println();
System.out.print("Now that you have read them, please choose an answer by typing \"1\" for the first, \"2\" for the second one ");
System.out.print("and \"3\" for the last one.");
System.out.println();
int Answer = myScanner.nextInt();
System.out.println("You chose: "+Answer+".");
if (question1 == Answer) System.out.println("it works");
switch (Answer) {
case 1:
System.out.println("You chose to ask Whether you should I get married?");
System.out.println();
System.out.println("Please answer the following questions!");
System.out.println("How many Years have you been dating");
int T = myScanner.nextInt();//
System.out.println("Number of times a day that something makes you think of other person");
int L = myScanner.nextInt();//
System.out.println("If families get together, #of times there will be uncomfortable friction");
int C = myScanner.nextInt();//
System.out.println("How many Shared interests/goals you two have?");
int S = myScanner.nextInt();//
System.out.println("How many individual or conflicting interests and/or goals do you two have?");
int A = myScanner.nextInt();//
System.out.println("What is the number of disagreements you have with this person in a month?");
int D = myScanner.nextInt();//
System.out.println("Thank you. We are now computing your answers...");
System.out.println();
//Initialize objects or create objects
TieTheKnot knots = new TieTheKnot( T, L, C, S, A, D);
System.out.println(knots.tieKnot());
break;
case 2:
So when I run this as is, I keep getting
Quote:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at TieTheKnot.tieKnot(TieTheKnot.java:29)
at MarriageEquations.main(MarriageEquations.java:63)
I don't understand why it's returning 0 as a value or why it's not passing the values collected from the user and giving me a non 0 value.
Thank you for your help. Any suggestions on syntax is welcome as well!