我试图将“user_input”放置在在函数内部,但我收到错误“表达式的非法开始”

发布于 2024-12-28 12:39:14 字数 5576 浏览 0 评论 0原文

当我尝试创建函数时,不断收到错误“表达式非法开始” 感谢您提前提供的帮助。

 import java.util.*;

public class project0
{
    public static void main(String[] args)
    {
            Scanner scan = new Scanner(System.in);
            Random gen;
            gen = new Random();
            int x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;
            int cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4, cx5, cy5;
            int[] player = new int[10];
            int[] comp = new int [10];
            int shot_x, shot_y, compshot_x, compshot_y;
            int hits, comphits;

//RIGHT HERE IS THE FUNCTION
                user_input(scan, player);

            public static void user_input(Scanner scan, int[] player)
            System.out.println("Welcome to battleship, please enter x and y");
            System.out.println("coordinates for 5 ships within a 4x4 grid");
            x1 = scan.nextInt();
            player[0] = x1;
            y1 = scan.nextInt();
            player[1] = y1;
            x2 = scan.nextInt();
            player[2] = x2;
            y2 = scan.nextInt();
            player[3] = y2;
            x3 = scan.nextInt();
            player[4] = x3;
            y3 = scan.nextInt();
            player[5] = y3;
            x4 = scan.nextInt();
            player[6] = x4;
            y4 = scan.nextInt();
            player[7] = y4;
            x5 = scan.nextInt();
            player[8] = x5;
            y5 = scan.nextInt();
            player[9] = y5;

//Ignore everything below this line:        


                cx1 = Math.abs( gen.nextInt() ) % 4;
                comp[0] = cx1;
                cy1 = Math.abs( gen.nextInt() ) % 4;
                comp[1] = cy1;
                cx2 = Math.abs( gen.nextInt() ) % 4;
                comp[2] = cx2;
                cy2 = Math.abs( gen.nextInt() ) % 4;
                comp[3] = cy2;
                cx3 = Math.abs( gen.nextInt() ) % 4;
                comp[4] = cx3;
                cy3 = Math.abs( gen.nextInt() ) % 4;
                comp[5] = cy3;
                cx4 = Math.abs( gen.nextInt() ) % 4;
                comp[6] = cx4;
                cy4 = Math.abs( gen.nextInt() ) % 4;
                comp[7] = cy4;
                cx5 = Math.abs( gen.nextInt() ) % 4; 
                comp[8] = cx5;
                cy5 = Math.abs( gen.nextInt() ) % 4;
                comp[9] = cy5;


            hits = 0;
            comphits = 0;
            int i;
            for(i = 0; hits < 5 || comphits < 5; i++)
            {
                      System.out.println("Enter coords for your shot");
                      shot_x = scan.nextInt();
                      shot_y = scan.nextInt();

                      if(shot_x == comp[0] || shot_y == comp[1])
                      {
                              System.out.println("Nice hit!");
                              hits++;

                      }       
                      else if(shot_x == comp[2] || shot_y == comp[3])
                      {
                              System.out.println("Nice hit!");
                              hits++;
                      }       
                      else if(shot_x == comp[4] || shot_y == comp[5])
                      {
                              System.out.println("Nice hit!");
                              hits++;
                      }       
                      else if(shot_x == comp[6] || shot_y == comp[7])
                      {
                              System.out.println("Nice hit!");
                              hits++;
                      }       
                      else if(shot_x == comp[8] || shot_y == comp[9])
                      {
                              System.out.println("Nice hit!");
                              hits++;
                      }       
                      else
                               System.out.println("You missed");        

                      compshot_x = Math.abs( gen.nextInt() ) % 4;
                      compshot_y = Math.abs( gen.nextInt() ) % 4;

                           if(compshot_x == player[0] || compshot_y == player[1])
                      {
                              System.out.println("You were hit!");
                              comphits++;

                      }       
                      else if(compshot_x == player[2] || compshot_y == player[3])
                      {
                              System.out.println("You were hit!");
                              comphits++;
                      }       
                      else if(compshot_x == player[4] || compshot_y == player[5])
                      {
                              System.out.println("You were hit!");
                              comphits++;
                      }       
                      else if(compshot_x == player[6] || compshot_y == player[7])
                      {
                              System.out.println("You were hit!");
                              comphits++;
                      }       
                      else if(compshot_x == player[8] || compshot_y == player[9])
                      {
                              System.out.println("You were hit!");
                              comphits++;
                      }       
                      else
                               System.out.println("You are still safe");




            }


            if (comphits == 5)
                    System.out.println("You win! Congrats.");
            else
                    System.out.println("You lost to a machine! Loser.");





    }
}

Keep getting the error 'illegal start of expression' when I try to make a function
thank you for the help in advance.

 import java.util.*;

public class project0
{
    public static void main(String[] args)
    {
            Scanner scan = new Scanner(System.in);
            Random gen;
            gen = new Random();
            int x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;
            int cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4, cx5, cy5;
            int[] player = new int[10];
            int[] comp = new int [10];
            int shot_x, shot_y, compshot_x, compshot_y;
            int hits, comphits;

//RIGHT HERE IS THE FUNCTION
                user_input(scan, player);

            public static void user_input(Scanner scan, int[] player)
            System.out.println("Welcome to battleship, please enter x and y");
            System.out.println("coordinates for 5 ships within a 4x4 grid");
            x1 = scan.nextInt();
            player[0] = x1;
            y1 = scan.nextInt();
            player[1] = y1;
            x2 = scan.nextInt();
            player[2] = x2;
            y2 = scan.nextInt();
            player[3] = y2;
            x3 = scan.nextInt();
            player[4] = x3;
            y3 = scan.nextInt();
            player[5] = y3;
            x4 = scan.nextInt();
            player[6] = x4;
            y4 = scan.nextInt();
            player[7] = y4;
            x5 = scan.nextInt();
            player[8] = x5;
            y5 = scan.nextInt();
            player[9] = y5;

//Ignore everything below this line:        


                cx1 = Math.abs( gen.nextInt() ) % 4;
                comp[0] = cx1;
                cy1 = Math.abs( gen.nextInt() ) % 4;
                comp[1] = cy1;
                cx2 = Math.abs( gen.nextInt() ) % 4;
                comp[2] = cx2;
                cy2 = Math.abs( gen.nextInt() ) % 4;
                comp[3] = cy2;
                cx3 = Math.abs( gen.nextInt() ) % 4;
                comp[4] = cx3;
                cy3 = Math.abs( gen.nextInt() ) % 4;
                comp[5] = cy3;
                cx4 = Math.abs( gen.nextInt() ) % 4;
                comp[6] = cx4;
                cy4 = Math.abs( gen.nextInt() ) % 4;
                comp[7] = cy4;
                cx5 = Math.abs( gen.nextInt() ) % 4; 
                comp[8] = cx5;
                cy5 = Math.abs( gen.nextInt() ) % 4;
                comp[9] = cy5;


            hits = 0;
            comphits = 0;
            int i;
            for(i = 0; hits < 5 || comphits < 5; i++)
            {
                      System.out.println("Enter coords for your shot");
                      shot_x = scan.nextInt();
                      shot_y = scan.nextInt();

                      if(shot_x == comp[0] || shot_y == comp[1])
                      {
                              System.out.println("Nice hit!");
                              hits++;

                      }       
                      else if(shot_x == comp[2] || shot_y == comp[3])
                      {
                              System.out.println("Nice hit!");
                              hits++;
                      }       
                      else if(shot_x == comp[4] || shot_y == comp[5])
                      {
                              System.out.println("Nice hit!");
                              hits++;
                      }       
                      else if(shot_x == comp[6] || shot_y == comp[7])
                      {
                              System.out.println("Nice hit!");
                              hits++;
                      }       
                      else if(shot_x == comp[8] || shot_y == comp[9])
                      {
                              System.out.println("Nice hit!");
                              hits++;
                      }       
                      else
                               System.out.println("You missed");        

                      compshot_x = Math.abs( gen.nextInt() ) % 4;
                      compshot_y = Math.abs( gen.nextInt() ) % 4;

                           if(compshot_x == player[0] || compshot_y == player[1])
                      {
                              System.out.println("You were hit!");
                              comphits++;

                      }       
                      else if(compshot_x == player[2] || compshot_y == player[3])
                      {
                              System.out.println("You were hit!");
                              comphits++;
                      }       
                      else if(compshot_x == player[4] || compshot_y == player[5])
                      {
                              System.out.println("You were hit!");
                              comphits++;
                      }       
                      else if(compshot_x == player[6] || compshot_y == player[7])
                      {
                              System.out.println("You were hit!");
                              comphits++;
                      }       
                      else if(compshot_x == player[8] || compshot_y == player[9])
                      {
                              System.out.println("You were hit!");
                              comphits++;
                      }       
                      else
                               System.out.println("You are still safe");




            }


            if (comphits == 5)
                    System.out.println("You win! Congrats.");
            else
                    System.out.println("You lost to a machine! Loser.");





    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紙鸢 2025-01-04 12:39:14

java 中不能在方法内定义方法。

所以,你不能

public static void user_input(Scanner scan, int[] player)

在 main 的中间声明。

完成 main 定义(以大括号结束),然后声明“user_input”

You cannot define a method within a method in java.

So, you cannot declare

public static void user_input(Scanner scan, int[] player)

in the middle of main.

Complete defining main ( end with a curly braces ) and then declare "user_input"

尹雨沫 2025-01-04 12:39:14

你忘记了大括号,也尝试遵循标准 Java 命名约定

public static void userInput(Scanner scan, int[] player) {
  System.out.println("Welcome to battleship, please enter x and y");

You forgot a curly brace, also try to follow standard Java naming convention

public static void userInput(Scanner scan, int[] player) {
  System.out.println("Welcome to battleship, please enter x and y");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文