當前位置:編程學習大全網 - 編程語言 - 真心求高手幫忙編壹個JAVA程序

真心求高手幫忙編壹個JAVA程序

測試實體類:

package quiz;

public class Quiz {

private int[] score;

private int count;

private String name;

public Quiz(int count, String name) {

super();

this.count = count;

this.name = name;

}

public int[] getScore() {

return score;

}

public void setScore(int[] score) {

this.score = score;

}

public int getCount() {

return count;

}

public void setCount(int count) {

this.count = count;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

測試主方法:

package quiz;

import java.util.Arrays;

import java.util.Scanner;

public class QuizTest {

private static int numAdd;

private static String stuName;

private static int[] temp;

private static Quiz myQuiz = new Quiz(numAdd, stuName); // 初始化

/*

* 添加輸入的分數

* */

public static int[] add(int[] a, int e) {

a = Arrays.copyOf(a, a.length + 1);

a[a.length - 1] = e;

return a;

}

/*刪除指定的數,如果沒有提示*/

public static void delete(int delEle){

int remIndex = Arrays.binarySearch(temp,delEle);

if(remIndex != -1){

temp = removeElement(temp,remIndex);

}else{

System.out.println("the value "+delEle+" was not found and cannot be deleted!");

}

}

/*輸出重復的*/

public static int seach(int num){

int findNum = 0;

return findNum;

}

public static void print(){

System.out.println(stuName+"\n");

for (int ia: temp) {

System.out.print(ia+" ");

}

}

public static void printMenu()

{

System.out.print("\nCommand Options\n"

+ "-----------------------------------\n"

+ "n: Create a new data\n"

+ "a: Add a score\n"

+ "d: Delete a score\n"

+ "p: Print the information\n"

+ "?: display the menu again\n"

+ "q: Quit this program\n\n");

} // end of the printMenu method

public static int[] removeElement(int[] original, int element){

int[] n = new int[original.length - 1];

System.arraycopy(original, 0, n, 0, element );

System.arraycopy(original, element+1, n, element, original.length - element-1);

return n;

}

public static void main(String[] args) {

int maxSize = 0;

Scanner console = new Scanner (System.in);

temp = new int[maxSize];

String choice;

char command;

// 打印提示菜單

printMenu();

do

{

// 提示用戶輸入命令

System.out.println("\nPlease enter a command or type ?");

choice = console.next().toLowerCase();

command = choice.charAt(0);

switch (command)

{

case 'n': //提示輸入學生名字和分數數組

System.out.println("n [Create a new Quiz]");

System.out.print("[Input the size of quizzes]: ");

maxSize = console.nextInt();

System.out.print("[Input the name of student]: ");

stuName = console.next();

myQuiz.setName(stuName);

break;

case 'a': // 添加分數到數組

System.out.println("a [Add a score]: ");

numAdd = console.nextInt();

if(temp.length <= maxSize){

temp = add(temp,numAdd);

}else{

System.out.println("Array is full. The value "+numAdd+"cannot be added.");

}

myQuiz.setScore(temp);

break;

case 'd': // 刪除指定分數

System.out.println("d [Delete a score]: ");

numAdd = console.nextInt();

delete(numAdd);

break;

case 'p': //輸出信息

System.out.println("p [Print the information]: ");

print();

break;

case '?':

printMenu();

break;

case 'q':

System.out.println("***********End of the program************");

break;

default:

System.out.println("Invalid input");

}

} while (command != 'q');

console.close();

}

}

  • 上一篇:唐山市高新技術開發區的唐山市高新技術開發區
  • 下一篇:51單片機 c語言看門狗程序怎麽寫
  • copyright 2024編程學習大全網