2017-03-24 3 views
-2
public String toString(){ 

    Scanner keyboard = new Scanner(System.in); 

    System.out.println("Please enter 3 digits for your code."); 

    int a = keyboard.nextInt(); 
    int b = keyboard.nextInt(); 
    int c = keyboard.nextInt(); 

    String formatcode = "Clockwise " + a + "- Counter-Clockwise " + b + "- Clockwise " + c; 

    System.out.println(formatcode); 

    keyboard.close(); 
    return formatcode; 
} 

CombinationLock(int a, int b, int c){ 

    num1 = a; 
    num2 = b; 
    num3 = c; 

    open = true; 



} 

私はこれらの2つの方法を持っていますが、それらをドライバで一緒に動作させて初期化することはできません私のオブジェクトの変数。メソッドの後にコンストラクタが動作するのは異常ですが、私はこのようにするように求められました。任意のヒントやヒントが高く評価されています。2つの方法:1つは変数の値を定義し、もう1つはオブジェクトに値を属性付けるコンストラクタです

+1

私たちは 'toString'方法 – Ravi

+0

ToString関数に入力useer受け入れているようなコードを見ていないが、通常は使用されませんユーザーから何かを尋ねたコンストラクタで3桁の数字を尋ねて、引数を使用しないでください。 –

+0

コンストラクタで初期化される前にオブジェクトに対してメソッドを呼び出すことはできません。あなたがしようとしていることは不可能です。また、 'toString()'は、あなたがやったやり方で上書きしてはならないjavaオブジェクトの既存のメソッドです。 –

答えて

0

以下にコードを変更する必要があります。

public String toString(){ 

    return "Clockwise " + num1 + "- Counter-Clockwise " + num2 + "- Clockwise " + num3; 

    } 

CombinationLock(int a, int b, int c){ 

    num1 = a; 
    num2 = b; 
    num3 = c; 

    open = true; 
} 

そして、あなたの主な方法には、次の手順を実行します

Scanner keyboard = new Scanner(System.in); 

System.out.println("Please enter 3 digits for your code."); 

int a = keyboard.nextInt(); 
int b = keyboard.nextInt(); 
int c = keyboard.nextInt(); 
keyboard.close(); 

CombinationLock cl = new CombinationLock(a,b,c); 
System.out.println(cl); 

DEMO

+0

@downvoter任意のコメントをいただければ幸いです。 – Ravi

+0

私はdownvoterではありませんが、なぜあなたはclのメモリアドレスを印刷していますか? – abcOfJavaAndCPP

+0

次に、 'toString'をどのように使うのか教えてください。 – Ravi

0

をたぶん、あなたはこのような何かを試してみてください?

public String toString(){ 
    String formatcode = "Clockwise " + this.num1 + "- Counter-Clockwise " + this.num2 + "- Clockwise " + this.num3; 

    System.out.println(formatcode); 


    return formatcode; 
} 

CombinationLock(){ 
    Scanner keyboard = new Scanner(System.in); 

    System.out.println("Please enter 3 digits for your code."); 

    int a = keyboard.nextInt(); 
    int b = keyboard.nextInt(); 
    int c = keyboard.nextInt(); 

    this.num1 = a; 
    this.num2 = b; 
    this.num3 = c; 
    keyboard.close(); 
    this.open = true; 

}

1

これはこれはCombinationLockクラス

public class CombinationLock 
    { 
     private int a; 
     private int b; 
     private int c; 

     public CombinationLock(int a,int b,int c) 
     { 
      this.a=a; 
      this.b=b; 
      this.c=c; 
     } 
     public String toString() 
     { 
      String formatcode = "Clockwise " + a + "- Counter-Clockwise " + b + "- Clockwise " + c; 
      return formatcode; 
     } 

    } 
+0

これは情報が隠れているので最新の正しい書式になっています – abcOfJavaAndCPP

0

はのクラスCombinationLockを定義することから始めましょうです

import java.util.Scanner; 
    public class Driver 
    { 
     public static void main(String[] args) 
     { 
      Scanner keyboard = new Scanner(System.in); 

      System.out.println("Please enter 3 digits for your code."); 

      int a = keyboard.nextInt(); 
      int b = keyboard.nextInt(); 
      int c = keyboard.nextInt(); 
      CombinationLock cl = new CombinationLock(a,b,c); 
      System.out.println(cl.toString()); 
     } 


    } 

メインアプリケーションクラスである私の答え を試しください。

コンビネーションロックには、ロックを解除するために必要な3つの番号があります。これらは、ロックを構築するときに のいずれかを渡すか、ロックにハードコードすることができます。また、ロックを解除する方法も含まれています。実装は次のようになります。

public class CombinationLock { 
    int num1; 
    int num2; 
    int num3; 
    boolean locked = true; 

    public CombinationLock(int num1, int num2, int num3) { 
     this.num1 = num1; 
     this.num2 = num2; 
     this.num3 = num3; 
    } 

    //creates a new combinationlock with default code 1-2-3 
    public CombinationLock(){ 
     this(1,2,3); 
    } 

    public void unlock(int a, int b, int c){ 
     if(a==num1 && b==num2 && c==num3){ 
      locked = false; 
     } 
    } 

    public void lock(){ 
     locked = true; 
    } 

    //toString method's are reserved to describe the object as a String. 
    @Override 
    public String toString(){ 
     String result = "A combination lock with a secret code. "; 
     if(locked){ 
      result += "It is currently locked"; 
     } else { 
      result += "It is currently open"; 
     } 
     return result;     
    } 
} 

このロックはどのように使用しますか?ユーザーに3つの数字を尋ね、ロックを解除しようとするもう1つの方法を追加しましょう。今度は、私たちのロックを試してmainメソッドを書いてみましょう

public void promptUser(){ 
    Scanner keyboard = new Scanner(System.in); 

    System.out.println("Please enter 3 digits for your code."); 

    int a = keyboard.nextInt(); 
    int b = keyboard.nextInt(); 
    int c = keyboard.nextInt(); 

    System.out.println(); 
    System.out.println("Clockwise " + num1 + "- Counter-Clockwise " + 
         num2 + "- Clockwise " + num3); 

    unlock(a,b,c); 
} 

public static void main(String[] args) throws Exception { 
    CombinationLock lock = new CombinationLock(3,5,2); 
    System.out.println(lock); 
    lock.promptUser(); 
    System.out.println(lock); 
} 


A combination lock with a secret code. 
It is currently locked 
Please enter 3 digits for your code. 
3 
5 
2 

You try turning Clockwise 3- Counter-Clockwise 5- Clockwise 2 
A combination lock with a secret code. 
It is currently open 
+0

あなたのコメントの後に、何が期待されたのかを明確にしました.Raviの答えが良いです。 – Imus

関連する問題