java大学考试常用代码,java大学考试常用代码大全
java考试,求编程题步骤!!!
题目一:
为永定等地区用户提供了全套网页设计制作服务,及永定网站建设行业解决方案。主营业务为网站制作、成都做网站、永定网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
参考代码
import java.util.Scanner;
public class JiaFaDemo {
public static void main(String[] args) {
int x = (int) (Math.random() * 100);
int y = (int) (Math.random() * 100);
System.out.println(x+"+"+y+"="+(x+y));//自动答题 58+4=62
// System.out.print(x+"+"+y+"=");//人工输入答题 需要导入包import java.util.Scanner;
// Scanner input = new Scanner(System.in);
// int z = input.nextInt();
// if(z==(x+y)){
// System.out.println("回答正确");
// }else{
// System.out.println("回答错误");
// }
}
}
输出
58+4=62
题目二
public class JiShuDemo {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i = 50; i++) {
if(i%2!=0){//不能被2整除的数是奇数
sum+=i;
}
}
System.out.println("1~50间奇数和="+sum);
}
}
输出
1~50间奇数和=625
跪求简单计算器的java代码,要入门级的编程~~
//////////////////////////这个很简单,方法也差不多////////////////////
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculator extends JFrame implements ActionListener
{
///////////////菜单
JMenuBar mb;
JMenu mEdit,mFind,mHelp;
JMenuItem mEditCopy,mEditPaste;
JMenuItem mFindStandarded,mFindScience,mFindGroup;
JMenuItem mHelpTopic,mHelpAbout;
JPanel pan;
/////////////按钮
JTextField tf;
JButton butnull;
JButton butBackspace,butCE,butC;
JButton butMC,butMR,butMS,butM;
JButton but1,but2,but3,but4,but5,but6,but7,but8,but9,but0;
JButton butChu,butSqrt,butCheng,butYu,butJian,but1x,butDot,butJia,butEqual,butJJ;
Calculator()
{
mb =new JMenuBar();
mEdit =new JMenu("编辑(E)");
mEdit.setMnemonic('E');
mFind =new JMenu("查看(F)");
mFind.setMnemonic('F');
mHelp =new JMenu("帮助(H)");
mFind.setMnemonic('H');
mEditCopy =new JMenuItem("复制(C)");
mEditCopy.setMnemonic('C');
mEditPaste =new JMenuItem("粘贴(P)");
mEditCopy.setMnemonic('P');
mFindStandarded=new JMenuItem("标准型(T)");
mFindStandarded.setMnemonic('T');
mFindScience=new JMenuItem("科学型(S)");
mFindScience.setMnemonic('S');
mFindGroup =new JMenuItem("数字分组(I)");
mFindGroup.setMnemonic('I');
mHelpTopic =new JMenuItem("帮助主题(H)");
mHelpTopic.setMnemonic('H');
mHelpAbout =new JMenuItem("关于我们(A)");
mHelpAbout.setMnemonic('A');
////////////////////////////添加mb组件beginning
mb.add(mEdit);
mb.add(mFind);
mb.add(mHelp);
mEdit.add(mEditCopy);
mEdit.add(mEditPaste);
mFind.add(mFindStandarded);
mFind.add(mFindScience);
mFind.add(mFindGroup);
mHelp.add(mHelpTopic);
mHelp.add(mHelpAbout);//////////////ending
////////////////////////***创建计算机按钮***////////////////////
Color red =new Color(250,0,0);
Color green =new Color(0,127,0);
butnull =new JButton("?");
butnull.setForeground(red);
butnull.addActionListener(this);
butBackspace =new JButton("Backspace");
butBackspace.setForeground(green);
butCE =new JButton("CE");
butCE.setForeground(green);
butC =new JButton("C");
butC.addActionListener(this);
butC.setForeground(red);
butMC =new JButton("MC");
butMC.setForeground(green);
butMR =new JButton("MR");
butMR.setForeground(green);
butMS =new JButton("MS");
butMS.setForeground(green);
butM =new JButton("M+");
butM.setForeground(green);
butChu =new JButton("/");
butChu.addActionListener(this);
butChu.setForeground(red);
butCheng =new JButton("*");
butCheng.addActionListener(this);
butCheng.setForeground(red);
butJian =new JButton("-");
butJian.addActionListener(this);
butJian.setForeground(red);
butJia =new JButton("+");
butJia.addActionListener(this);
butJia.setForeground(red);
butEqual =new JButton("=");
butEqual.addActionListener(this);
butEqual.setForeground(red);
Color blue =new Color(0,0,250);
butSqrt =new JButton("sqrt");
butSqrt.setForeground(green);
butYu =new JButton("%");
butYu.setForeground(green);
but1x =new JButton("1/x");
but1x.setForeground(green);
butDot =new JButton(".");
butDot.setForeground(blue);
butDot.addActionListener(this);
butJJ =new JButton("+/-");
butJJ.setForeground(blue);
butJJ.addActionListener(this);
but1 =new JButton("1");
but1.addActionListener(this);
but1.setForeground(blue);
but2 =new JButton("2");
but2.addActionListener(this);
but2.setForeground(blue);
but3 =new JButton("3");
but3.addActionListener(this);
but3.setForeground(blue);
but4 =new JButton("4");
but4.addActionListener(this);
but4.setForeground(blue);
but5 =new JButton("5");
but5.addActionListener(this);
but5.setForeground(blue);
but6 =new JButton("6");
but6.addActionListener(this);
but6.setForeground(blue);
but7 =new JButton("7");
but7.addActionListener(this);
but7.setForeground(blue);
but8 =new JButton("8");
but8.addActionListener(this);
but8.setForeground(blue);
but9 =new JButton("9");
but9.addActionListener(this);
but9.setForeground(blue);
but0 =new JButton("0");
but0.addActionListener(this);
but0.setForeground(blue);///////////////////////////////ending
pan =new JPanel(null);//创建最大的panel
tf =new JTextField("0");//输入显示框
tf.setBounds(5,0,337,25);//显示框设置大小位置
tf.setBackground(Color.white);
tf.setHorizontalAlignment(JTextField.RIGHT);//输入框设置位子居右显示
tf.setEditable(false);
JPanel pa=new JPanel(new BorderLayout());//所有按钮的panel
pa.setBounds(5,35,340,240);
JPanel pp=new JPanel(new BorderLayout());//null,MC,MR,MS,M+的panel;pa的西
JPanel aa=new JPanel(new BorderLayout());//其余所有Button的panel;pa的中
JPanel p =new JPanel(new GridLayout(4,1));//MC,MR,MS,M+的Panel,pp的中 北是一个butnull
JPanel a =new JPanel(new GridLayout(1,3));//backspace,ce,c的panel,aa的北
JPanel a2=new JPanel(new GridLayout(4,5));//其余的button的panel,aa的中
pa.add(pp,BorderLayout.WEST);
pa.add(aa,BorderLayout.CENTER);
pp.add(butnull,BorderLayout.NORTH);
pp.add(p,BorderLayout.CENTER);
p.add(butMC);
p.add(butMR);
p.add(butMS);
p.add(butM);
aa.add(a,BorderLayout.NORTH);
aa.add(a2,BorderLayout.CENTER);
a.add(butBackspace);
a.add(butCE);
a.add(butC);
a2.add(but7);
a2.add(but8);
a2.add(but9);
a2.add(butChu);
a2.add(butSqrt);
a2.add(but4);
a2.add(but5);
a2.add(but6);
a2.add(butCheng);
a2.add(butYu);
a2.add(but1);
a2.add(but2);
a2.add(but3);
a2.add(butJian);
a2.add(but1x);
a2.add(but0);
a2.add(butJJ);
a2.add(butDot);
a2.add(butJia);
a2.add(butEqual);
this.add(pan);
pan.add(tf);
pan.add(pa);
this.setTitle("我的计算器");
this.setJMenuBar(mb);
this.setBounds(200,300,353,330);//设置窗口大小
this.setResizable(false);
this.setVisible(true);
}
public static void main(String args[])
{
try
{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}
catch (Exception ex)
{
ex.printStackTrace();
}
new Calculator();
}
////////////////////********以下是监听设置和运算*******///////////////////
boolean b=true;//初始值设置为真未点运算符号
String fuhao="";//保存最后一次运算符号,用于进行上次结果的运算
String lastCommand="";//保存按等号前的最后的运算符号
double lastValue=0.0;//保存按等号钱的最后的输入框里的值
double value=0.0;//原值,当你点完运算符的时候计算之前的值
double fuzhi=0.0;
public void actionPerformed(ActionEvent click)/////////////////////行为监听设置
{
if(click.getSource()==butJJ)
{
fuzhi=Double.parseDouble(tf.getText());
if(tf.getText().equals("0")||tf.getText().equals("0."))
{
boolean b=true;
String fuhao="";
double value=0.0;
tf.setText("0");
}
else if(fuzhi0)
{
tf.setText((-fuzhi)+"");
}
else
{
tf.setText("-"+tf.getText());
}
}
if(click.getSource()==butnull)
{
tf.setText("本计算器版权所有归青软培训--胡迅--2008年10月18日著 ");
}
if(click.getSource()==butC)
{
tf.setText("0");
lastCommand="";
lastValue=0.0;
b=true;
fuhao="";
value=0.0;
}
if(click.getSource()==but1)
{
if(b==false)
{
tf.setText("1");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("1");
}
else
{
tf.setText(tf.getText()+1);
}
}
if(click.getSource()==but2)
{
if(b==false)
{
tf.setText("2");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("2");
}
else
{
tf.setText(tf.getText()+2);
}
}
if(click.getSource()==but3)
{
if(b==false)
{
tf.setText("3");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("3");
}
else
{
tf.setText(tf.getText()+3);
}
}
if(click.getSource()==but4)
{
if(b==false)
{
tf.setText("4");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("4");
}
else
{
tf.setText(tf.getText()+4);
}
}
if(click.getSource()==but5)
{
if(b==false)
{
tf.setText("5");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("5");
}
else
{
tf.setText(tf.getText()+5);
}
}
if(click.getSource()==but6)
{
if(b==false)
{
tf.setText("6");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("6");
}
else
{
tf.setText(tf.getText()+6);
}
}
if(click.getSource()==but7)
{
if(b==false)
{
tf.setText("7");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("7");
}
else
{
tf.setText(tf.getText()+7);
}
}
if(click.getSource()==but8)
{
if(b==false)
{
tf.setText("8");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("8");
}
else
{
tf.setText(tf.getText()+8);
}
}
if(click.getSource()==but9)
{
if(b==false)
{
tf.setText("9");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("9");
}
else
{
tf.setText(tf.getText()+9);
}
}
if(click.getSource()==but0)
{
if(b==false)
{
tf.setText("0");
b=true;
}
else if(tf.getText().equals("0"))
{
tf.setText("0");
}
else
{
tf.setText(tf.getText()+0);
}
}
if(click.getSource()==butDot)
{
/////////code/////////////////////////////////////////////////////////////////////////////////////////////////////////???
int izhi=Integer.parseInt(tf.getText());
double dzhi=Double.parseDouble(tf.getText());
if(tf.getText().equals("0"))
{
tf.setText("0.");
}
else
{
if(tf.getText().equals("0."))
{
tf.setText("0.");
}
else
{
if(dzhi0)
{
izhi=izhi;
dzhi=dzhi;
if(dzhi==izhi)
{
tf.setText(tf.getText()+".");
}
}
else
{
izhi=-izhi;
dzhi=-dzhi;
if(dzhi==izhi)
{
tf.setText(tf.getText()+".");
}
}
}
}
}
if(click.getSource()==butJia)///////////////////////////////////////////////"+"
{
if(tf.getText().equals("0"))
{
}
else if(fuhao=="")
{
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=tfvaluejia;
tf.setText(""+value);
b=false;
fuhao="+";
}
else if(fuhao=="*")
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=value*tfvalueCheng;
tf.setText(""+value);
b=false;
}
else if(fuhao=="/")
{
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=value/tfvalueChu;
tf.setText(""+value);
b=false;
}
else if(fuhao=="-")
{
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=value-tfvaluejian;
tf.setText(""+value);
b=false;
}
else if(fuhao=="=")
{
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=tfvaluejia;
b=false;
}
else
{
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());//2
value=value+tfvaluejia;
tf.setText(""+value);
b=false;
}
lastValue=Double.parseDouble(tf.getText());
fuhao="+";
lastCommand="+";
}
if(click.getSource()==butCheng)////////////////////////////////////////////////////////////"*"
{
if(tf.getText().equals("0"))
{
}
else if(fuhao=="")
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=tfvalueCheng;
tf.setText(""+value);
b=false;
fuhao="*";
}
else if(fuhao=="+")
{
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=value+tfvaluejia;
tf.setText(""+value);
b=false;
}
else if(fuhao=="/")
{
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=value/tfvalueChu;
tf.setText(""+value);
b=false;
}
else if(fuhao=="-")
{
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=value-tfvaluejian;
tf.setText(""+value);
b=false;
}
else if(fuhao=="=")
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=tfvalueCheng;
b=false;
}
else
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=value*tfvalueCheng;
tf.setText(""+value);
b=false;
}
lastValue=Double.parseDouble(tf.getText());
fuhao="*";
lastCommand="*";
}
if(click.getSource()==butChu)//////////////////////////////////////////////"/"
{
if(tf.getText().equals("0"))
{
}
else if(fuhao=="")
{
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=tfvalueChu;
tf.setText(""+value);
b=false;
fuhao="/";
}
else if(fuhao=="+")
{
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=value+tfvaluejia;
tf.setText(""+value);
b=false;
}
else if(fuhao=="*")
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=value*tfvalueCheng;
tf.setText(""+value);
b=false;
}
else if(fuhao=="-")
{
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=value-tfvaluejian;
tf.setText(""+value);
b=false;
}
else if(fuhao=="=")
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=tfvalueCheng;
b=false;
}
else
{
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=value/tfvalueChu;
tf.setText(""+value);
b=false;
}
lastValue=Double.parseDouble(tf.getText());
fuhao="/";
lastCommand="/";
}
if(click.getSource()==butJian)////////////////////////////////////////////////////////////////////////"-"
{
if(tf.getText().equals("0"))
{
}
else if(fuhao=="")
{
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=tfvaluejian;
tf.setText(""+value);
b=false;
fuhao="/";
}
else if(fuhao=="+")
{
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=value+tfvaluejia;
tf.setText(""+value);
b=false;
}
else if(fuhao=="*")
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=value*tfvalueCheng;
tf.setText(""+value);
b=false;
}
else if(fuhao=="/")
{
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=value/tfvalueChu;
tf.setText(""+value);
b=false;
}
else if(fuhao=="=")
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=tfvalueCheng;
b=false;
}
else
{
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=value-tfvaluejian;
tf.setText(""+value);
b=false;
}
lastValue=Double.parseDouble(tf.getText());
fuhao="-";
lastCommand="-";
}
if(click.getSource()==butEqual)//////////////////////////////////////////"="
{
if(tf.getText().equals("0"))
{
}
if(fuhao=="+")
{
double tfvaluejia;
tfvaluejia=Double.parseDouble(tf.getText());
value=value+tfvaluejia;
tf.setText(""+value);
b=false;
}
else if(fuhao=="*")
{
double tfvalueCheng;
tfvalueCheng=Double.parseDouble(tf.getText());
value=value*tfvalueCheng;
tf.setText(""+value);
b=false;
}
else if(fuhao=="/")
{
double tfvalueChu;
tfvalueChu=Double.parseDouble(tf.getText());
value=value/tfvalueChu;
tf.setText(""+value);
b=false;
}
else if(fuhao=="-")
{
double tfvaluejian;
tfvaluejian=Double.parseDouble(tf.getText());
value=value-tfvaluejian;
tf.setText(""+value);
b=false;
}
else if(fuhao=="=")
{
if(lastCommand=="+")
{
value = value +lastValue;
tf.setText(""+value);
b=false;
}
else if(lastCommand=="-")
{
value = value - lastValue;
tf.setText(""+value);
b=false;
}
if(lastCommand=="*")
{
value = value *lastValue;
tf.setText(""+value);
b=false;
}
if(lastCommand=="/")
{
value = value /lastValue;
tf.setText(""+value);
b=false;
}
}
fuhao="=";
}
}
}
大学JAVA选修考试求大神
楼主,程序如下:
题目1:
public class Student {
private String name; //姓名
private int sno; //学号
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
}
=========================华丽的分割线===========================
题目2:
public class Student implements ComparableStudent{
private String name; //姓名
private int sno; //学号
private float score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
public int compareTo(Student o) {
if(this == o)
{
return 0;
}
if(this.score o.score)
{
return 1;
}
else if(this.score o.score)
{
return -1;
}
else
{
return 0;
}
}
}
=========================华丽的分割线===========================
题目3:
import java.util.Calendar;
import java.util.GregorianCalendar;
public class HelloDuke {
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
if(hour 12)
{
System.out.println("上午好");
}
else if(hour 13)
{
System.out.println("中午好");
}
else if(hour 18)
{
System.out.println("下午好");
}else
{
System.out.println("晚上好");
}
}
}
有问题欢迎提问,满意请采纳!
Java实验,代码怎么写?
Shape.java接口代码
public interface Shape {
public static final double PI = 3.14d;
public double area();
}
Circle.java圆类代码
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return PI * this.radius * this.radius;
}
public double perimeter() {
return 2 * PI * this.radius;
}
}
Cylinder.java圆柱体类代码
public class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double height) {
super(radius);
this.height = height;
}
public double area() {
return 2 * super.area() + super.perimeter() * this.height;
}
public double volume() {
return super.area() * this.height;
}
}
X5_3_6.java主类代码
public class X5_3_6 {
public static void main(String[] args) {
Circle cir1 = new Circle(5);
System.out.println("圆的面积为:" + cir1.area());
System.out.println("圆的周长为:" + cir1.perimeter());
Cylinder cy1 = new Cylinder(10, 15);
System.out.println("圆柱体的表面积为:" + cy1.area());
System.out.println("圆柱体的体积为:" + cy1.volume());
}
}
上面是我写的代码,下图是执行结果,麻烦看一下,是否可以。
新闻标题:java大学考试常用代码,java大学考试常用代码大全
文章出自:http://hbruida.cn/article/dscjdij.html