我目前在高中的 AP 计算机科学课上。我们使用“kareltherobot”包在 Java 中编码。以下是说明:对于这个实验室,我们的机器人将像一个 Roomba 一样工作。它会拾取房间里所有的蜂鸣器。它会在执行任务时跟踪信息。它必须清理房间中的每个位置。必须提示用户清洁房间(要读入的世界文件)以及机器人初始状态的位置和方向。
反馈给用户的信息:
• 房间面积
• 桩数
• 蜂鸣器总数
• 最大的蜂鸣器堆
• 最大桩的位置(相对)
• 平均桩大小
• 脏污百分比(堆/面积)
我提供了一个模板和一个世界来测试。因为我们正处于学习编程和测试的初始阶段,所以我正在提供测试世界。考虑可能破坏代码的测试用例,并在开发过程中尝试测试这些用例是个好主意。
这是我当前的代码。我的问题是我无法让机器人去东南角。我试图让它去那里然后让它移动并使用方法清洁行。这样,它将一一清洁每一行。有人可以帮帮我吗?
import java.util.Scanner;
import javax.swing.JOptionPane;
import kareltherobot.*;
public class Driver implements Directions{
public static int locationx2;
public static int locationy2;
public static String direction;
Direction direction2;
int beeperspicked;
Robot r ;
/**
* @param args
*/
public static void main(String[] args) {
// LEAVE THIS ALONE!!!!!!
Driver d = new Driver();
d.getInfo();
d.cleanRoom();
//d.printResults();
}
private void printResults() {
// A bunch of System.out.prints go here
JOptionPane.showMessageDialog(null, "Number of piles was" + );
JOptionPane.showMessageDialog(null, "Area of room was" + );
JOptionPane.showMessageDialog(null, "Number of beepers was" + beeperspicked );
JOptionPane.showMessageDialog(null, "Largest pile of beepers was" + );
JOptionPane.showMessageDialog(null, "Location of the largest pile was" + );
JOptionPane.showMessageDialog(null, "Average pile size was" + );
JOptionPane.showMessageDialog(null, "The percent dirty was" + );
}
private void cleanRoom() {
// all the code that cleans and counts goes here
// obviously, lots more here
r= new Robot(locationy2,locationx2,direction2,0);
beeperspicked= 0;
int loop= 2;
while (r.frontIsClear()){
while (!r.facingEast()){
r.turnLeft(); }
while (r.facingEast()){
r.move(); }
}
while (!r.frontIsClear()){
r.turnLeft();
}
while (loop==0){
cleanRow();
}
}
private void cleanRow() {
while (r.nextToABeeper()== true) {
r.pickBeeper();
beeperspicked++;}
while (r.frontIsClear()== true) {
r.move(); }
while (!r.frontIsClear()){
r.turnLeft();
r.turnLeft();
}
}
private void getInfo() {
// this method acquires the starting street, avenue and direction
// of the robot from the user. Also it inputs the name of the world
// file. It then opens the world file and creates the robot
String worldname = JOptionPane.showInputDialog("World Name?");
String locationx = JOptionPane.showInputDialog("X Coordinate?");
locationx2 = Integer.parseInt(locationx);
String locationy = JOptionPane.showInputDialog("Y Coordinate?");
locationy2 = Integer.parseInt(locationy);
String direction = JOptionPane.showInputDialog("Direction to face?");
if (direction.equals("North")) {
direction2= North;
}
if (direction.equals("East")) {
direction2= East;
}
if (direction.equals("West")) {
direction2= West;
}
if (direction.equals("South")) {
direction2= South;
}
else
{
direction2= North;
}
String wrldName = worldname + ".wld";
World.readWorld(wrldName);
//set world size?
World.setVisible(true);
World.setDelay(20);
}
}