Home > OS >  Issues with adding variables together
Issues with adding variables together

Time:10-18

I am trying to write this code so that it will add the values of variables, but the sum always totals to 0.00. Can anyone explain why, as there are no errors shown on the sidebar. Apologies if this issue is very simple, I am new to coding. I suspect that the issue could be due to conflicting variables, but I'm not sure, and if it is that, I do not know how I can fix it.

import java.util.Scanner;
public class TitanicAgeSubmissions {

    public static void main(String[] args) {
        
        // Write a program for four people to enter the Titanic Belfast. 
        // They all must have variable ages, which the user should be able to input. 
        // There are four age values:
        // Adult: £19
        // Child (5-16): £8.50
        // Child under 5: Free
        
        
        
        
        System.out.println("Please enter the age of the first Visitor:");
 Scanner sc= new Scanner (System.in);
 double age1 = sc.nextDouble();

 System.out.println("Please enter the age of the second Visitor:");
 Scanner sc2= new Scanner (System.in);
 double age2 = sc.nextDouble() ;
 
 System.out.println("Please enter the age of the third Visitor:");
 Scanner sc3= new Scanner (System.in);
 double age3 = sc.nextDouble(); 
 
 System.out.println("Please enter the age of the fourth Visitor:");
 Scanner sc4= new Scanner (System.in);
 double age4 = sc.nextDouble();
        
        
 if (age1>16) {;
 double ticketprice1 = 19;   }
 
     else if  (age1 <16 && age1>=5)    {
     double ticketprice1= 8.50;  }
      
      else if  (age1 <5)  {
          }double ticketprice1 = 0.00 ;  
     
     
     // next values
     
     
     if (age1>16) {;
     double ticketprice2 = 19;   }
     
         else if  (age1 <16 && age1>=5)    {
         double ticketprice2= 8.50;  }
          
          else if  (age1 <5)  {
              }double ticketprice2  = 0.00;  
         
// next values
         
         if (age1>16) {;
         double ticketprice3 = 19;   }
         
             else if  (age1 <16 && age1>=5)    {
             double ticketprice3= 8.50;  }
              
              else if  (age1 <5)  {
                 }double ticketprice3= 0.00;  
             
        // next values    
              
             if (age1>16) {;
              double ticketprice4 = 19;   }
             
                 else if  (age1 <16 && age1>=5)    {
                  double ticketprice4= 8.50;  }
                 
                  else if  (age1 <5)  {
                      }double ticketprice4 = 0.00;  
                 
                  
                 double grandtotal = ticketprice1   ticketprice2   ticketprice3   ticketprice4;
                System.out.println("The grand total of the tickets in pounds is:"   " "    grandtotal);
                 
                  }
              
          
      }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

your problem is because you must declare tickerprice variables before if, and you should pay attention to curly brackets, because you put some of them in wrong positions. And you should learn about properly using semicolon ( ; ) you write them after if(...) { ; , but it isn't necessary to write them after if. I recommend you to write code more carefully, and try to make your code more readable, and for the end, this is not javascript, learn more about programming languages, and everything will be fine next time. This is solution for your problem:

import java.util.Scanner;
public class TitanicAgeSubmissions {

    public static void main(String[] args) {
        
        // Write a program for four people to enter the Titanic Belfast. 
        // They all must have variable ages, which the user should be able to input. 
        // There are four age values:
        // Adult: £19
        // Child (5-16): £8.50
        // Child under 5: Free
        
        
        
        
        System.out.println("Please enter the age of the first Visitor:");
        Scanner sc= new Scanner (System.in);
        double age1 = sc.nextDouble();

        System.out.println("Please enter the age of the second Visitor:");
        Scanner sc2= new Scanner (System.in);
        double age2 = sc.nextDouble() ;
        
        System.out.println("Please enter the age of the third Visitor:");
        Scanner sc3= new Scanner (System.in);
        double age3 = sc.nextDouble(); 
        
        System.out.println("Please enter the age of the fourth Visitor:");
        Scanner sc4= new Scanner (System.in);
        double age4 = sc.nextDouble();

        double ticketprice1 = 0.0, ticketprice2 = 0.0, ticketprice3 = 0.0, ticketprice4 = 0.0;
        
        if (age1>16) {
            ticketprice1 = 19;   
        }
        else if  (age1 <16 && age1>=5)    {
            ticketprice1= 8.50;  
        }
        else if  (age1 <5)  {
            ticketprice1 = 0.00 ; 
        } 
     
     
        // next values
        
        
        if (age2>16) {
            ticketprice2 = 19;   
        }
        else if  (age2 <16 && age2>=5)    {
            ticketprice2= 8.50;  
        }
        else if  (age2 <5)  {
            ticketprice2 = 0.00 ; 
        } 
         
        // next values
            
        if (age3>16) {
            ticketprice3 = 19;   
        }
        else if  (age3 <16 && age3 >=5)    {
            ticketprice3= 8.50;  
        }
        else if  (age3 <5)  {
            ticketprice3 = 0.00 ; 
        } 
             
        // next values    
              
        if (age4>16) {
            ticketprice4 = 19;   
        }
        else if  (age4 <16 && age4 >=5)    {
            ticketprice4= 8.50;  
        }
        else if  (age4 <5)  {
            ticketprice4 = 0.00 ; 
        } 
                 
        double grandtotal = ticketprice1   ticketprice2   ticketprice3   ticketprice4;
        System.out.println("The grand total of the tickets in pounds is:"   " "    grandtotal);  
    
    }
}

CodePudding user response:

The root cause of the miscalculation is incorrect declaration and initialization of ticketprice# variables. Also, there is a bug in using the same age1 to define values of all ticketprice# variables.

Other bugs/flaws are:

  • declaration of unused Scanner instances sc2, sc3, sc4 -- using only one Scanner is quite fine
  • gap for age == 16 in if statements
  • multiple usage of double var1, var2, ... instead of array double[] ages, ticketPrices;

A separate method should be used to define price from the age.

So the improved solution may loik like this:

static double getPrice(double age) {
    double price = 0.0; // default value

    if (age > 16) { // for adults
        price = 19;
    } else if (age >= 5) { // for children
        price = 8.5;
    }

    return price;
}

public static void main(String[] args) {
    int n = 4;

    double[] ages = new double[n];
    double[] prices = new double[n];
    String[] ids = {"first", "second", "third", "fourth"};

    double sum = 0.0;
    Scanner sc= new Scanner (System.in);

    for (int i = 0; i < n; i  ) {
        System.out.println("Please enter the age of the "   ids[i]   " Visitor:");
        ages[i] = sc.nextDouble();
        prices[i] = getPrice(ages[i]);
        sum  = prices[i];

        // or a chain may be used
        // sum  = prices[i] = getPrice(ages[i] = sc.nextDouble());
    }

    System.out.println("The grand total of the tickets in pounds is: £"   sum);
}

Output of a test run:

Please enter the age of the first Visitor:
4
Please enter the age of the second Visitor:
16
Please enter the age of the third Visitor:
21
Please enter the age of the fourth Visitor:
9
The grand total of the tickets in pounds is: £36.0
  • Related