2017-04-13 35 views
1

Algorithms Courseraアルゴリズムコースで提供されているPrincetonライブラリのStdIn.isEmpty()メソッドを使用していますが、その動作については混乱しています。私はこの声明を持っていますStdIn.isEmpty()をtrueに戻す方法を教えてください。

while (!StdIn.isEmpty()) 

ユーザ入力を読み取るコードが入っていますが、私はループから脱出することはできません。標準入力が空でwhileループが壊れているはずであることを意味するテキストを入力せずにEnterキーを押したかどうかを理解することで私はのisEmpty用のコードを見上げたが、それは何かを明確にしませんでした:

public static boolean isEmpty() { 
    return !scanner.hasNext(); 
} 

誰かが入力がどのように動作するか、標準明確にし、私はこの方法の私の誤解を修正するのに役立つことはできますか?

+0

は、Ctrl + Zを入力してみてください。 – shmosel

+0

私はこれを試しました。私はマックbtwです。 – RB34

+0

私はisEmptyの代わりにhasNextLineを使うべきだと思います。プログラムを停止するには、別のウィンドウでkillコマンドを試してください(https://www.howtogeek.com/209658/how-to-force-applications-and-processes-to-quit-on-os-x/)。 –

答えて

0

私は、MacではCMD+D⌘+D)を使用してシェルの入力の終了をマークできます。参照:https://www.jetbrains.com/help/idea/debug-tool-window-console.html

キーボードショートカット

を⌘Dキーの組み合わせは、あなたがそれ以上のデータがデータソースから読み取ることができないこと 信号にEOF(ファイルの終わり)、すなわちを送信することができます。アルゴリズムで提供プリンストンライブラリの問題で

- パート1コーセラのコースでは、あなたが特にクラス import edu.princeton.cs.algs4.StdIn;で、自分のコードでビットを再生することができます。そのコードを使用して、その実装は、おそらく概念と遊ぶのが最も洗練されていません。

while (!StdIn.isEmpty()) { 
    int p = StdIn.readInt(); 
    int q = StdIn.readInt(); 

    if (!uf.connected(p, q)) { 
     uf.union(p, q); 
     StdOut.println(p + " " + q); 
    } 
} 

は、ここに私がやったことだ:IntelliJのに新しいJavaプロジェクトを作成し

  1. (新しいJavaパッケージを作成します。
  2. メインメソッドを持つ新しいクラスを作成します。
  3. プロジェクトにlibsフォルダを作成しました
  4. http://algs4.cs.princeton.edu/code/algs4.jarからコードをダウンロードし、libsフォルダにjarを置いた
  5. プロジェクトにIntelliJのプロジェクトタブを追加し、プロジェクトを右クリック>モジュール設定を開く>依存関係タブをクリック> JARまたはディレクトリ...上記のlibsフォルダからjarを選択
  6. シェルでより良いユーザインタラクションができるように、上記のwhileループのコードを変更しました。ここでは、isEmpty()を使用する代わりに、p入力終了を示すためにはqを0とする。

これを実行するには、メインの静的メソッドを持つクラスを持つコードを右クリックし、実行(Ctrl + Shift + R)を選択します。

これは恐ろしいCMD + Dとあなたは、whileループの後に多くのコードを書きたい場合はそれで行くいくつかの問題を避けることができます - たとえば、私は2つのオブジェクトが接続されているかどうかを確認するためのコードを追加しました:

StdOut.println("Check if 2 objects are connected: "); 
try { 
    Scanner reader = new Scanner(System.in); // Reading from System.in 
    System.out.println("Enter first object: "); 
    int n = reader.nextInt(); // Scans the next token of the input as an int. 
    System.out.println("Enter second object: "); 
    int m = reader.nextInt(); // Scans the next token of the input as an int. 
    StdOut.println(uf.connected(n, m) ? "Connected" : "Not Connected"); 
} catch(Exception e) { 
    // whatever... 
} 
ここ

は問題に私の非常に迅速かつ汚いアプローチです:

package com.algorithms; 

import edu.princeton.cs.algs4.StdIn; 
import edu.princeton.cs.algs4.StdOut; 
import edu.princeton.cs.algs4.UF; 

import java.util.Scanner; 

public class Week1 { 
    private static UF uf; 

    public static void main(String[] args) 
    { 
     StdOut.println("Enter the total number of objects: "); 
     int N = StdIn.readInt(); 

     StdOut.println("Enter the unions between any of those objects..."); 

     Week1.uf = new UF(N); 
     while (true) { 

      int p = StdIn.readInt(); 
      int q = StdIn.readInt(); 

      if (!uf.connected(p, q)) { 
       Week1.uf.union(p, q); 
       StdOut.println(p + " " + q); 
      } 

      if(p==0 && q==0) break; 
     } 
     checkIfConnected(); 
    } 

    private static void checkIfConnected() 
    { 
     StdOut.println("Check if 2 objects are connected: "); 
     try { 
      Scanner reader = new Scanner(System.in); // Reading from System.in 
      System.out.println("Enter first object: "); 
      int n = reader.nextInt(); // Scans the next token of the input as an int. 
      System.out.println("Enter second object: "); 
      int m = reader.nextInt(); // Scans the next token of the input as an int. 
      StdOut.println(Week1.uf.connected(n, m) ? "Connected" : "Not Connected"); 
     } catch(Exception e) {} 
    } 
} 
関連する問題