为什么不打印“Num”圈子?
该代码应打印出用户在“Num”处输入的圆圈数量,并给它们一个随机半径和中心点,但它不会绘制任何东西。该applet本身只显示“Applet开始”。任何解决方案为什么不打印“Num”圈子?
编辑:此外,打开的小程序不会关闭,直到Eclipse本身关闭。 编辑:更新的代码。
import java.applet.Applet;
import java.awt.*;
import java.util.Random;
public class project10 extends Applet {
ConsoleReader console = new ConsoleReader(System.in);
Random generator = new Random();
private static final long serialVersionUID = -3660618513445557612L;
public void drawCenteredCircle(Graphics g, int x, int y, int r) {
g.fillOval(x,y,r,r);
}
public void paint(Graphics g){
int num = 5;
try{
System.out.println("How many circles would you like to draw?");
//num = console.readInt();
for(int i = 1; num >= i; i++){
g.setColor(Color.BLACK);
int one = generator.nextInt(100);
int two = generator.nextInt(100);
int three = generator.nextInt(100);
drawCenteredCircle(g,one,two,three);
}
}catch(NumberFormatException e){
System.out.println("Input was not a valid number, please try again.");
}finally{
System.out.println("You printed " + num + " circles");
}
}
}
控制台读取器类:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
/**
A class to read strings and numbers from an input stream.
This class is suitable for beginning Java programmers.
It constructs the necessary buffered reader,
handles I/O exceptions, and converts strings to numbers.
*/
public class ConsoleReader
{ /**
Constructs a console reader from an input stream
such as System.in
@param inStream an input stream
*/
public ConsoleReader(InputStream inStream)
{ reader = new BufferedReader
(new InputStreamReader(inStream));
}
/**
Reads a line of input and converts it into an integer.
The input line must contain nothing but an integer.
Not even added white space is allowed.
@return the integer that the user typed
*/
public int readInt()
{ String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
/**
Reads a line of input and converts it into a floating-
point number. The input line must contain nothing but
a nunber. Not even added white space is allowed.
@return the number that the user typed
*/
public double readDouble()
{ String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
/**
Reads a line of input. In the (unlikely) event
of an IOException, the program terminates.
@return the line of input that the user typed, null
at the end of input
*/
public String readLine()
{ String inputLine = "";
try
{ inputLine = reader.readLine();
}
catch(IOException e)
{ System.out.println(e);
System.exit(1);
}
return inputLine;
}
private BufferedReader reader;
}
你ConsoleReader类以某种方式阻止,也可能是错误的,基本应该解决您的问题。
替换:
ConsoleReader console = new ConsoleReader(System.in);
与
Scanner console = new Scanner(System.in);
还记得,你必须按下输入您所输入的号码后,您必须在控制台输入它,而不是在小程序窗口。
编辑: 由于小应用程序查看器正在关注焦点,所以必须在控制台中按下然后编写才能输入。 (这可能也适用于您的其他控制台输入类)
这仍然无法正常工作。 – sirnomnomz
我添加了控制台阅读器类以供审阅@spydon – sirnomnomz
您是否能够提供您的输入?您是否在控制台中看到您的号码,并显示“您打印了#个圈子”的文字? – spydon
该代码适用于我,您可能在HTML文件中发生了错误?你可以在eclipse中的applet查看器中运行它吗? – spydon
这就是我最初运行它的地方@spydon – sirnomnomz
如果你设置num = 5并且不对你的ConsoleReader类进行调用,它是否工作? – spydon