无法计算这些编译器错误(Java)

时间:2013-09-01 18:01:19

标签: java syntax

这是我的整个代码。我得到了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

2 个答案:

答案 0 :(得分:1)

通常你必须返回一个布尔值(true或false),因为你将方法的返回值声明为boolean,但你没有,所以将它改为void。

将此添加到您的类块:

public int revoCount;

更改第266行及以后的序号。到:

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

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

这应该删除大多数错误,但是你应该看看如何用Java定义类。

答案 1 :(得分: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;