首页 > 解决方案 > 将变量加在一起的问题

问题描述

我正在尝试编写此代码,以便将变量的值相加,但总和始终为 0.00。谁能解释原因,因为侧边栏上没有显示错误。抱歉,如果这个问题很简单,我是编码新手。我怀疑这个问题可能是由于变量冲突引起的,但我不确定,如果是这样,我不知道如何解决它。

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);
                 
                  }
      }

标签: javavariablesdouble

解决方案


您的问题是因为您必须在 if 之前声明tickerprice 变量,并且您应该注意大括号,因为您将其中一些放在错误的位置。你应该学习如何正确使用分号 ( ; ) 你在 if(...) { ; 之后写它们 ,但没有必要在 if 之后编写它们。我建议你更仔细地编写代码,并尽量让你的代码更具可读性,最后,这不是javascript,多学习编程语言,下次一切都会好起来的。这是您的问题的解决方案:

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);  
    
    }
}

推荐阅读