我有一个计算机科学课的项目,我们正在制造战舰。该计划的一部分是我们确保玩家放下的棋子不会脱离棋盘。
我已经制定了一种方法来检查它是否会脱离董事会:
private static boolean test(String s, int row, int column,int spaces)
{
if(s.equals("right")&&column+5<=10)
{
return true;
}
if(s.equals("up")&&row-spaces>=0)
{
return true;
}
if(s.equals("left")&&column-spaces>=0)
{
return true;
}
if(s.equals("Down")&&row+spaces<=10)
{
return true;
}
return false;
}
但是一旦我打印出错误信息,我就不知道如何制作它以便程序可以重新接收该作品的新位置,而无需使用if语句if语句中的in和if语句(以及on和on),因为你需要检查新的位置以确保它不会离开棋盘。
这是我获得比赛位置的部分(虽然我认为你不需要它)
Scanner sonic= new Scanner(System.in);
System.out.println("Please input the row where you want the aircraft carrier (5 spaces) to begin: ");
int beginrow = sonic.nextInt();
System.out.println("Please input the column where you want the aircraft carrier (5 spaces) to begin: ");
int begincolumn = sonic.nextInt();
System.out.print("Please input what direction (up, down, left, right) \nyou want your battle ship to face, making sure it doesn't go off of the board.");
String direction = sonic.next();
这是我用来检查/放置片段的if语句之一
if(direction.equals("left")&&test("left",beginrow,begincolumn,5))
{
for(int i = beginrow; i>beginrow-5; i--)
{
battleship[begincolumn-1][i-1] = ('a');
}
}
else if(!test("left",beginrow,begincolumn,5))
{
System.out.println(" ");
System.out.println("*****ERROR: your piece goes off the board, please re-enter your position and direction*****");
}
这可能是重复的,但我不知道如何改写我的搜索以找到我想要的内容。 (所以,如果有人能指导我找到正确的文章,那也很好)
答案 0 :(得分:2)
您应该做的是将代码适当地拆分为方法并重复调用这些方法,直到您的程序对结果满意为止。
例如:
startGame()
,该方法的作业调用方法会在满意之前获得用户输入这可能看起来像
public void startGame() {
// do some setup
while(!requestShipInput()) { // request ship data until the data is valid
System.out.println(" ");
System.out.println("*****ERROR: your piece goes off the board, please re-enter your position and direction*****");
}
// do some more ship setup
// get the actual playing started
}
public boolean requestShipInput() {
Scanner sonic= new Scanner(System.in);
System.out.println("Please input the row where you want the aircraft carrier (5 spaces) to begin: ");
int beginrow = sonic.nextInt();
System.out.println("Please input the column where you want the aircraft carrier (5 spaces) to begin: ");
int begincolumn = sonic.nextInt();
System.out.print("Please input what direction (up, down, left, right) \nyou want your battle ship to face, making sure it doesn't go off of the board.");
String direction = sonic.next();
if(direction.equals("left")&&test("left",beginrow,begincolumn,5)) {
for(int i = beginrow; i>beginrow-5; i--) {
battleship[begincolumn-1][i-1] = ('a');
}
return true; // valid ship data
}
return false; // invalid ship data
}
答案 1 :(得分:1)
我认为你可以很自然地在这里使用recursion:
public void getInput() {
// scanner code to get input
if (!test("left",beginrow,begincolumn,5)) { // test failed
getInput()
return
}
// test succeeded, continue
}
答案 2 :(得分:1)
作为第一步,将输入验证与基于该输入的操作分开 - 您已经在单独的函数中具有验证逻辑,因此这很容易。然后找出在输入无效的情况下需要做什么 - 在你的情况下,你需要在获得有效位置之前要求新的输入:
import random
questions = ['What is 1 + 2?','What is 2 + 2?' ]
a,b = questions
x = random.choice(questions)
if x == a:
l = int(input(a))
if l == 3:
print('Correct')
else:
print('False')
elif x == b:
l = int(input(b))
if l == 4:
print('Correct')
else:
print('False')
之后,你知道你有一个有效的职位。
我的下一步可能是将在船上描述船舶所需的信息(即do {
System.out.println("Please input the row where you want the aircraft carrier (5 spaces) to begin: ");
beginrow = sonic.nextInt();
System.out.println("Please input the column where you want the aircraft carrier (5 spaces) to begin: ");
begincolumn = sonic.nextInt();
System.out.print("Please input what direction (up, down, left, right) \nyou want your battle ship to face, making sure it doesn't go off of the board.");
direction = sonic.next();
} while (!test(direction, beginrow, begincolumn, 5))
,beginrow
,begincolumn
,可能还有direction
)分组。一个单独的对象 - 可能名为size
。
答案 3 :(得分:0)
你已经有了一些限制你的董事会?如果您先执行检查,则不需要执行if-else
的级联if(!test(direction,beginrow,begincolumn,size))
{
System.out.println(" ");
System.out.println("*****ERROR: your piece goes off the board, please re-enter your position and direction*****");
} else {
// check for collision with already placed ships
}
请记住,有可能将向上/向下和向左/向右组合。计算规则几乎相同,您只需要决定是否必须查看其中一个或另一个方向。