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