-2

这是我的整个代码。我遇到了 java 语法错误、重复的方法,而且我似乎无法弄清楚其中的大部分。任何帮助表示赞赏。这是一个旨在模拟操作锁的程序。

另外,这是第一次在这里发布海报,所以如果您需要我的其他任何东西,请告诉我。谢谢。

编辑:在底部添加错误。另外,我使用 DrJava.jar 作为我的程序来实现它。

EDIT2:更新了修改后的代码和当前错误。

import java.util.Scanner;

public class Lock{
private static int first;
private static int second;
private static int third;
private Boolean isClosed;
// private static int[] dial = new int[40];           NO LONGER IMPLEMENTING ARRAY
private static int activeNumber = 0;
public int revoCount = 0;
public int count;
public int tempFirst;
public int tempSecond;
public int tempThird;
private Boolean isClockwise;
private int desiredNumber;

Scanner in = new Scanner(System.in);

/**for (int i = 0; i < 40; i++)
{                                                     NO LONGER IMPLEMENTING ARRAY
  dial[i] = i;
} **/

//*****************************************************************************************************

public Lock()
{ 
  first = 1;
  second = 2;
  third = 3; 
}

//*****************************************************************************************************

public void alterCombo(int x, int y, int z)
{ 
  first = x;
  second = y;
  third = z;
  System.out.println("The correct combination needed has been changed to: " + first +     ", " + second + ", " + third);
  return; 
}

    //*****************************************************************************************************

public void turnLock()
{       
  System.out.print("This is a lock that goes from 0 to 39. You must turn the knob clockwise first, then counterclockwise twice, ");
  System.out.print("then clockwise for the final input. Specify how many revolutions you want (Positive number indicates ");
  System.out.println("COUNTER CLOCKWISE. Negative number indicates CLOCKWISE.");

  turnFirstNumber();
  turnSecondNumber();
  turnThirdNumber();

    System.out.println("The combination you chose was: " + tempFirst + ", " +     tempSecond + ", and " + tempThird + ".");
  return;
}
    //*****************************************************************************************************

public void closeLock()
{ 
  if (isClosed)
    System.out.println("Lock is already closed.");
  else
  {
    System.out.println("Lock has been closed.");
    isClosed = true;
  }
  return; 
}

//*****************************************************************************************************

public void openLock()
{ 
  if ((turnFirstNumber()) && (turnSecondNumber()) && (turnThirdNumber()) && (isClosed)) //if all 3 passed and lock is not open already
  { 
    isClosed = false;
    System.out.println("Your combination is correct and the lock has been opened.");
    return; 
  }
  else if (!isClosed)                                                        //lock's already open
  { 
    System.out.println("The lock is already open.");
    return;
  }
  else if ((!turnFirstNumber()) && (turnSecondNumber()) && (turnThirdNumber())) //first wrong
  {
    System.out.println("The first number you input is incorrect.");
    return;
  }
  else if ((!turnFirstNumber()) && (!turnSecondNumber()) && (turnThirdNumber())) //first and second wrong
  {
    System.out.println("The first 2 numbers you input are incorrect.");
    return;
  }
  else if ((!turnFirstNumber()) && (turnSecondNumber()) && (!turnThirdNumber())) //first and third wrong
  {
    System.out.println("The first and last numbers you input are incorrect.");
    return;
  }
  else if ((turnFirstNumber()) && (turnSecondNumber()) && (!turnThirdNumber())) //third wrong
  {
    System.out.println("The last number you input is incorrect.");
    return;
  }
  else if ((turnFirstNumber()) && (!turnSecondNumber()) && (!turnThirdNumber())) //second and third wrong
  {
    System.out.println("The second and last numbers you input are incorrect.");
    return;
  }
  else if ((turnFirstNumber()) && (!turnSecondNumber()) && (turnThirdNumber())) //second is wrong
  {
    System.out.println("The second number you input is incorrect.");
    return;
  }
  else
  { 
    System.out.println("Your entire combination is INCORRECT. Please try again."); //all wrong
    return; 
  }
}

  //*****************************************************************************************************

public void lockStatus()   
{
  if (isClosed)
  { 
    System.out.println("Closed");
    return;
  }
  else
  { 
    System.out.println("Open");
    return;
  }
}
 //*****************************************************************************************************

public static int getActiveNumber()
{
  return activeNumber;
}

//*****************************************************************************************************

private boolean turnFirstNumber()
{
  System.out.print("What is your desired direction and number of revolutions? (Positive number is counterclockwise, negative number is clockwise): ");
  count = in.nextInt();
  if (count > 0)
    isClockwise = false;
  else if (count < 0)
    isClockwise = true;
  else
  {
    throw new InvalidInputException("Your desired direction of spinning the lock is invalid. Please choose a number other than 0.");
    return false;
  }
  System.out.print("\nWhat is your desired first number?: ");
  desiredNumber = in.nextInt();

  if (!isClockwise) //user desires countercockwise revolution
  {
    do {
      for (int i = 0; i < (count * 40); i++)
      {
        activeNumber++;
          if (activeNumber > 39)
            activeNumber = 0;
          if (activeNumber == desiredNumber)
            revoCount++;
      }
       } while ((activeNumber != desiredNumber) && (revoCount < count));
  }      
  else if (isClockwise) //user desires clockwise revolution
  {
    do {
      for (int i = 0; i < (Math.abs(count) * 40); i++)
      {
      activeNumber--;
        if (activeNumber < 0)
          activeNumber = 39;
        if (activeNumber == desiredNumber)
          revoCount++;
      }
       } while ((activeNumber != desiredNumber) && (revoCount < Math.abs(count)));
  }

  tempFirst = activeNumber;

  if ((activeNumber == first) && (count < 0)) //if first number is correct and user picked correct orientation and revolutions
    return true;
  else
    return false;
  revoCount = 0;
}

//*****************************************************************************************************

private boolean turnSecondNumber()
{
  System.out.print("What is your desired direction and number of revolutions? (Positive number is counterclockwise, negative number is clockwise): ");
  count = in.nextInt();
  if (count > 0)
    isClockwise = false;
  else if (count < 0)
    isClockwise = true;
  else
  {
    throw new InvalidInputException("Your desired direction of spinning the lock is invalid. Please choose a number other than 0.");
    return false;
  }
  System.out.print("\nWhat is your desired second number?: ");
  desiredNumber = in.nextInt();

  if (!isClockwise) //user desires countercockwise revolution
  {
    do {
      for (int i = 0; i < (count * 40); i++)
      {
        activeNumber++;
          if (activeNumber > 39)
            activeNumber = 0;
          if (activeNumber == desiredNumber)
            revoCount++;
      }
       } while ((activeNumber != desiredNumber) && (revoCount < count));
  }      
  else if (isClockwise) //user desires clockwise revolution
  {
    do {
      for (int i = 0; i < (Math.abs(count) * 40); i++)
      {
      activeNumber--;
        if (activeNumber < 0)
          activeNumber = 39;
        if (activeNumber == desiredNumber)
          revoCount++;
      }
       } while ((activeNumber != desiredNumber) && (revoCount < Math.abs(count)));
  }

  tempSecond = activeNumber;

  if ((activeNumber == second) && ((count == "+2") || (count == "2"))) //if second number is correct and user picked correct orientation and revolutions
    return true;
  else
    return false;
  revoCount = 0;
}

//*****************************************************************************************************

private boolean turnThirdNumber()
{
  System.out.print("Enter '1' to twist the dial counterclockwise until you reach your desired number. Enter '-1' to twist the dial clockwise until you reach your desired number.: ");
  count = in.nextInt();
  if ((count == "1") || (count == "+1"))
    isClockwise = false;
  else if (count == "-1")
    isClockwise = true;
  else
  {
    throw new OutOfBoundsException("You are not supposed to do a full revolution on the third number of the combination. Now you have to restart.");
    return false;
  }
  System.out.print("\nWhat is your desired third and final number?: ");
  activeNumber = in.nextInt();
  activeNumber = Math.abs(activeNumber);
  tempThird = activeNumber;
  /** if (!isClockwise) //user desires countercockwise revolution
  {
    do {
        activeNumber++;
          if (activeNumber > 39)
            activeNumber = 0;
       } while (activeNumber != desiredNumber)
  } 
  else if (isClockwise) //user desires clockwise revolution
  {
    do {
      for (int i = 0; i < (count * 40); i++)
      {
      activeNumber--;
        if (activeNumber < 0)
          activeNumber = 39;
       } while (activeNumber != desiredNumber)
  }                                                                           REDUNDANT. Same result, more work.*/
  if (activeNumber > 39)
  {
    throw new OutOfBoundsException("You desire a number that is not on the lock. The lock goes from 0 to 39. Try again.");
    return false;
  }
  if ((activeNumber == third) && (isClockwise)) //if third number is correct and user picked correct orientation and revolutions
    return true;
  else    
    return false;
}

//*****************************************************************************************************
}

这些是错误:

9 errors found:
File: C:\Users\Alex\Java Source Files\Lock.java  [line: 165]
Error: InvalidInputException cannot be resolved to a type
File: C:\Users\Alex\Java Source Files\Lock.java  [line: 219]
Error: InvalidInputException cannot be resolved to a type
File: C:\Users\Alex\Java Source Files\Lock.java  [line: 254]
Error: Incompatible operand types int and java.lang.String
File: C:\Users\Alex\Java Source Files\Lock.java  [line: 254]
Error: Incompatible operand types int and java.lang.String
File: C:\Users\Alex\Java Source Files\Lock.java  [line: 267]
Error: Incompatible operand types int and java.lang.String
File: C:\Users\Alex\Java Source Files\Lock.java  [line: 267]
Error: Incompatible operand types int and java.lang.String
File: C:\Users\Alex\Java Source Files\Lock.java  [line: 269]
Error: Incompatible operand types int and java.lang.String
File: C:\Users\Alex\Java Source Files\Lock.java  [line: 273]
Error: OutOfBoundsException cannot be resolved to a type
File: C:\Users\Alex\Java Source Files\Lock.java  [line: 300]
Error: OutOfBoundsException cannot be resolved to a type
4

2 回答 2

1

通常您必须返回一个布尔值(true 或 false),因为您将方法的返回值声明为布尔值,但您没有,因此将其更改为 void。

将此添加到您的课程块中:

public int revoCount;

更改第 266 行等。至:

if (count == 1)  //there is no difference between int with the value 1 and +1
isClockwise = false;

最后:删除最后一个返回:类定义不以返回结尾。

这应该会消除大部分错误,但您应该看看如何在 Java 中定义类。

于 2013-09-01T18:27:42.737 回答
0

查看编译错误中显示的信息。例如

这些变量声明turnLock应该在类块中

public boolean isClockwise = false;
public int tempFirst = 0;
public int tempSecond = 0;
public int tempThird = 0;
public int desiredNumber = 0;
public int count = 0;

revoCount从未声明过

private int revoCount;

do-while并且您的语句中有许多缺少的分号

} while ((activeNumber != desiredNumber) && (revoCount < Math.abs(count)));
                                            add this ---------------------^

编译器找不到该类InvalidInputException。确保它在编译路径上。同样适用于OutOfBoundsException

删除return紧跟在语句之后的任何throw语句 - 这些语句是无法访问的,例如

throw new InvalidInputException("Your desired direction of spinning the lock is invalid. Please choose a number other than 0.");
return false;
于 2013-09-01T18:09:43.983 回答