JAVA上机考试常出现的十道编程题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
常用十题
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; } }