私は、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プロジェクトを作成し
-
- (新しいJavaパッケージを作成します。
- メインメソッドを持つ新しいクラスを作成します。
- プロジェクトにlibsフォルダを作成しました
- http://algs4.cs.princeton.edu/code/algs4.jarからコードをダウンロードし、libsフォルダにjarを置いた
- プロジェクトにIntelliJのプロジェクトタブを追加し、プロジェクトを右クリック>モジュール設定を開く>依存関係タブをクリック> JARまたはディレクトリ...上記のlibsフォルダからjarを選択
- シェルでより良いユーザインタラクションができるように、上記の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) {}
}
}
は、Ctrl + Zを入力してみてください。 – shmosel
私はこれを試しました。私はマックbtwです。 – RB34
私はisEmptyの代わりにhasNextLineを使うべきだと思います。プログラムを停止するには、別のウィンドウでkillコマンドを試してください(https://www.howtogeek.com/209658/how-to-force-applications-and-processes-to-quit-on-os-x/)。 –