我在做一条练习时,要编写一个applet程式,利用FOR循环结构.读入从难从1到30之间的5个整数,并对每个读入的数打印机出一行含有同样数目的连续"*"号.比如,若读入的一个数为5,则应打印出"*****".
以下为我写的程式.
import javax.swing.JOptionPane;
import javax.swing.JApplet;
import java.awt.Graphics;public class Test30 extends JApplet{ public void init () {
String N;
int n=1,count=1;
N = JOptionPane.showInputDialog("Enter N:");
n = Integer.parseInt(N);
while(n<=30){
if(count<=5){
N = JOptionPane.showInputDialog("Enter N:");
n = Integer.parseInt(N); for(int j=1;j<n;j++)
{ System.out.print("*");
}
System.out.print("");
count++;
}
n++;
}
}
public void paint (Graphics g)
{
g.drawString(???);
}
}
不知道在HTML中如何输出,只能在DOS下先输出.请指教一下.谢谢!
因为小弟已经无分了,请各位包涵.
以下为我写的程式.
import javax.swing.JOptionPane;
import javax.swing.JApplet;
import java.awt.Graphics;public class Test30 extends JApplet{ public void init () {
String N;
int n=1,count=1;
N = JOptionPane.showInputDialog("Enter N:");
n = Integer.parseInt(N);
while(n<=30){
if(count<=5){
N = JOptionPane.showInputDialog("Enter N:");
n = Integer.parseInt(N); for(int j=1;j<n;j++)
{ System.out.print("*");
}
System.out.print("");
count++;
}
n++;
}
}
public void paint (Graphics g)
{
g.drawString(???);
}
}
不知道在HTML中如何输出,只能在DOS下先输出.请指教一下.谢谢!
因为小弟已经无分了,请各位包涵.
import javax.swing.JOptionPane;
import javax.swing.JApplet;
import java.awt.Graphics;public class Test30 extends JApplet{
public void init () {
String N;
for(int n=1;n<=30;n++)
{
for(int count=1;count<=5;count++)
{
N = JOptionPane.showInputDialog("Enter N:");
n = Integer.parseInt(N);
if(n<count)
System.out.print("*");
else
System.out.print("\n");
}
System.out.println(" ");
}
System.exit(0);
} }
请指点我一下.
如果是在想在applet中画图,你不应该在init里画,要在paint方法里,用g.drawString()方法来画。
用g.drawString()的方法,如何编写呢?请指导一下.
而且我自己写的程式应该有错误,请指导一下.
谢谢!
import java.awt.Graphics;
import javax.swing.JOptionPane;public class Test30 extends JApplet{
public void init () {
String N,star="";
int n;
N = JOptionPane.showInputDialog("Enter N:");
n = Integer.parseInt(N);
for(int m=30;m>=1;m--)
{
for(int count=1;count<=5;count++)
{
N = JOptionPane.showInputDialog("Enter N:");
n = Integer.parseInt(N); if(n<m)
star += "*";
}
}
System.exit(0);
}
public void paint (Graphics g)
{
g.drawString(star+"\n",25,25);
}}
我又写了一个,这个应该有改善了.请指导我一下,谢谢!