Quantcast
Channel: 質問!ITmedia 新着質問(Java/253)
Viewing all articles
Browse latest Browse all 2439

後置インクリメントの挙動が不明で困ってます

$
0
0
 Eclipse 4.7 Java 8でプログラミングをやっています。 いまスレッドのプログラミングをやっているのですが、後置インクリメント の挙動がC言語の時と違っていて戸惑っています。 問題は、変数 idx でこの変数の値が加算されていきません。 条件演算子がある箇所で加算しているのですが、後置インクリメントでも 加算されていくはずですよね? しかしそうなりません。値は0のままです。 なぜそうなるのか分からないので質問しました。 答えられたらよろしくお願いします。 import static java.lang.System.out; import java.applet.Applet; import java.awt.Graphics; public class JavaThread4 extends Applet implements Runnable { // TODO 自動生成されたメソッド・スタブ final String HelloWorld = "Hello World"; volatile int idx; Thread helloThread = null; public void init() { idx = 0; out.println("init"); } public void paint(Graphics g) { g.drawString(HelloWorld.substring(0, idx), 30, 30); } public void start() { out.println("start"); if(helloThread == null) { helloThread = new Thread(this); helloThread.start(); } } public void run() { out.println("run"); for(;;) { try { Thread.sleep(400); }catch(InterruptedException e) { } out.println(" idx = " + idx); idx = (idx < HelloWorld.length()) ? idx++ : 0;//この部分が問題の部分です repaint(); } } }

Viewing all articles
Browse latest Browse all 2439

Trending Articles