导航:首页 > 编程语言 > java考试编程题

java考试编程题

发布时间:2022-10-20 04:48:53

java编程题目,求求大佬救救我

这个题考察的是面向对象三大特性之一的继承。

子类继承父类。

项目结构如何所示:

我是冯修远,如果我的答案对您有帮助的话,请采纳以帮助更多的人,如果还有其它的问题,也请关注我,私信我,谢谢!

Ⅱ 5道简单的JAVA编程题(高分悬赏)

很详细的帮你写下,呵呵,所以要给分哦!
1、
(1)源程序如下:
public class One {

public static void main(String[] args) {
String name = "张三";
int age = 23;
char sex = '男';
String myclass = "某某专业2班";
System.out.println("姓名:" + name);
System.out.println("姓名:" + age);
System.out.println("姓名:" + sex);
System.out.println("姓名:" + myclass);

}

}

(2)

编写完程序的后缀名是.java,如本题,文件名就是One.java。
开始\运行\cmd,进入“命令提示符窗口”,然后用javac编译器编译.java文件,语句:javac One.java。

(3)
编译成功后,生成的文件名后缀是.class,叫做字节码文件。再用java解释器来运行改程序,语句:java One

2、编写程序,输出1到100间的所有偶数
(1)for语句
public class Two1 {

public static void main(String[] args) {
for(int i=2;i<=100;i+=2)
System.out.println(i);

}
}

(2)while语句
public class Two2 {

public static void main(String[] args) {
int i = 2;
while (i <= 100) {
System.out.println(i);
i += 2;
}
}
}

(3)do…while语句
public class Two3 {

public static void main(String[] args) {
int i = 2;
do {
System.out.println(i);
i += 2;
}while(i<=100);
}
}

3、编写程序,从10个数当中找出最大值。
(1)for循环
import java.util.*;

public class Three1 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
for (int i = 0; i < 10; i++) {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max < number)
max = number;
}
System.out.println("最大值:" + max);
}
}

(2)while语句
import java.util.*;

public class Three2 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
while (i < 10) {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max < number)
max = number;
i++;
}
System.out.println("最大值:" + max);
}
}

(3)do…while语句
import java.util.*;

public class Three3 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
do {
System.out.print("输入第" + (i + 1) + "个数:");
number = input.nextInt();
if (max < number)
max = number;
i++;
}while(i<10);
System.out.println("最大值:" + max);
}
}

4、编写程序,计算从1到100之间的奇数之和。
(1)for循环

public class Four1 {

public static void main(String[] args) {
int sum=0;
for(int i = 1;i<=100;i+=2){
sum+=i;
}
System.out.println("1~100间奇数和:" + sum);
}
}

(2)while语句
public class Four2 {

public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i <= 100) {
sum += i;
i += 2;
}
System.out.println("1~100间奇数和:" + sum);
}
}

(3)do…while语句
public class Four3 {

public static void main(String[] args) {
int sum = 0;
int i = 1;
do {
sum += i;
i += 2;
} while (i <= 100);
System.out.println("1~100间奇数和:" + sum);
}
}

5、
(1)什么是类的继承?什么是父类?什么是子类?举例说明。
继承:是面向对象软件技术当中的一个概念。如果一个类A继承自另一个类B,就把这个A称为"B的子类",而把B称为"A的父类"。继承可以使得子类具有父类的各种属性和方法,而不需要再次编写相同的代码。在令子类继承父类的同时,可以重新定义某些属性,并重写某些方法,即覆盖父类的原有属性和方法,使其获得与父类不同的功能。另外,为子类追加新的属性和方法也是常见的做法。继承需要关键字extends。举例:
class A{}
class B extends A{}
//成员我就不写了,本例中,A是父类,B是子类。

(2)编写一个继承的程序。
class Person {
public String name;
public int age;
public char sex;

public Person(String n, int a, char s) {
name = n;
age = a;
sex = s;
}

public void output1() {
System.out.println("姓名:" + name + "\n年龄:" + age + "\n性别:" + sex);
}
}

class StudentPerson extends Person {
String school, department, subject, myclass;

public StudentPerson(String sc, String d, String su, String m, String n,
int a, char s) {
super(n, a, s);
school = sc;
department = d;
subject = su;
myclass = m;
}

public void output2() {
super.output1();
System.out.println("学校:" + school + "\n系别:" + department + "\n专业:"
+ subject + "\n班级:" + myclass);
}
}

public class Five2 {

public static void main(String[] args) {
StudentPerson StudentPersonDemo = new StudentPerson("某某大学", "某某系别",
" 某专业", "某某班级", " 张三", 23, '男');
StudentPersonDemo.output2();
}
}

Ⅲ Java编程题,求解

// 上源码

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test {

/**
* 数字键 0-9键 *键 #键
*/
private static String[][] digits = new String[][]{
// 0
{},
// 1
{},
// 2
{"A", "B", "C"},
// 3
{"D", "E", "F"},
// 4
{"G", "H", "I"},
// 5
{"J", "K", "L"},
// 6
{"M", "N", "O"},
// 7
{"P", "Q", "R", "S"},
// 8
{"T", "U", "V"},
// 9
{"W", "X", "Y", "Z"},
// *
{},
// #
{},
};

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String inputs;
String[] arr;
System.out.print("请输入按键数字(多个数字空格隔开):");

while (null != (inputs = scanner.nextLine())) {
if ("exit".equalsIgnoreCase(inputs) || "quit".equalsIgnoreCase(inputs)) {
System.out.println("退出程序");
System.exit(1);
}
// 检查args输入参数是否合法
if (checkInput(inputs.split(" "))) {
arr = inputs.trim().split(" ");
// 查找输入键的所有字母组合
List<String[]> inputCharList = new ArrayList<>();
for (String digit : arr) {
// *#01没有字母
if ("*#01".indexOf(digit) >= 0) {
continue;
}
inputCharList.add(digits[Integer.parseInt(digit)]);
}

if (!inputCharList.isEmpty()) {
combineChars("", inputCharList);
System.out.println();
} else {
System.out.println("输入的数字没有字母组合。");
}
} else {
System.out.println("按键输入不正确,请输入0-9 * #键。");
}

System.out.print("请输入按键数字(多个数字空格隔开):");
}
}

/**
* 递归查找所有字母组合
* @param headerChar
* @param inputCharList
*/
private static void combineChars(String headerChar, List<String[]> inputCharList) {
if (inputCharList.size() == 1) {
for (String ch : inputCharList.get(0)) {
System.out.print(headerChar + ch + " ");
}
} else {
for (String ch : inputCharList.get(0)) {
combineChars(headerChar + ch, inputCharList.subList(1, inputCharList.size()));
}
}
}

/**
* 校验输入是否合法
*
* @param args
* @return
*/
static boolean checkInput(String[] args) {
String validInputs = "0123456789*#";
boolean isValid = true;

for (String arg : args) {
if (arg.length() != 1 || validInputs.indexOf(arg) < 0) {
isValid = false;
break;
}
}

return isValid;
}

}


运行效果

题目里只说了输入两个数字的情况,输入* # 1 0这里我直接视为无效输入了(忽略掉了),对于输入超过两个数字以上的情况也能按要求输出。(考虑到输入的数字个数不确定因素,这里用到了递归,如果确定只有1或2个数字输入,代码会简介很多,也不需要递归)

Ⅳ java编程题 本人新手,求详解。

先看下最终的结果吧,是不是你想要的?

其中,Student是父类,PostGraate是子类,继承自父类Student,Main是主类,用于创建对象以及把这些对象的功能调用起来。

---------------------------Student代码如下:------------------------------

/**

* 学生类

* @author 逍遥

*

*/

public class Student {

//学号

private int sId;

//姓名

private String sName;

//数学成绩

private double mathScore;

//计算机成绩

private double computerScore;

/**

* 获取学号

* @return

*/

public int getsId() {

return sId;

}

/**

* 设置学号

* @param sId

*/

public void setsId(int sId) {

this.sId = sId;

}

/**

* 获取姓名

* @return

*/

public String getsName() {

return sName;

}

/**

* 设置姓名

* @param sName

*/

public void setsName(String sName) {

this.sName = sName;

}

/**

* 获取数学成绩

* @return

*/

public double getMathScore() {

return mathScore;

}

/**

* 设置数学成绩

* @param mathScore

*/

public void setMathScore(double mathScore) {

this.mathScore = mathScore;

}

/**

* 获取计算机成绩

* @return

*/

public double getComputerScore() {

return computerScore;

}

/**

* 设置计算机成绩

* @param computerScore

*/

public void setComputerScore(double computerScore) {

this.computerScore = computerScore;

}

/**

* 输出成员变量(4个成员变量)的信息。

*/

public void print(){

System.out.println("学号:"+sId);

System.out.println("姓名:"+sName);

System.out.println("计算机成绩:"+mathScore);

System.out.println("数学成绩:"+computerScore);

}

}

---------------------------Student代码结束------------------------------

---------------------------PostGraate代码如下:------------------------------

/**

* 研究生类

* @author 逍遥

*

*/

public class PostGraate extends Student{

//导师姓名

private String tName;

//研究方向

private String ResearchDirection;

/**

* 获取导师姓名

* @return

*/

public String gettName() {

return tName;

}

/**

* 设置导师姓名

* @param tName

*/

public void settName(String tName) {

this.tName = tName;

}

/**

* 获取研究方向

* @return

*/

public String getResearchDirection() {

return ResearchDirection;

}

/**

* 设置研究方向

* @param researchDirection

*/

public void setResearchDirection(String researchDirection) {

ResearchDirection = researchDirection;

}

/**

* 研究生类重写父类的void print()方法,功能是输出成员变量(6个成员变量)的信息

*/

@Override

public void print() {

// TODO Auto-generated method stub

super.print();

System.out.println("导师姓名:"+tName);

System.out.println("研究方向:"+ResearchDirection);

}

}

---------------------------PostGraate代码结束------------------------------

---------------------------Main代码如下:------------------------------

import java.util.Scanner;


/**

* 主类

* @author 逍遥

*

*/

public class Main {


/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

//用于获取从键盘上输入的信息

Scanner input=new Scanner(System.in);

//创建一个Student类的对象

Student student=new Student();

//从键盘上输入其属性信息

System.out.print("请输入学生的学号:");

student.setsId(input.nextInt());

System.out.print("请输入学生的姓名:");

student.setsName(input.next());

System.out.print("请输入学生的数学成绩:");

student.setMathScore(input.nextDouble());

System.out.print("请输入学生的计算机成绩:");

student.setComputerScore(input.nextDouble());

//并且通过其print方法输出这些信息;

student.print();

//创建一个PostGraate类的对象

PostGraate postGraate=new PostGraate();

//从键盘上输入其属性信息

System.out.print("请输入研究生的学号:");

postGraate.setsId(input.nextInt());

System.out.print("请输入研究生的姓名:");

postGraate.setsName(input.next());

System.out.print("请输入研究生的数学成绩:");

postGraate.setMathScore(input.nextDouble());

System.out.print("请输入研究生的计算机成绩:");

postGraate.setComputerScore(input.nextDouble());

System.out.print("请输入研究生的导师姓名:");

postGraate.settName(input.next());

System.out.print("请输入研究生的研究方向:");

postGraate.setResearchDirection(input.next());

//并且通过其print方法输出这些信息。

postGraate.print();

}


}

---------------------------Main代码结束------------------------------

=================知识点的简单总结=================

本题考察的知识点是面向对象的三大特性之一:继承。

Student为父类,包含了学号、姓名、数学成绩和计算机成绩4个属性,以及一个print()方法。

PostGraate 继承父类的时候,继承了父类中的所有方法,因为方法我都是用的public,而属性继承不了,因为我在父类中用了封装,所有属性都用private修饰了,想访问属性的话,必须通过get、set方法,这里,我重写了父类中的print方法,通过super.print();调用了父类中的print()方法。

最后就是Main类,提供了main方法作为入口函数,用于按要求声明这些对象以及去调用对象中的方法。

Ⅳ java编程题

1.package test;

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class Test {
public static void main(String[] args) {
String str = "afdjasdg&&&&&$$jfsjdfjdjjdjdjdjdjdj";
int max=0;
Object chars=null;
Map tree = new TreeMap();

for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if ((ch >= 1 && ch <= 255) ) {
if (!tree.containsKey(ch)) {
tree.put(ch, new Integer(1));
} else {
Integer in = (Integer) tree.get(ch) + 1;
tree.put(ch, in);
}
}
}
Iterator tit = tree.keySet().iterator();
while (tit.hasNext()) {
Object temp = tit.next();

if(max<=Integer.parseInt(tree.get(temp)+""))
{
max=Integer.parseInt(tree.get(temp)+"");
chars=temp;
}
}
System.out.print(chars.toString() + "出现" + max + "次");
}
}
只要用assic码做范围就可以了.任何字符都可以过滤.

2.方法很多,hashmap或是arraylist,数组都可以的.就是对应关系而已.
package test;

public class ListTest {

static String[] to_19 = { "zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
"eighteen", "nineteen" };
static String[] tens = { "twenty", "thirty", "forty", "fifty", "sixty",
"seventy", "eighty", "ninety" };
static String[] denom = { "", "thousand ", "million", "billion",
"trillion", "quadrillion", "quintillion", "sextillion",
"septillion", "octillion", "nonillion", "decillion", "undecillion",
"odecillion", "tredecillion", "quattuordecillion",
"sexdecillion", "septendecillion", "octodecillion",
"novemdecillion", "vigintillion" };

public static void main(String[] argv) throws Exception {
long tstValue = 12345;
ListTest itoe = new ListTest();
System.out.println(itoe.english_number(tstValue));
}
private String convert_nn(int val) {
if (val < 20) return to_19[val];
int flag = val / 10 - 2; if (val % 10 != 0)
return tens[flag] + "-" + to_19[val % 10];
else return tens[flag];
}
private String convert_nnn(int val) {
String word = "";
int rem = val / 100;
int mod = val % 100;
if (rem > 0) {word = to_19[rem] + " hundred ";}
if (mod > 0) {word = word + convert_nn(mod);}
return word;
}
public String english_number(long val) {
if (val < 100) {System.out.println((int) val);return convert_nn((int) val);}
if (val < 1000) {return convert_nnn((int) val); }

for (int v = 0; v < denom.length; v++) {
int didx = v - 1;
long dval = new Double(Math.pow(1000, v)).longValue();
if (dval > val) {
long mod = new Double(Math.pow(1000, didx)).longValue();
int l = (int) (val / mod);
long r = (long) (val - (l * mod));
String ret = convert_nnn(l) + " " + denom[didx];
if (r > 0) {ret = ret + ", " + english_number(r);}
return ret;
}
}
return null;
}
}

Ⅵ Java的编程题目,在线等,急急急

先做两个比较简单的先用:
import java.util.Arrays;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Function {
/**
* 设计一个方法,完成字符串的解析。方法定义为:public void myParseString(String inputStr);
* 对于给定的字符串,取得字符串内的各个整数(不考虑小数,),然后将取得的数排序,按从小到大依次打印出来。
* @param args
*/
public static void main(String[] args) {
String s = "aa789bB22cc345dd;5.a";
new Function().myParseString(s);
}
public void myParseString(String inputStr){
String mathregix="\\d+";//数字
Vector vector=new Vector();
Pattern fun=Pattern.compile(mathregix);
Matcher match = fun.matcher(inputStr);
while (match.find()) {
vector.add(match.group(0));
}
Object[] obj=vector.toArray();
int[] result=new int[obj.length];

for (int i = 0; i < obj.length; i++) {
result[i]=Integer.parseInt((String) obj[i]);
}
Arrays.sort(result);
for (int i = 0; i < result.length; i++) {
System.out.println(result[i]);
}
}
}

import java.util.Date;
/**
* 有一个学生类(Student),其有四个私有属性,分别为:
* 学号no,字符串型;
* 姓名name,字符串型;
* 年龄age,整型;
* 生日birthday,日期型;
* 请:
* 1) 按上面描述设计类;
* 2) 定义对每个属性进行取值,赋值的方法;
* 3) 要求学号不能为空,则学号的长度不能少于5位字符,否则抛异常;
* 4) 年龄范围必须在[1,500]之间,否则抛出异常;
*/
public class Student {
private String no;
private String name;
private int age;
private Date birthday;
public int getAge() {
return age;
}
public void setAge(int age) throws Exception {
if(age<1||age<500)throw new Exception("年龄不合法。");
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNo() {
return no;
}
public void setNo(String no) throws Exception {
if(no==null)throw new Exception("学号不能为空!");
if(no.length()<5)throw new Exception("学号不能少于五位!");
this.no = no;
}
}

二、三题
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

/**
* 2. 设计一个GUI界面,要求打界面如下:
*点击文件->打开,打开文件选择器,选择打开给定的java.txt文件,将文件内所有a-z,A-Z之间的字符显示在界面的文本区域内。
* 3. 设置一个GUI界面,界面如下:
* 点击文件->保存,打开文件选择窗体,选择一个保存路径,将本界面文本区域内输入的内容进行过滤,将所有非a-z,A-Z之间的字符保存到C盘下的java.txt文件内。
* Java.txt文件内容如下:
* 我们在2009年第2学期学习Java Programming Design,于2009-6-16考试。
*
*
*
*/
public class FileEditer extends javax.swing.JFrame implements ActionListener {
private JMenuBar menubar;
private JMenu file;
private JMenuItem open;
private JTextArea area;
private JMenuItem save;
private JFileChooser jfc;

/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
FileEditer inst = new FileEditer();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}

public FileEditer() {
super();
initGUI();
}

private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
area = new JTextArea();
getContentPane().add(area, BorderLayout.CENTER);
area.setText("文本区");
}
{
menubar = new JMenuBar();
setJMenuBar(menubar);
{
file = new JMenu();
menubar.add(file);
file.setText("文件");
file.setPreferredSize(new java.awt.Dimension(66, 21));
{
open = new JMenuItem();
file.add(open);
open.setText("打开");
open.setBorderPainted(false);
open.setActionCommand("open");
open.addActionListener(this);
}
{
save = new JMenuItem();
file.add(save);
save.setActionCommand("save");
save.setText("保存");
save.addActionListener(this);
}
}
}
{
jfc=new JFileChooser();
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}

public void actionPerformed(ActionEvent e) {
if("open".equals(e.getActionCommand())){
int result=jfc.showOpenDialog(this);
if(result==jfc.APPROVE_OPTION){
File file=jfc.getSelectedFile();
try {
byte[] b=new byte[1024];
FileInputStream fis=new FileInputStream(file);
fis.read(b);
fis.close();
String string=new String(b,"GBK");
string=string.replaceAll("[^a-zA-Z]*", "");
area.setText(string.trim());
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException es) {
// TODO Auto-generated catch block
es.printStackTrace();
}

}
}else if("save".equals(e.getActionCommand())){
int result=jfc.showSaveDialog(this);
if(result!=jfc.APPROVE_OPTION)return;
File file=jfc.getSelectedFile();
try {
if(!file.exists()){
file.createNewFile();
}
String string = area.getText();
string=string.replaceAll("[a-zA-Z]*", "");
byte[] b=string.getBytes();
FileOutputStream fis=new FileOutputStream(file);
fis.write(b);
fis.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException es) {
// TODO Auto-generated catch block
es.printStackTrace();
}

}
}

}

Ⅶ 求一个java编程题的答案

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
class Student {

private String id;

private String name;

private boolean isMale;

private Date birth;
public Student() {
super();
}
public Student(String id, String name, boolean isMale, Date birth) {
super();
this.id = id;
this.name = name;
this.isMale = isMale;
this.birth = birth;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isMale() {
return isMale;
}
public void setMale(boolean isMale) {
this.isMale = isMale;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", isMale=" + isMale + ", birth=" + birth + "]";
}
}
public class StudentTest {

public static void main(String[] args) throws ParseException {

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");

Student stu = new Student("00001", "Tom", true, dateFormat.parse("2000-01-01"));
System.out.println(stu);
}
}

Ⅷ java简单编程题,有追加分

第一题,x和n从命令行作为参数输入:
public class Test1{
public static void main(String[] args){
int argLen = args.length;
//判断是否至少传入了两个参数
if (argLen < 2){
System.out.println("请输入两个整型参数");
return;
}

int x = 0;
int n = 0;

//转换传递进来的参数,如果输入的参数不合法,不能转换为int型,则Integer.parseInt方法会抛出NumberFormatException异常
try{
x = Integer.parseInt(args[0]);
n = Integer.parseInt(args[1]);
}
catch(NumberFormatException e)
{
System.out.println("输入的参数不是整数");
System.exit(1);
}

//判断x和n的值是否是正数
if (x<=0 || n<=0)
{
System.out.println("不能输入负值或0,请输入两个正整数");
System.exit(1);
}

//打印转换后的x和n
System.out.println("你输入的x和n分别为: " + x + ", " + n);

/*
y=1+x/1+x*x*x/3+......+x^n/n
根据公式计算结果。由于公式中y增长的很快,所以我们定义一个double型的变量存储结果的值。但仍然很有可能溢出。必要的话可以使用math包中的类来进行任意长度和精度的处理,但这里就不麻烦了。
*/
double y = 1.0;
for (int i=1; i<=n; i+=2)
{
y += Math.pow(x, i)/(double)i;
}

//打印结果
System.out.println("根据公式y=1+x/1+x*x*x/3+......+x^n/n所计算出的结果为: " + y);

} // main()

} /* Test1 */

第二题,需要的test11.html文件内容如下:
<html>
<head>
<title>Test11 demo</title>
</head>

<body>
<applet width="300" height="400" code="Test11.class"></applet>
</body>
</html>

然后使用appletviewer test11.html浏览小应用程序(在浏览器中可能不能正常运行)。

java代码如下:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Label;

public class Test11 extends Applet{

//定义文字所在位置与顶部的距离
private int posY = 200;

private Label textsLabel = new Label("我猜你将看到这句话一直在滚动");

public void init()
{
textsLabel.setBounds(50, 200, 200, 30);
this.add(textsLabel);

//启动新线程
SecThread st = new SecThread();
st.start();
} // init()

public void paint(Graphics g){
super.paint(g);
} //paint()

//定义一个内部类,以启动一个新的线程
private class SecThread extends Thread{
public void run()
{
while(true){
//让当前线程休眠50毫秒,注意sleep方法会抛出InterruptedException异常
try{
Thread.sleep(50);
}
catch(InterruptedException e){
System.out.println("执行过程中出错");
System.exit(1);
}

//设置文字的新位置
posY -= 5;
//判断是否小于0(即已经到达顶部),如果小于0则重置为400
posY = (posY<=0?400:posY);
textsLabel.setBounds(50, posY, 200, 30);
Test11.this.repaint();
}
}
}

} /* Test2 */

3, 4两题实在很简单,略过了。

找到你的帖子了!
将3,和4也写一下:

3.运行方法看2:

import java.applet.Applet;
import java.awt.Graphics;

public class Test111 extends Applet
{
public void paint(Graphics g)
{
for (int i=1; i<=10; i++) //画横线
{
g.drawLine(20, i*20, 200, i*20);
}

for (int j=1; j<=10; j++) //画竖线
{
g.drawLine(j*20, 20, j*20, 200);
}
}
}

4. 代码如下:(你说已经写好的程序怎么改成applet。记住一点,applet在运行时自动调用init、start和paint方法,而通常的应用程序调用main方法。只要将main方法中的内容妥善地移到这三个方法中就可以了。但修改的时候要注意,不要引入错误。)

//任意输入三个数,可以有小数,然后比较它们的大小
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;

public class Test1111 extends Applet
{

public void paint(Graphics g)
{
this.setLayout(null);
Button btn = new Button("开始输入");
btn.setBounds(100, 130, 100, 30);
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sort();
}
});

this.add(btn);
}

private void sort()
{
//3个元素的字符串数组,存放输入的数
String[] numberStrs = new String[3];

for (int i=0; i<numberStrs.length; i++)
{
//如果输入时按了取消按钮,则继续提示输入
while(numberStrs[i] == null)
{
numberStrs[i] = JOptionPane.showInputDialog("请输入第 " + (i+1) + " 个数");
}
}

//定义3个元素的double型数组,存放转换后的值
double[] numbers = new double[3];
try
{
for (int j=0; j<numbers.length; j++)
{
numbers[j] = Double.parseDouble(numberStrs[j]);
}
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "输入的不是数字!"
, "ERROR", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}

String result = "";

result += "你输入的数字为: ";
for (int k=0; k<numbers.length-1; k++)
{
result += numbers[k] + ", ";
}

result += numbers[numbers.length-1] + "\n";

//简单点,使用冒泡排序
for (int i=1; i<numbers.length; i++)
{
for (int j=0; j<numbers.length-1; j++)
{
if (numbers[j] > numbers[j+1])
{
double temp = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}

result += "排序后的数字为: ";
for (int k=0; k<numbers.length-1; k++)
{
result += numbers[k] + ", ";
}

result += numbers[numbers.length-1];

//输出结果
JOptionPane.showMessageDialog(null, result, "Result", JOptionPane.PLAIN_MESSAGE);
}
}

Ⅸ java考试,求编程题步骤!!!

题目一:

参考代码

importjava.util.Scanner;
publicclassJiaFaDemo{
publicstaticvoidmain(String[]args){
intx=(int)(Math.random()*100);
inty=(int)(Math.random()*100);
System.out.println(x+"+"+y+"="+(x+y));//自动答题58+4=62

//System.out.print(x+"+"+y+"=");//人工输入答题需要导入包importjava.util.Scanner;
//Scannerinput=newScanner(System.in);
//intz=input.nextInt();
//if(z==(x+y)){
//System.out.println("回答正确");
//}else{
//System.out.println("回答错误");
//}
}
}

输出

58+4=62

题目二

publicclassJiShuDemo{
publicstaticvoidmain(String[]args){
intsum=0;
for(inti=1;i<=50;i++){
if(i%2!=0){//不能被2整除的数是奇数
sum+=i;
}
}
System.out.println("1~50间奇数和="+sum);
}
}

输出

1~50间奇数和=625
阅读全文

与java考试编程题相关的资料

热点内容
rf3148编程器 浏览:505
浙江标准网络服务器机柜云主机 浏览:587
设置网络的服务器地址 浏览:600
java图形界面设计 浏览:751
纯前端项目怎么部署到服务器 浏览:538
瓜子脸程序员 浏览:505
如何保证服务器优质 浏览:94
小微信aPP怎么一下找不到了 浏览:299
算法纂要学术价值 浏览:975
程序员你好是什么意思 浏览:801
倩女幽魂老服务器如何玩 浏览:561
电子钟单片机课程设计实验报告 浏览:999
看加密频道 浏览:381
程序员算不算流水线工人 浏览:632
三星电视我的app怎么卸载 浏览:44
简述vi编译器的基本操作 浏览:507
让程序员选小号 浏览:91
加强数字货币国际信息编译能力 浏览:584
购买的app会员怎么退安卓手机 浏览:891
程序员的种类及名称 浏览:295