「昔、コンピュータはテキストしか表示できませんでした。プログラムは、キーボードからの入力を受け取った後、画面にデータを表示しました。これは、「コンソール ユーザー インターフェイス」または単に「コンソール」と呼ばれます。ウィンドウ インターフェイスは、コンソールの代替品です。このタイプのインターフェイスでは、ユーザーは 1 つまたは複数のウィンドウを通じてプログラムと対話します。私たちはプログラミングの方法を学んでいるだけなので、コンソールで作業することから始めます。」

"わかった。"

「テキストはコンソール (画面) に 1 行ずつ連続して表示されます。テキストはキーボードを使用して入力されます。間違いを避けるために、キーボード入力は画面に表示されます。人間のユーザーとプログラムが交互に表示されることがあります。画面に何かを書くこと。

「 System.out.print () メソッドを使用すると、画面上にテキストを表示できます。このメソッドは単にテキストを表示するだけですが、System.out.println () はテキストを表示し、カーソルを次の行に移動します。」

コード 結果
System.out.print("Rain");
System.out.print("In");
System.out.print("Spain");
スペインの雨
System.out.print("Rain");
System.out.println("In");
System.out.print("Spain");

スペインの雨
System.out.println("Rain");
System.out.println("In");
System.out.println("Spain");
スペイン
の雨

「テキストの断片を分離するには、スペースを追加する必要があります。例:」

コード 結果
int a = 5, b = 6;
System.out.print(a);
System.out.print(b);
56
int a = 5, b = 6;
System.out.print(" " + a + " " + b);
 5 6
int a = 5, b = 6;
System.out.print("The sum is " + (a + b));
The sum is 11

"とった"

「これにより、画面上に何でも表示できるようになります。 すべての Java オブジェクトは文字列に変換できます。すべての Java クラスは、toString() メソッドを持つ Object クラスから派生します。このメソッドは、オブジェクトを文字列に変換するときに呼び出されます。弦。"

コード 説明
Cat cat = new Cat("Oscar");
System.out.println("The cat is " + cat);
これら 3 つの例は同等です。
Cat cat = new Cat("Oscar");
System.out.println("The cat is " + cat.toString());
Cat cat = new Cat("Oscar");
String catText = cat.toString();
System.out.println("The cat is " + catText);
3
タスク
Java Syntax,  レベル 3レッスン 3
ロック未解除
Display right away
Programming isn't always difficult. Sometimes you need to do simple tasks. And the more similar tasks you have to do, the more you want to automate them. Let's implement one more method. Ultimately, objects without methods don't do anything. We'll pass the string s to the method, which will then display it on the screen.

「しかし、私のプログラムでは『The cat is com.codegym.lesson3.Cat@1fb8ee3』と表示されました。これは一体何を意味するのでしょうか?」

「Object クラスの標準 toString() メソッドは、クラス名オブジェクトのメモリ アドレス(16 進数形式) で構成される文字列を返します。」

「うーん。それで、そんな方法で何の役に立つの?」

「クラス内に toString() の独自の実装を記述することができます。そうすれば、それが呼び出されるメソッドになります。」

「そうですか?大丈夫です。」

「ディエゴからいくつかのタスクがあります。」

3
タスク
Java Syntax,  レベル 3レッスン 3
ロック未解除
Currency exchange
Loan sharking, sales, banking: do you have a backup plan if programming doesn't work out? No? In that case, let's help traders and financiers with our top-notch programs. First, we'll organize a currency exchange: write a program that converts euros to dollars at a given exchange rate.
3
タスク
Java Syntax,  レベル 3レッスン 3
ロック未解除
Task with percentages
If you weren't afraid of problems with percentages in school, then this problem won't scare you either. But if you do suddenly find yourself afraid, it's time to face your fear and realize that this is actually a simple and pleasant topic. Let's implement a method that increases the passed integer by 10 percent.
1
タスク
Java Syntax,  レベル 3レッスン 3
ロック未解除
Code entry
Your attention, please! Now recruiting code entry personnel for CodeGym. So turn up your focus, let your fingers relax, read the code, and then... type it into the appropriate box. Code entry is far from a useless exercise, though it might seem so at first glance: it allows a beginner to get used to and remember syntax (modern IDEs seldom make this possible).