当前位置: 首页 > 知识库问答 >
问题:

“令牌”if“语法错误,删除此令牌”if(java)[closed]上显示错误

幸经艺
2023-03-14

我写了一个java代码作为我大学作业的练习。在if-else语句中,我得到了一个错误:“syntax error on token”if“,delete this token”。我想我犯了一些小错误,比如;.但是我找不出那个错误

我注释了显示错误的区域。下面是我写的代码。

package com.slt.dbal;

import java.util.Scanner;

public class DataBalance {

    public static void main(String[] args) {
        
        //Methods
        PackDetails pd = new PackDetails();
        Scanner scn = new Scanner(System.in);
        
        //welcome note
        System.out.println("\nHello, \tWelcome to SLT available data amount finder...\n");
        
        
        //Select package
        int pid;
        
        System.out.println("Enter your package ID from the list below...");
        System.out.println("WEB PAL\t\t- 1\nWEB FAMILY ACTIVE\t- 2");
        
        System.out.print("\nPackage ID: ");
        pid = scn.nextInt();
        
        System.out.println(" ");
        
        //Show selected package to the user
        String pName;
        
        if (pid == 1) {
            
            pName = "WEB PAL";
            
        }
        else {
            
            //Error         
            if {
                pName = "WEB FAMILY ACTIVE";
            }
            //Error
            
        }
        
        System.out.println("Package you selected is " + pName+ "\n\n");
        
        //##################################################################################
        
        //Input users' package current usage
        double SDUse, TDUse;
        
        System.out.print("Enter Standard usage: ");
        SDUse = scn.nextDouble();
        
        System.out.print("Enter Total usage: ");
        TDUse = scn.nextDouble();
        

    }

}

共有1个答案

沃弘图
2023-03-14
if {
                pName = "WEB FAMILY ACTIVE";
            }

这不是有效的java语法,if语句需要一个条件。

if ( condition == true ) {
  pName = "WEB FAMILY ACTIVE";
}

例如,如果您有一个称为condition的布尔值。如果您试图将pName的值与“Web FAMILY active”进行比较,则需要

if ( "WEB FAMILY ACTIVE".equals(pName) ) {
 // code
}

如果您打算在pId不是1时执行字符串赋值,请进行编辑:

    if (pid == 1) {            
        pName = "WEB PAL";            
    } else {
            pName = "WEB FAMILY ACTIVE";
    }
 类似资料: