
正文
java语言简单程序代码 java简单编程代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
用JAVA语言编写一个程序,要求如下:
import java.util.Random;
import java.util.Scanner;
public class T {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
int difficulty;//难度
int mode;//运算类型
int answer;//答案
int amount;//挑战题目数量
int score = 0;//得分
System.out.println("请输入难度(1:一位数、2:两位数、3:三位数):");
difficulty = in.nextInt();
System.out.println("请输入运算类型(1:加、2:减、3:乘、4:除):");
mode = in.nextInt();
System.out.println("请输入想要挑战的题目数量:");
amount = in.nextInt();
Random random = new Random();
for (int i = 0; i amount; i++) {
if (difficulty == 1) {
if (mode == 1) {
int x = random.nextInt(10);
int y = random.nextInt(10);
System.out.println("第" + i + "题:");
System.out.print(x + " + " + y + " = ");
answer = in.nextInt();
if (answer == (x + y)) {
System.out.println("答对了\n");
score++;
} else {
System.out.println("答错了,答案是:" + (x + y) + "\n");
}
} else if (mode == 2) {
int x = random.nextInt(10);
int y = random.nextInt(10);
System.out.println("第" + i + "题:");
System.out.print(x + " - " + y + " = ");
answer = in.nextInt();
if (answer == (x - y)) {
System.out.println("答对了\n");
score++;
} else {
System.out.println("答错了,答案是:" + (x - y) + "\n");
}
} else if (mode == 3) {//乘法
} else if (mode == 4) {//除法 考虑小数的问题
} else {
throw new Exception("运算类型输入值不合法");
}
} else if (difficulty == 2) {
if (mode == 1) {
int x = random.nextInt(100);
int y = random.nextInt(100);
System.out.println("第" + i + "题:");
System.out.print(x + " + " + y + " = ");
answer = in.nextInt();
if (answer == (x + y)) {
System.out.println("答对了\n");
score++;
} else {
System.out.println("答错了,答案是:" + (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 = random.nextInt(1000);
int y = random.nextInt(1000);
System.out.println("第" + i + "题:");
System.out.print(x + " + " + y + " = ");
answer = in.nextInt();
if (answer == (x + y)) {
System.out.println("答对了\n");
score++;
} else {
System.out.println("答错了,答案是:" + (x + y) + "\n");
}
} else if (mode == 2) {
} else if (mode == 3) {//乘法
} else if (mode == 4) {//除法 考虑小数的问题
} else {
throw new Exception("运算类型输入值不合法");
}
} else {
throw new Exception("难度输入值不合法");
}
}
System.out.println("挑战结束,您的分数为:" + score);
}
}
我就只举了加法的例子,其他运算的写法都是类似的,你照葫芦画瓢即可
运行结果:
相关问答
Q1: 用JAVA编写一个程序,要求如下:
实现代码如下:
Student类:
public class Student {
private String name;
private String sex;
private int age;
private double chinese;
private double math;
private double english;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getChinese() {
return chinese;
}
public void setChinese(double chinese) {
this.chinese = chinese;
}
public double getMath() {
return math;
}
public void setMath(double math) {
this.math = math;
}
public double getEnglish() {
return english;
}
public void setEnglish(double english) {
this.english = english;
}
}
-----------------------------------------------------------------
StudentTest类:(测试类)
import java.util.Scanner;
public class StudentTest {
public static void main(String[] args) {
Student student = new Student();
Scanner sc = new Scanner(System.in);
System.out.println("请输入姓名:");
student.setName(sc.next());
System.out.println("请输入性别:");
student.setSex(sc.next());
System.out.println("请输入年龄:");
student.setAge(sc.nextInt());
System.out.println("请输入语文成绩、数学成绩、英语成绩:");
student.setChinese(sc.nextDouble());
student.setMath(sc.nextDouble());
student.setEnglish(sc.nextDouble());
Double count = student.getChinese()+ student.getMath()+student.getEnglish();
System.out.println("姓名:"+student.getName()+" 性别:"+student.getSex()+" 年龄:"+student.getAge());
System.out.println("总分:"+count+" 平均分:"+count/3);
}
}
运行结果为:
Q2: 求编写一个超级简单的Java的程序源代码
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login {
public static void main(String args[]) {
LoginFrm frame = new LoginFrm();
}
}
class LoginFrm extends JFrame implements ActionListener{
JLabel nameLabel=new JLabel("用户名:");
JLabel pwdLabel=new JLabel("密码:");
JTextField name=new JTextField(10);
JPasswordField password=new JPasswordField(10);
JButton butnSure=new JButton("确定");
JButton butnCancel=new JButton("取消");
public LoginFrm() {
super("登陆");
setBounds(500, 200, 280, 220);
setVisible(true);
setLayout(null);
nameLabel.setBounds(45, 20, 100, 25);
add(nameLabel);
add(name);
name.setBounds(105, 20, 110, 25);
add(pwdLabel);
pwdLabel.setBounds(45, 60, 100, 25);
add(password);
password.setBounds(105, 60, 110, 25);
add(butnSure);
butnSure.setBounds(45, 100, 80, 25);
add(butnCancel);
butnCancel.setBounds(135, 100, 80, 25);
butnSure.addActionListener(this);
butnCancel.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();//刷新
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() ==butnSure){
System.out.println("用户名:"+name.getText());
System.out.println("密码:"+name.getText());
if("admin".equals(name.getText().trim())"123".equals(password.getText().trim())){
this.dispose();
new MainFrm("用户界面",name.getText().trim(),password.getText().trim());
}else {
JOptionPane.showMessageDialog(this, "用户不存在");
}
}else if(e.getSource()==butnCancel){
System.exit(1);
}
}
class MainFrm extends JFrame{
private JLabel info;
public MainFrm(String s,String name,String password) {
super(s);
setBounds(400, 200, 500, 400);
setLayout(new FlowLayout());
info=new JLabel("登陆成功,用户名:"+name+",密码:"+password);
add(info);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
}
}
Q3: 求一个简单java程序代码,谢谢
public class TestStar {
public static void main(String[] args) {
String star = "*";
for (int i = 0; i 5; i++) {
if (i == 0) {
System.out.print(" " + star);
System.out.println();
}
if (i == 1) {
for (int z = 0; z 4; z++) {
System.out.print(" " + star);
}
System.out.println();
}
if (i == 2) {
System.out.print(" ");
for (int x = 0; x 3; x++) {
System.out.print(" " + star);
}
System.out.println();
}
if (i == 3) {
for (int y = 0; y 2; y++) {
System.out.print(" " + star + " ");
}
}
}
}
}
是好使的 但是我没找到画五角星有什么规律(五角星好象不是正规图形吧?)如果还有什么要求的话 补充问题(如果是用*填充所有的东西 不包括 “ ”的话 我可以重新再给你写一个)
Q4: Java语言简单程序设计
参考代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//jdk1.8 通过测试
public class DemoFrame extends JFrame implements ActionListener{
JTextField jtf1,jtf2,jtf3;
public DemoFrame() {
jtf1 = new JTextField(5);//文本框
JLabel jl = new JLabel("+");
jtf2 = new JTextField(5);
JButton jb1 = new JButton("=");
jb1.addActionListener(this);
jtf3 = new JTextField(5);
jtf3.setEditable(false);//文本框不许编辑
add(jtf1);add(jl);add(jtf2);add(jb1);add(jtf3);
setLayout(new FlowLayout());//流式布局
setTitle("Computer");
setSize(300,100);//大小
setLocationRelativeTo(null);//居中
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {//main方法,启动窗口
new DemoFrame();
}
public void actionPerformed(ActionEvent e) {
try{
double d1 = Double.parseDouble(jtf1.getText());
double d2 = Double.parseDouble(jtf2.getText());
jtf3.setText((d1+d2)+"");
}catch(Exception ex){
jtf3.setText("Error");//如果输入字母等.就在第三个文本框提示
//JOptionPane.showMessageDialog(this, "非法输入!!\n提示:请输入正确的两个数字");
}
}
}
测试
java语言简单程序代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java简单编程代码、java语言简单程序代码的信息别忘了在本站进行查找喔。






