lz 你好
具体代码如下:
import javaawt;import javamath;
import javaxswing;
import javaawtevent;
public class Test extends JFrame{
private JPanel panel;
private JLabel one,equ,result,img,end;
private JTextField input;
private JButton check;
private int first, second;
public Test(){
super("小程序查看器");
one = new JLabel("小程序");
onesetFont(new Font("宋体", FontPLAIN, 15));
makeRandom();
equ = new JLabel(first + "+" + second + "=");
input = new JTextField(10);
check = new JButton("检查");
checkaddActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int r = IntegerparseInt(inputgetText());
if(first+second == r){
resultsetText("you are right!");
}
else{
resultsetText("you are wrong!");
}
}
});
result = new JLabel();
img = new JLabel(new ImageIcon("1jpg"));
panel = new JPanel(new FlowLayout(FlowLayoutCENTER, 5, 5));
paneladd(equ);
paneladd(input);
paneladd(check);
paneladd(result);
paneladd(img);
end = new JLabel("小程序已启动。");
endsetFont(new Font("宋体", FontPLAIN, 15));
endsetOpaque(true);
endsetBackground(ColorWHITE);
getContentPane()add(one, BorderLayoutNORTH);
getContentPane()add(panel, BorderLayoutCENTER);
getContentPane()add(end, BorderLayoutSOUTH);
setSize(230, 230);
setLocationRelativeTo(null);
setDefaultCloseOperation(3);
setVisible(true);
}
public void makeRandom(){
first = (int)(Mathrandom() 100 + 1);
second = (int)(Mathrandom() 100 + 1);
}
public static void main (String[] args) {
new Test();
}
}
运行结果:
希望能帮助你哈
import javautil;
public class Test {
public static void main(String[] args){
Systemoutprintln("您的性别是:\n1男\n2女\n请根据具体情况选择编号:");
Scanner sc=new Scanner(Systemin);
int sex=scnextInt();
switch(sex){
case 1:Systemoutprintln("您选择的性别是男人");
break;
case 2:Systemoutprintln("您选择的性别是女人");
break;
default:Systemoutprintln("数据非法!");
break;
}
}
}
工程自己建,然后你建个Test类把代码复制进去就行了。
import javautilRandom;
import javautilScanner;
public class T {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(Systemin);
int difficulty;//难度
int mode;//运算类型
int answer;//答案
int amount;//挑战题目数量
int score = 0;//得分
Systemoutprintln("请输入难度(1:一位数、2:两位数、3:三位数):");
difficulty = innextInt();
Systemoutprintln("请输入运算类型(1:加、2:减、3:乘、4:除):");
mode = innextInt();
Systemoutprintln("请输入想要挑战的题目数量:");
amount = innextInt();
Random random = new Random();
for (int i = 0; i < amount; i++) {
if (difficulty == 1) {
if (mode == 1) {
int x = randomnextInt(10);
int y = randomnextInt(10);
Systemoutprintln("第" + i + "题:");
Systemoutprint(x + " + " + y + " = ");
answer = innextInt();
if (answer == (x + y)) {
Systemoutprintln("答对了\n");
score++;
} else {
Systemoutprintln("答错了,答案是:" + (x + y) + "\n");
}
} else if (mode == 2) {
int x = randomnextInt(10);
int y = randomnextInt(10);
Systemoutprintln("第" + i + "题:");
Systemoutprint(x + " - " + y + " = ");
answer = innextInt();
if (answer == (x - y)) {
Systemoutprintln("答对了\n");
score++;
} else {
Systemoutprintln("答错了,答案是:" + (x - y) + "\n");
}
} else if (mode == 3) {//乘法
} else if (mode == 4) {//除法 考虑小数的问题
} else {
throw new Exception("运算类型输入值不合法");
}
} else if (difficulty == 2) {
if (mode == 1) {
int x = randomnextInt(100);
int y = randomnextInt(100);
Systemoutprintln("第" + i + "题:");
Systemoutprint(x + " + " + y + " = ");
answer = innextInt();
if (answer == (x + y)) {
Systemoutprintln("答对了\n");
score++;
} else {
Systemoutprintln("答错了,答案是:" + (x + y) + "\n");
}
} else if (mode == 2) {
} else if (mode == 3) {//乘法
} else if (mode == 4) {//除法 考虑小数的问题
} else {
throw new Exception("运算类型输入值不合法");
}
} else if (difficulty == 3) {
if (mode == 1) {
int x = randomnextInt(1000);
int y = randomnextInt(1000);
Systemoutprintln("第" + i + "题:");
Systemoutprint(x + " + " + y + " = ");
answer = innextInt();
if (answer == (x + y)) {
Systemoutprintln("答对了\n");
score++;
} else {
Systemoutprintln("答错了,答案是:" + (x + y) + "\n");
}
} else if (mode == 2) {
} else if (mode == 3) {//乘法
} else if (mode == 4) {//除法 考虑小数的问题
} else {
throw new Exception("运算类型输入值不合法");
}
} else {
throw new Exception("难度输入值不合法");
}
}
Systemoutprintln("挑战结束,您的分数为:" + score);
}
}
我就只举了加法的例子,其他运算的写法都是类似的,你照葫芦画瓢即可
运行结果:
public static void main(String[] args){
String firstString="This is";
String secondString="This is";
if(firstString==null && secondString==null){
Systemoutprintln("①firstString 与 secondString 相等");
}else if(firstString!=null && firstStringequals(secondString)){
Systemoutprintln("①firstString 与 secondString 相等");
}else{
Systemoutprintln("①firstString 与 secondString 不相等");
}
String str = firstString + "his father";
Systemoutprintln("②"+str);
Systemoutprintln("③"+strreplaceAll("i", "z"));
}
以上就是关于java简单小程序编写,就一点点全部的内容,包括:java简单小程序编写,就一点点、求大神帮忙编一个简单的java程序代码、用JAVA语言编写一个程序,要求如下:等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)