public class Hello{
public static int max = 0;
static{
System.out.println("*********猜数游戏***********");
}
public static void main(String[] args){
try{
new Hello(Hello.syso());
}catch(Exception e){
System.out.println("输入错误,请输入整数.");
new Hello(Hello.syso());
}
}
public static int syso(){
java.util.Scanner sc = new java.util.Scanner(System.in);
return sc.nextInt();
}
public Hello(int a){
//产生随机数
int suiJi = new java.util.Random().nextInt(101)+1;
switch(max){
case 0:
case 1:
case 2:
if(suiJi == a){
System.out.println("猜中了...");
}else{
System.out.println("请再猜.");
Hello.max = Hello.max + 1;
if(max == 3){
System.out.println("只有三次机会.");
}else{
new Hello(Hello.syso());
}
}
break;
}
}
}
package com.isoftstone.interview.traffic;
import java.util.Random;
import java.util.Scanner;
public class BullsandCows {
private static int lucknum;
/**
* 随机产生1-99的一个数字
* @return
*/
public static int makeNum(){
lucknum = new Random().nextInt(99) + 1;
return lucknum;
}
/**
* @param args
*/
public static void main(String[] args) {
BullsandCows.makeNum();
System.out.println("幸运数字是:" + lucknum);
int count = 1;
while(count < 4){
System.out.print("请输入:");
Scanner objScanner = new Scanner(System.in);
int putnum = objScanner.nextInt();
if(putnum == lucknum){
System.out.println("你真棒!猜对了!");
break;
}else if(putnum > lucknum){
System.out.println("你猜大了");
}else{
System.out.println("你猜小了");
}
if(count == 3){
System.out.println("游戏结束!");
}
count++;
}
}
}
//编写一个字符界面的Java Application 程序,接受用户从键盘输入的一个正整数,然后统计并输出从1到这个正整数的累加和。
import java.awt.*;
import java.awt.event.*;
public class DS implements ActionListener
{
Frame f=new Frame("输入正整数");
Label l=new Label("请输入整数");
Button b=new Button("确定");
TextField t=new TextField(20);
TextField t2=new TextField(20);
Label l1=new Label("结果为");
DS(){
f.setLayout(new GridLayout(2,3));
f.add(l);
f.add(t);
f.add(b);
f.add(l1);
f.add(t2);
f.setSize(200,300);
f.setVisible(true);
t2.setEditable(false);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}
});
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
int k = Integer.valueOf(t.getText()).intValue();
int sum=0;
String s="";
for (int i=1;i<=k;i++)
{
sum=sum+i;
}
s=s+sum;
t2.setText(s);
}
public static void main(String args[]){
new DS();
}
}
希望采纳。