Unable to get total amount
Unable to get total amount
Hi guys I have fixed the initial problem but now its not adding up correctly. I am unsure what to do and where I have gone wrong. Any help would be appreciated.
import java.util.Scanner;
public class zoo {
public static void main(String args) {
int quantity, confirm, option, finalTotal;
float childTotal = 0;
float adultTotal = 0;
float seniorTotal = 0;
final double childCost = 18;
final double adultCost = 36;
final double seniorCost = 32.50;
int Option = new int[3];
Option[0] = 1;
Option[1] = 2;
Option[2] = 3;
boolean continueLoop = true;
char resume;
switch (option) {
case 1:
childTotal=(int) ((double) quantity*childCost) ;
System.out.println("Total amount for child tickets: $" + childTotal);
break;
case 2:
adultTotal=(int) ((double) quantity*adultCost) ;
System.out.println("Total amount for adult tickets $" + adultTotal);
break;
default:
seniorTotal=(int) ((double) quantity*seniorCost);
System.out.println("Total amount for senior tickets $" + seniorTotal);
break;
}
System.out.println("Do you wish to continue? (Y/N) ");
resume = input.next().charAt(0);
switch (option) {
case 1:
finalTotal=(int) ((double) childCost+childTotal);
System.out.println("Total amount for tickets: $" + finalTotal);
break;
case 2:
finalTotal=(int) ((double) adultCost+adultTotal) ;
System.out.println("Total amount for tickets $" + finalTotal);
break;
default:
finalTotal=(int) ((double) seniorCost+seniorTotal);
System.out.println("Total amount for senior tickets $" + finalTotal);
break;
}
@Glains i have fixed the initial problem but now its not adding up correctly
– Bec
Jun 29 at 10:58
So what's the question?
– Mureinik
Jun 29 at 10:59
@Mureinik at the end of the program it should calculate the total amount which has been chosen. the program is designed to re run the program if the user would like to choose more tickets
– Bec
Jun 29 at 11:07
1 Answer
1
maybe you trying to do something like this:
public int someMethod(){
int childTotal; // here variable must be initialized before return
// like this int childTotal = 0;
switch (option) {
case 1:
childTotal=(int) ((double) quantity*childCost) ;
System.out.println("Total amount for child tickets: $" + childTotal);
break;
}
...
return childTotal;
}
in this case you'll get an error, that variable must be initialized.
Anyway you gave to few information about your problem. Maybe you can show what kind of answer you get from the system, stackTrace or something like that.
Did you know about diference between initialize and declare?
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Could you actually point to the location the variables are initialized? You included source code, but it seems the source of the problem is missing.
– Glains
Jun 29 at 10:24