我试图将“user_input”放置在在函数内部,但我收到错误“表达式的非法开始”
当我尝试创建函数时,不断收到错误“表达式非法开始” 感谢您提前提供的帮助。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
java 中不能在方法内定义方法。
所以,你不能
在 main 的中间声明。
完成 main 定义(以大括号结束),然后声明“user_input”
You cannot define a method within a method in java.
So, you cannot declare
in the middle of main.
Complete defining main ( end with a curly braces ) and then declare "user_input"
你忘记了大括号,也尝试遵循标准 Java 命名约定
You forgot a curly brace, also try to follow standard Java naming convention