文档库 最新最全的文档下载
当前位置:文档库 › JAVA上机考试常出现的十道编程题

JAVA上机考试常出现的十道编程题

JAVA上机考试常出现的十道编程题
JAVA上机考试常出现的十道编程题

常用十题

1、九九乘法表:

public class chengfa {

public static void main(String[] args) {

int i,j;

System.out.println("9*9乘法表如下:");

for(i=1;i<=9;i++)

{

for(j=1;j<=i;j++)

{

System.out.print(+i+"*"+j+"="+i*j);

System.out.print(" " );

}

System.out.println( );

}

}

}

2、成绩判断

import java.util.Scanner;

public class chengjipanduan {

public static void main(String[] args) {

int grade;

System.out.println("输入你的成绩:");

Scanner s=new Scanner(System.in);

grade=s.nextInt();

switch(grade/10)

{

case 10:

case 9:

System.out.println("你的成绩为优");break;

case 8:

System.out.println("你的成绩为良");break;

case 7:

System.out.println("你的成绩为中");break;

case 6:

System.out.println("你的成绩为及格");break;

case 5:

case 4:

case 3:

case 2:

case 1:

case 0:

System.out.println("你的成绩为差");break;

}

}

}

3、矩形类

public class Rectangle {

double width=1.0;

double height=1.0;

public Rectangle(double width,double height)

{

this.width=width;

this.height=height;

}

public void setwidth(double width)

{

this.width=width;

}

public double getwidth(double widtht)

{

return width;

}

public void setheight(double height)

{

this.height=height;

}

public double getheight(double height)

{

return height;

}

public double getArea()

{

return width*height;

}

public double getperimeter()

{

return 2*(width+height);

}

}

import tixing06.Rectangle;

public class TestRectangle {

public static void main(String[] args)

{

Rectangle r=new Rectangle(4,40);

System.out.println("矩形面积为"+r.getArea()+"周长为"+r.getperimeter());

}

}

4、两数对换

package tixing;

import java.util.Scanner;

public class TiXing02 {

public static void main(String[] args) {

int a,b;

System.out.println("输入两个数a和b:");

Scanner s=new Scanner(System.in);

a=s.nextInt();

Scanner k=new Scanner(System.in);

b=k.nextInt();

System.out.println("两个数为:a="+a+" b="+b );

exchange(a,b);

}

public static void exchange(int a,int b)

{

int t;

t=a;a=b;b=t;

System.out.println("交换后两个数为:a="+a +" b="+b );

}

}

5、数组

import java.util.Scanner;

import java.util.Random;

public class shuzu {

public static void main(String[] args) {

int n,i,j,k;

double ave=0;

System.out.println("输入数组长度:");

Scanner s=new Scanner(System.in);

n=s.nextInt();

int a[]=new int[n];

System.out.println("输入的原数组为:");

for(i=0;i

{

a[i]=(int)(Math.random()*10+1);

System.out.printf("%4d",a[i]);

}

for(i=0;i

for(j=i+1;j

{

if(a[j]>a[i])

{

k=a[i];a[i]=a[j];a[j]=k;

}

}

System.out.println( );

System.out.println("输入的数组排序为:");

for(i=0;i

{

System.out.printf("%4d",a[i]);

}

int max,min;

max=a[0];min=a[0];

for(i=0;i

{

if(a[i]>max)

{

max=a[i];

}

if(a[i]

{

min=a[i];

}

ave+=a[i];

}

System.out.println( );

System.out.println("数组中最大数为:"+max);

System.out.println("数组中最小数为:"+min);

System.out.println("数组中平均数为:"+ave/n);

}

}

6、填充圆

package tianchongyuan;

import java.applet.*;

import java.awt.*;

public class TianChongY uan extends Applet{

public void paint(Graphics g)

{

g.drawOval(0,0,100,100);

g.drawRoundRect(110,10,90,60,50,30);

g.setColor(Color.blue);

g.fillArc(0,0,100,100,0,360);

g.setColor(Color.red);

g.fillArc(110,10,90,60,0,360);

}

7、星形三角形

package sanjiaoxing;

import java.util.Scanner;

public class SanJiaoXing {

public static void main(String[] args) {

int i,j,n;

System.out.println("输入层数n:");

Scanner s=new Scanner(System.in);

n=s.nextInt();

for(i=1;i<=n;i++)

{

for(j=0;j

{

System.out.printf(" ");

}

if(i%2!=0)

{

for(j=0;j<2*i-1;j++)

{

System.out.printf("*");

}

System.out.println("");

}

else

{

System.out.printf("*");

for(j=0;j<2*i-3;j++)

{

System.out.printf(" ");

}

System.out.printf("*");

System.out.println("");

}

}

}

8、一元二次

import java.util.Scanner;

public class yiyuanerci {

public static void main(String[] args) {

double a,b,c,m;

double x1,x2;

System.out.println("输入一元二次方程前的三个系数a,b,c:");

Scanner s=new Scanner(System.in);

a=s.nextDouble();

Scanner sc=new Scanner(System.in);

b=sc.nextDouble();

Scanner sr=new Scanner(System.in);

c=sr.nextDouble();

m=b*b-4*a*c;

if(m>0)

{

System.out.println("两根为x1="+(-b+Math.sqrt(m))/(2*a)+"

x2="+(-b-Math.sqrt(m))/(2*a));

}

else

if(m==0)

{

System.out.println("方程有同根为:x1=x2="+(-b/(2*a)));

}

if(m<0)

{

System.out.println("方程无根");

}

}

}

9、平方根

import java.util.Scanner;

public class pingfanggeng {

public static void main(String[] args) {

int a,b,c,m;

double x1,x2;

Scanner sc=new Scanner(System.in);

System.out.println("请输入一元二次方程的二次项系数:");

a=sc.nextInt();

Scanner sr=new Scanner(System.in);

System.out.println("请输入一元二次方程的一次项系数:");

b=sr.nextInt();

Scanner st=new Scanner(System.in);

System.out.println("请输入一元二次方程的常数项系数:");

c=st.nextInt();

m=b*b-4*a*c;

x1=(-b+Math.sqrt(m))/(2*a);

x2=(-b-Math.sqrt(m))/(2*a);

System.out.println("一元二次方程的根为:"+x1+" "+x2);

}

}

10、简单计算机窗口(此题为JFrame窗体)

import javax.swing.JOptionPane;

public class NewJFrame extends javax.swing.JFrame {

public NewJFrame() {

initComponents();

}

private void initComponents() {

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

jTextField1 = new javax.swing.JTextField();

jTextField2 = new javax.swing.JTextField();

jButton1 = new javax.swing.JButton();

jButton2 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("第一个数");

jLabel2.setText("第二个数");

jTextField1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jTextField1ActionPerformed(evt);

}

});

jTextField2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jTextField2ActionPerformed(evt);

}

});

jButton1.setText("计算");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

}

});

jButton2.setText("取消");

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton2ActionPerformed(evt);

}

});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(49, 49, 49)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm ent.LEADING)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayou t.Alignment.LEADING)

.addComponent(jLabel2)

.addComponent(jLabel1))

.addGap(82, 82, 82)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayou t.Alignment.LEADING, false)

.addComponent(jTextField1,

javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jTextField2,

javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))

.addGroup(layout.createSequentialGroup()

.addComponent(jButton1)

.addGap(125, 125, 125)

.addComponent(jButton2)))

.addContainerGap(112, Short.MAX_V ALUE))

);

layout.setV erticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(31, 31, 31)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm ent.BASELINE)

.addComponent(jLabel1)

.addComponent(jTextField1,

javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(35, 35, 35)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm ent.BASELINE)

.addComponent(jLabel2)

.addComponent(jTextField2,

javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(40, 40, 40)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm ent.BASELINE)

.addComponent(jButton1)

.addComponent(jButton2))

.addContainerGap(129, Short.MAX_V ALUE))

);

pack();

}

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {

}

private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {

}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

double qiuhe;

qiuhe = Double.parseDouble(jTextField1.getText()) + Double.parseDouble(jTextField2.getText());

JOptionPane.showMessageDialog(null,qiuhe);

}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

dispose();

System.exit(0);

// TODO add your handling code here:

}

public static void main(String args[]) {

/* Set the Nimbus look and feel */

//

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see https://www.wendangku.net/doc/5a18020788.html,/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

}

}

} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level. SEVERE, null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level. SEVERE, null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level. SEVERE, null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level. SEVERE, null, ex);

}

//

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new NewJFrame().setV isible(true);

}

});

}

// V ariables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JButton jButton2;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JTextField jTextField1;

private javax.swing.JTextField jTextField2;

// End of variables declaration

}

相关文档