-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

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 に答える