有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

令牌上的java Sytnax错误,构造位置错误

我在做一个计算机科学的作业,我有几个错误。我不得不上传代码,因为我无法让它在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

请帮忙


共 (2) 个答案

  1. # 1 楼答案

    正如已经发布的,在主方法中有静态方法,这是根本错误的。你不能在一个方法中有一个方法——除非它在匿名类中(但我们不要讨论这个)。因此,首先要删除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语句传递参数,您可以通过这样做,即

    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"
    }
    
  2. # 2 楼答案

    我不明白您想做什么,但此代码应该可以工作:

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