java题
⑴ java试题
start方法哈
给你个例子哈
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class AppletAnimation extends Applet implements Runnable {
int frameNumber = -1;
int delay = 100;
Thread animatorThread;
boolean frozen = false;
public void init() {
String str;
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (frozen) {
frozen = false;
start();
} else {
frozen = true;
stop();
}
}
});
}
public void start() {
if (!frozen) {
if (animatorThread == null) {
animatorThread = new Thread(this);
}
animatorThread.start();
}
}
public void stop() {
animatorThread = null;
}
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
long startTime = System.currentTimeMillis();
Thread currentThread = Thread.currentThread();
while (currentThread == animatorThread) {
frameNumber++;
repaint();
try {
startTime += delay;
Thread.sleep(100);
} catch (InterruptedException e) {
break;
}
}
}
public void paint(Graphics g) {
g.drawString("Frame " + frameNumber, 0, 30);
}
}
⑵ Java题!
输出信息重写了toString,输出C语言成绩直接用了变量。可以根据自己需要修改,望采纳
publicclassStudent{
Stringname;
intage;
Stringsex;
intgrade;
int$class;
doublecMark;
publicStudent(Stringname,intage,intgrade,int$class,doublecMark){
this.name=name;
this.age=age;
this.grade=grade;
this.$class=$class;
this.cMark=cMark;
}
publicStringtoString(){
return"姓名:"+name+"性别:"+sex+"年龄:"+age+"年级:"+grade+"班级:"+$class;
}
}
publicclassStudentTest{
publicstaticvoidmain(String[]args){
Studentstu=newStudent("张三",18,3,32,99.5);
System.out.println(stu.toString());
System.out.println(stu.cMark);
}
}
⑶ Java题
大致写了一下,基本功能满足,望采纳
publicclassMingXing{
privateStringname;
privateStringsex;
privateintage;
privateStringdaiBiaoZuo;
publicMingXing(Stringname,Stringsex,intage,StringdaiBiaoZuo){
this.name=name;
this.sex=sex;
this.age=age;
this.daiBiaoZuo=daiBiaoZuo;
}
publicvoidshow(){
System.out.println("明星姓名:"+name);
System.out.println("明星性别:"+sex);
System.out.println("明星年龄:"+age);
System.out.println("明星代表作:"+daiBiaoZuo);
}
//set、get方法
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetName(){
returnname;
}
//TODO
}
⑷ java题
package com.test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 字符串工具类
*
* @author 刘逸晖
*
*/
public class StringUtil {
/**
* 删除字符串数组中重复的成员
*
* @param src
* 原数组
* @return 删除重复成员后的数组
*/
public static String[] trim(String[] src) {
// 我这个方法估计也不是效率最高的,只是想到可以这样写就这样写了。
List<String> srcList = new ArrayList<>(Arrays.asList(src));
// 注意,不能直接用数组转化出来的那个ArrayList对象,因为用数组转化出来的那个ArrayList对象做某些操作的时候会报错。
for (int i = 0; i < srcList.size(); i++) {
String str = srcList.get(i);
// 注意,在这里别用高级for循环,因为在使用高级for循环的时候不能修改集合或数组中的元素。
while (srcList.remove(str)) {
}
srcList.add(str);
}
return srcList.toArray(new String[srcList.size()]);
}
/**
* 字符反转
*
* @param src
* 原文本
* @return 反转后的文本
*/
public static String reverse(String src) {
return new StringBuffer(src).reverse().toString();
}
/**
* 统计字符串中单词的个数。
*
* @param str
* 原文本
* @return 单词个数
*/
public static int split(String str) {
return str.split("[^A-Za-z]+").length;
}
}
package com.test;
import java.util.Arrays;
/**
* 我的测试类
*
* @author 刘逸晖
*
*/
public class MyTest {
public static void main(String[] args) {
String[] srcArray = new String[] { "a", "b", "c", "a", "a", "d", "c",
"b" };
System.out.println("原字符串数组:\r\n" + Arrays.toString(srcArray));
System.out.println("去除重复成员后的数组:\r\n"
+ Arrays.toString(StringUtil.trim(srcArray)));
String srcStr = "hello world";
System.out.println("原文本:\r\n" + srcStr);
System.out.println("反转后的文本:\r\n" + StringUtil.reverse(srcStr));
System.out.println("文本中的单词数:\r\n" + StringUtil.split(srcStr));
}
}
⑸ java题目
1.................程序输入非数字%结束
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Du {
public static void main(String[] args) {
double[] numbers = getNumbersFromInput();
Arrays.sort(numbers);
double max = numbers[numbers.length - 1];
double min = numbers[0];
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
}
private static double[] getNumbersFromInput() {
List<Double> list = new ArrayList<Double>();
String input = "";
final String EXIT = "%";
while(true){
System.out.print("Please input a number:");
Scanner scanner = new Scanner(System.in);
try{
input = scanner.next().trim();
if(input.equals(EXIT)){
break;
}
list.add(Double.parseDouble(input));
}catch(NumberFormatException nbFmtExp){
System.out.println("Only numbers allowed. Please input again!");
continue;
}
}
double[] numbers = new double[list.size()];
for(int i = 0; i < list.size(); i++){
numbers[i] = list.get(i).doubleValue();
}
return numbers;
}
}
-----------------------
Please input a number:35
Please input a number:ab
Only numbers allowed. Please input again!
Please input a number:68
Please input a number:5
Please input a number:96
Please input a number:35.3
Please input a number:2
Please input a number:129
Please input a number:652
Please input a number:%
Max is: 652.0
Min is: 2.0
2.......................
import java.util.Random;
public class Second {
public static void main(String[] args) {
int[][] nums = new int[4][5];
Random rand = new Random();//测试用随机数生成数组
for(int i = 0; i < nums.length; i++){
for(int j = 0; j < nums[i].length; j++){
nums[i][j] = rand.nextInt(500);//方便测试,限制小于500
}
}
MinItem min = new MinItem(nums[0][0], 0, 0);
for(int i = 0; i < nums.length; i++){
for(int j = 0; j < nums[i].length; j++){
System.out.print(nums[i][j] + "\t");//输出数组
if(min.getValue() > nums[i][j]){
min = new MinItem(nums[i][j], i, j);
}
}
System.out.println();
}
System.out.println("Min is " + min.getValue()
+ ". Row: " + (min.getRow() + 1)
+ ", Column : " + (min.getCol() + 1));
}
}
class MinItem {
private int row;
private int col;
private int value;
public MinItem(int value, int row, int col){
this.value = value;
this.row = row;
this.col = col;
}
public int getValue() {
return value;
}
public int getCol() {
return col;
}
public int getRow() {
return row;
}
}
---------------------------
241 46 112 113 177
320 82 482 381 329
78 435 354 399 53
381 5 354 478 198
Min is 5. Row: 4, Column : 2