簡體   English   中英

令牌上的語法錯誤,錯位的構造

[英]Sytnax error on token(s), misplaced construct(s)

我正在做計算機科學方面的作業,但我遇到了一些錯誤。 我不得不上傳代碼,因為我無法讓它在 Stackoverflow 上工作。 這是鏈接: http : //txt.do/tqmh

以下是錯誤:

16 errors found:
File: C:\Users\Om\Desktop\CYOA_Om_Malhar.java  [line: 15]
Error: Syntax error on token(s), misplaced construct(s)
File: C:\Users\Om\Desktop\CYOA_Om_Malhar.java  [line: 15]
Error: Syntax error on token "int", @ expected
File: C:\Users\Om\Desktop\CYOA_Om_Malhar.java  [line: 32]
Error: Syntax error on token(s), misplaced construct(s)
File: C:\Users\Om\Desktop\CYOA_Om_Malhar.java  [line: 32]
Error: Syntax error on token "int", @ expected
File: C:\Users\Om\Desktop\CYOA_Om_Malhar.java  [line: 57]
Error: Syntax error on token(s), misplaced construct(s)
File: C:\Users\Om\Desktop\CYOA_Om_Malhar.java  [line: 57]
Error: Syntax error on token "int", @ expected
File: C:\Users\Om\Desktop\CYOA_Om_Malhar.java  [line: 83]
Error: Syntax error on token(s), misplaced construct(s)
File: C:\Users\Om\Desktop\CYOA_Om_Malhar.java  [line: 83]
Error: Syntax error on token "int", @ expected
File: C:\Users\Om\Desktop\CYOA_Om_Malhar.java  [line: 112]
Error: Syntax error on token(s), misplaced construct(s)
File: C:\Users\Om\Desktop\CYOA_Om_Malhar.java  [line: 112]
Error: Syntax error on token "int", @ expected
File: C:\Users\Om\Desktop\CYOA_Om_Malhar.java  [line: 137]
Error: Syntax error on token(s), misplaced construct(s)
File: C:\Users\Om\Desktop\CYOA_Om_Malhar.java  [line: 137]
Error: Syntax error on token "int", @ expected
File: C:\Users\Om\Desktop\CYOA_Om_Malhar.java  [line: 161]
Error: Syntax error on token(s), misplaced construct(s)
File: C:\Users\Om\Desktop\CYOA_Om_Malhar.java  [line: 161]
Error: Syntax error on token "int", @ expected
File: C:\Users\Om\Desktop\CYOA_Om_Malhar.java  [line: 184]
Error: Syntax error on token(s), misplaced construct(s)
File: C:\Users\Om\Desktop\CYOA_Om_Malhar.java  [line: 184]
Error: Syntax error on token "int", @ expected

請幫忙!

我不明白你想做什么,但這段代碼應該可以工作:

import java.util.Scanner;
public class CYOA_Om_Malhar 
{
    public static void main(String[]args) 
    {
        Scanner in = new Scanner(System.in);
        String choice = "";
        Introduction();

        //Instructions
        System.out.println("Please type 'A' or 'B' to choose an option: ");
        while(!choice.equalsIgnoreCase("A") && !choice.equalsIgnoreCase("B") ){
            choice = in.nextLine();
        }

        page1(choice);
        page2(choice);
        page3(choice);
        page4(choice);
        page5(choice);
        page6(choice);
        page7();


    }
    public static void Introduction()
    {
        System.out.println("You are walking home one night after being fired from your job. "); 
        System.out.println("You are late on your rent, and you have no money. ");
        System.out.println("As you are walking by, you see a black object out of the corner of your eye. ");
        System.out.println("You reach over to pick it up, and you realize that it's a wallet full of cash. ");
        System.out.println("You take it home to your rat infested apartment. ");
        System.out.println("You then start counting the money as soon as you sit on the squeaky chair of yours. ");
        System.out.println("You get surprised as you notice that the wallet has $10,000. What do you do?");
    }

    //Page 1
    public static int page1(String choice)
    {
        System.out.println("A. Take the wallet to the police station. ");
        System.out.println("B. Use the money to rent a new condo near Lakeshore Blvd., and lease a new BMW X6. ");


        if(choice.equalsIgnoreCase("A"))
        {
            System.out.println("Great decision. ");
            return 2;
        }
        else if(choice.equalsIgnoreCase("B"))
        {
            System.out.println("You're disonest. Start over. :(");
            return -1;
        }
        else
        {
            System.out.println("Invalid selection. Please try again. ");
            return -1;
        }
    }

    //Page 2
    public static int page2(String choice)
    {
        System.out.println("Now, all you have to do is find a way to get to the police station - which is on the other side of the town. ");
        System.out.println("How do you get there? ");
        System.out.println("A. Hitchhike! Stand by the side of the road, and wait for a car to pass by. ");
        System.out.println("B. Borrow $10 from your Aunt May. ");

        if(choice.equalsIgnoreCase("A"))
        {
            System.out.println("You're standing there on the side of the road, and no cars are passing by. Try again. ");
            return -1;
        }
        else if(choice.equalsIgnoreCase("B"))
        {
            System.out.println("Rejoice! Your aunt agrees to give you $10!");
            return 3;
        }
        else
        {
            System.out.println("Invalid selection. Please try again. ");
            return -2;
        }
    }
    //Page 3
    public static int page3(String choice)
    {
        System.out.println("You hear Macklemore's 'Thrift Shop' playing in the background. For some odd reason, you feel rich today. ");
        System.out.println("As you're walking your jolly way, you see a beggar on the street. You notice his pale skin, and that unpleasant smell he carries around with him. ");
        System.out.println("You stop near him, and he asks you for $5, and says that he hasn't eaten any food for the part 2 days. ");
        System.out.println("What do you do?");
        System.out.println("A. Give $5 to the beggar. ");
        System.out.println("B. Act as if you're not listening to him, and walk away. ");

        if(choice.equalsIgnoreCase("A"))
        {
            System.out.println("Great choice!");
            return 4;
        }
        else if(choice.equalsIgnoreCase("B"))
        {
            System.out.println("You're very shelfish. Try again. ");
            return -1;
        }
        else
        {
            System.out.println("Invalid selection. Please try again. ");
            return -2;
        }
    }

    //Page 4
    public static int page4(String choice)
    {
        System.out.println("You finally reach the bus stop. ");
        System.out.println("Upon looking at the bus timings, you realize that you've missed the last bus for the night. ");
        System.out.println("What do you do? ");
        System.out.println("A. Spend the night at the hotel. ");
        System.out.println("B. Wait for the bus for a couple of hours. ");


        if(choice.equalsIgnoreCase("A"))
        {
            return 5;
        }
        else if(choice.equalsIgnoreCase("B"))
        {
            System.out.println("Great choice!");
            return 6;
        }
        else 
        {
            System.out.println("Invalid selection. Please try again. ");
            return -2;
        }
    }

    public static int page5(String choice)
    {
        System.out.println("Hotel, huh? You walk to the hotel.");
        System.out.println("You go to the counter, and the receptionist tells you that the hotel is full. ");
        System.out.println("What do you do?");
        System.out.println("A. Go back in time.");
        System.out.println("B. Go back to the bus stop and wait there for a couple of hours.");


        if(choice.equalsIgnoreCase("A"))
        {
            return 1;
        }
        else if(choice.equalsIgnoreCase("B"))
        {
            return 6;
        }
        else
        {
            System.out.println("Invalid selection. Please try again.");
            return -2;
        }
    }

    public static int page6(String choice)
    {
        System.out.println("2.78 HOURS LATER. ");
        System.out.println("It's almost dawn. You finally see a bus coming right at you. ");
        System.out.println("You get in the bus. Everything seems normal. UNTIL... ");
        System.out.println("A man climbs on the bus. He has a black cloth wrapped around his face. He takes a look at everyone in the bus through two holes in the cloth. ");
        System.out.println("He then asks everyone for money, jewelry, and watches. ");
        System.out.println("What do you do?");
        System.out.println("A. Don't give the cash to the thug. ");
        System.out.println("B. Give the cash to the thug. ");


        if(choice.equalsIgnoreCase("A"))
        {
            return -1;
        }
        else if(choice.equalsIgnoreCase("B"))
        {
            return 7;
        }
        else
        {
            System.out.println("Invalid selection. Please try again.");
            return -2;
        }
    }

    public static void page7()
    {
        System.out.println("As you are handing the cash to the thug, you hear sirens in the near distant.");
        System.out.println("The thug RUNS before he could take the cash. You feel very relieved. ");
        System.out.println("You FINALLY approach the police station.");
        System.out.println("You give the wallet to the police, and they thank you for your honesty. ");
        System.out.println("TWO DAYS LATER.");
        System.out.println("A knock on the door. It's Bill Gates! He thanked you for returning the wallet, and writes you a check for $100,000! ");
        System.out.println("An hour later, another knock at the door. It's the beggar. He won a lottery with the $5 that you gave him. ");
        System.out.println("He writes you a check for $50,000! :D");
        System.out.println("You then buy a brand spanking new BMW X8, and a condo at Lakeshore Blvd.!");
        System.out.println("And then everyone lived happily ever after. ");
    }
}

正如已經發布的那樣,您在主方法中有靜態方法 - 這從根本上是錯誤的。 你不能在一個方法里面有一個方法 - 除非它在匿名類里面(但不要進入這個)。 因此,首先從main(String[] args).刪除所有方法main(String[] args).

所以例如你應該有

public static void main(String[] args) {
    page1();
    page2();
    page3();
}
// this is now outside of your main method but inside your class 
public static void page1() {

}
public static void page2() {

} 
//etc.

此外,如果您想將參數傳遞給方法 - 我可以看到您需要為 if 語句執行此操作,您可以通過執行 ie

public static void main(String[] args) {
    // this is just example..!!

    String letter = "A";
    page1(letter);

    // what the above code will do, is pass a letter "A" as an argument to this "page1"
    // method and as a result you will be able to use this letter inside the method

}
public static void page1(String letter) {
    System.out.println(letter); // this will print letter "A"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM