import javaappletApplet;
import javaawtButton;
import javaawtColor;
import javaawtGraphics;
import javaawtTextField;
import javaawteventActionEvent;
public class Nicki extends Applet{
private static final long serialVersionUID = 1L;
private Button ok;
private int num=32;
private int resu=0;
private boolean isRig=false;
private TextField iPut;
public Nicki(){
thissetLayout(null);
ok=new Button("OK");
oksetActionCommand(getName());
oksetBounds(150, 150, 40, 20);
iPut=new TextField();
thisadd(iPut);
iPutsetBounds(100, 150, 40, 20);
thisadd(ok);
okaddActionListener(new ButtonAction(this));
}
public void paint(Graphics g){
gsetColor(Colorwhite);
gfillRect(0, 0, thisgetWidth(), thisgetHeight());
gsetColor(ColorBLACK);
gdrawString("Please guess a number ", 10, 20);
gdrawString("between 1 and 100", 10, 40);
if(isRig==false&&resu!=0){
if(resu>num){
gdrawString(""+resu+" is too big !", 10, 100);
}else if(resu<num){
gdrawString(resu+"is too small !", 10, 100);
}
}else if(isRig==true){
gsetColor(ColorGREEN);
gdrawString("Yes,"+resu+" is the right number", 10, 80);
gdrawString("Your are great! ", 10, 100);
gsetColor(Colorred);
gdrawString(resu+"!", 70, 120);
}
iPutsetText("");
gdrawString("Input the number:", 0, 150);
}
public void ButtonActionPerformed(ActionEvent e){
if(egetActionCommand()equals("panel0")){
resu=IntegerparseInt(iPutgetText());
if(num==resu){
isRig=true;
}else{
isRig=false;
}
repaint();
}
}
}
class ButtonAction implements javaawteventActionListener{
Nicki su;
public ButtonAction(Nicki bun){
thissu=bun;
}
public void actionPerformed(ActionEvent e) {
suButtonActionPerformed(e);
}
}
求杨辉三角吧,代码如下:
public class YhuiTriangle {
public static int binomial(int n,int k){
int a = 1,b = 1;
if(k == 0)return 1;
for(int i = 1 ;i<=k;i++){
b = bi;
a = a (n-i+1);
}
return a/b;
}
public static void main(String args[]){
Systemoutprintln(" "+1);
for(int n = 2;n<=10;n++){
for(int i = 9;i>=n;i--)Systemoutprint(" ");
for(int k = 0; k<n ;k++)Systemoutprint(binomial(n,k)+" ");
Systemoutprintln(1);
}
}
}
//这个算法还可以在时间上进行改进
先写第三题,为了简便,我用的是对话框输入值,你可以将其改成标准输入,里面的int一句不是没有用,而是确定它是不是整数。代码如下:
/
@(#)IntegerBackWardjava 2007-11-27
/
package cncomrobertbaidu;
import javaawtHeadlessException;
import javaxswingJOptionPane;
/
@author R
/
public class IntegerBackWard {
/
@param args
/
public static void main(String[] args) {
try {
String numOfInteger=JOptionPaneshowInputDialog(null, "Enter A Integer Sized Five", null, JOptionPaneQUESTION_MESSAGE);
int output=IntegerparseInt(numOfInteger);
writeBackward(numOfInteger,5);
} catch (HeadlessException e) {
eprintStackTrace();
} catch (NumberFormatException e) {
eprintStackTrace();
}
}
public static void writeBackward(String s, int size){
if(size>0){
Systemoutprint(ssubstring(size-1, size));
writeBackward(s,size-1);
}
}
}
以下是第4题
/
@(#)AddForjava 2007-11-27
/
package cncomrobertbaidu;
/
@author R
/
public class AddFor {
/
@param args
/
public static void main(String[] args) {
int result1=0, result2=0;
for(int i=1;i<=100;i++){
result1+=i;
}
Systemoutprintln("Use for");
Systemoutprintln("1+2+…+100="+result1);
int j=1;
while(j<=100){
result2+=j;
j++;
}
Systemoutprintln("Use while");
Systemoutprintln("1+2+…+100="+result2);
}
}
欢迎分享,转载请注明来源:表白网
评论列表(0条)