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