george.p
george.p

Reputation: 35

Syntax error on token "b", delete this token

import java.util.Scanner;

public class Demo1_1 {
    public static void main(String[] args) {
        int a,b,c,d;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入金字塔层数:");
        a = sc.nextInt();
        for(b=1;b<=a;b++){
            for(c=1;c<=a-b;c++){
                System.out.println(" ");
            }
            for(d=1;d<=2b-1;d++){
                System.out.println("*");
                }
            System.out.println("\n");
        }
    }
}

The exception shows:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error on token "b", delete this token at none.Demo1_1.main(Demo1_1.java:15)

What's wrong with the B token?

Upvotes: 0

Views: 1263

Answers (3)

2b-1 is a math expression that must be in java as 2*b -1

for(d=1;d<=2b-1;d++){ 

this in not correct, you mean for sure

for(d=1;d<=2*b-1;d++){

Upvotes: 1

Brian Huang
Brian Huang

Reputation: 35

instead of 2b it should be 2*b

for(d=1; d <= 2*b -1; d++){

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201447

You can't use d<=2b-1 (because Java won't assume that's multiplication)

for(d=1;d<=2b-1;d++){

I think you wanted d<=(2*b)-1; like

for(d=1;d<=(2*b)-1;d++){

Upvotes: 1

Related Questions