當前位置:編程學習大全網 - 編程語言 - JAVA編程的幾個簡單題目

JAVA編程的幾個簡單題目

第壹個:

import java.util.Scanner;

import java.util.*;

public class Validate

{

private int n;

/*count_6、count_7、count_8 用來記錄收斂那個數字的個數,在這裏我記錄只要他出現了10次我就認為他收斂與他了

* 還沒想到更好的辦法,如果不設置這個,就會出現棧溢出,遞歸不出來了!不過可以看到結果輸出結果確實是對的

*/

private int count_6;

private int count_7;

private int count_8;

private Stack<Integer> stack= new Stack<Integer>();//棧用來存放素因子

public void scan()

{

Scanner scan = new Scanner(System.in);

try{

n = scan.nextInt();

}catch(NumberFormatException ne){

System.out.println(ne.getMessage());

}

}

public boolean isPrime(int n)

{

if(n>2 && 0 == n%2)//是大於2偶數

{

return false;

}else{

for(int i=3; i<n; i +=2)

{

if(0 == n%i)

return false;

}

return true;

}

}

public void analyze(int n)

{

if(isPrime(n))

{

stack.push(n);

return;

}

if(0 == n%2){

stack.push(2);

n = n/2;

analyze(n);

}else{

for(int i=3; i<n; i +=2)

{

if(isPrime(i)&& 0 == n%i)

{

stack.push(i);

n = n/i;

analyze(n);

}

}

}

}

public void mySort()

{

check(n);

}

public void check(int m)

{

if(isPrime(m)){

m++;

}

else{

analyze(m);

m = 0;

while(!stack.empty())

{

int k = stack.pop().intValue();

m += k;

}

stack.clear();

m++;

}

if(m == 6 || m == 7 || m == 8)

{

if(6 == m)

{

count_6++;

System.out.println("m = " + m);

}else if(7 == m){

count_7++;

System.out.println("m = " + m);

}else if(8 == m){

count_8++;

System.out.println("m = " + m);

}

}

if(count_6 > 10 || count_7 > 10 || count_8 > 10)

{

return;

}

check(m);

}

public static void main(String[] args)

{

Validate v = new Validate();

v.scan();

v.mySort();

}

}

第二個:

import java.util.Scanner;

class MyException extends Exception

{

public MyException(String msg)

{

super(msg);

}

}

public class MyExceptionTest {

public int scan()

{

int a = 0;

Scanner scan = new Scanner(System.in);

try{

a = scan.nextInt();

}catch(NumberFormatException ne){

System.out.println(ne.getMessage());

}

return a;

}

public int cal(int a, int b) throws MyException

{

if(b==0) throw new MyException("自定義異常: 輸入的第二個數為0");

else if(a/b<1) throw new MyException("自定義異常: 相除的結果小於1");

else return a/b;

}

public static void main(String[] args) {

MyExceptionTest me = new MyExceptionTest();

System.out.print("請輸入第壹個數:");

int a = me.scan();

System.out.print("請輸入第二個數:");

int b = me.scan();

try{

System.out.println("相除的結果:" + me.cal(a, b));

}catch(MyException e){

System.out.println(e.getMessage());

}

}

}

第三個:

import java.util.*;

import java.util.Map.Entry;

public class CountCharacter {

private static Map<String, Integer> map = new LinkedHashMap<String, Integer>();

private static final int ONE = 1 ; //沒有出現過,則設置其出現壹次;

private String content = null;

public void scan()

{

Scanner scan = new Scanner(System.in);

content = scan.nextLine();

}

public void count()

{

String [] text = content.split(" ");

for(int i=0; i<text.length; i++)

{

if (!map.containsKey(text[i])) {

map.put(text[i], ONE);

} else {

int value = map.get(text[i]);

map.put(text[i], value + 1);

}

}

}

public <K, V extends Number> Map<String, V> sortMap(Map<String, V> map) {

class MyMap<M, N> {

private M key;

private N value;

private M getKey() {

return key;

}

private void setKey(M key) {

this.key = key;

}

private N getValue() {

return value;

}

private void setValue(N value) {

this.value = value;

}

}

List<MyMap<String, V>> list = new ArrayList<MyMap<String, V>>();

for (Iterator<String> i = map.keySet().iterator(); i.hasNext(); ) {

MyMap<String, V> my = new MyMap<String, V>();

String key = i.next();

my.setKey(key);

my.setValue(map.get(key));

list.add(my);

}

Collections.sort(list, new Comparator<MyMap<String, V>>() {

public int compare(MyMap<String, V> o1, MyMap<String, V> o2) {

if(o1.getValue().equals(o2.getValue())) {

return o1.getKey().compareTo(o2.getKey());

}else{

return (int)(o2.getValue().doubleValue() - o1.getValue().doubleValue());

}

}

});

Map<String, V> sortMap = new LinkedHashMap<String, V>();

for(int i = 0, k = list.size(); i < k; i++) {

MyMap<String, V> my = list.get(i);

sortMap.put(my.getKey(), my.getValue());

}

return sortMap;

}

public static void main(String[] args) {

CountCharacter cc = new CountCharacter();

cc.scan();

cc.count();

Map<String, Integer> sortMap = cc.sortMap(cc.map);

Iterator<Entry<String, Integer>> it = sortMap.entrySet().iterator();

Map.Entry<String,Integer> entry = null;

int i=0;

while(it.hasNext()&& i<10) {//去前面10個

i++;

entry = (Entry<String,Integer>) it.next();

System.out.println(entry.getKey() + " --> " + entry.getValue());

}

}

}

第四個:

import java.util.Scanner;

public class IntegerShape{

public static void main(String[] args){

double a = 0;

int b = 0;

Scanner in = null;

do{

try{

System.out.print("請輸入壹個的數:");

in=new Scanner(System.in);

a=in.nextFloat();

break;

}catch(Exception ne){

System.out.println("輸入數據錯誤!");

}

}while(true);

do{

try{

System.out.print("請輸入顯示的行數:");

in=new Scanner(System.in);

b=in.nextInt();

break;

}catch(Exception ne){

System.out.println("輸入數據錯誤!");

}

}while(true);

for(int i=b;i>0;i--)

{

System.out.println(a);

a=a/2;

}

}

}

第五個:

import java.util.Scanner;

import java.util.Vector;

public class MyVector {

private Vector<String> vectorStr = new Vector<String>(); //用來方單詞

private Vector<Integer> vectorInt = new Vector<Integer>();//用來記錄對應下標的單詞的個數

private static final int ONE = 1 ; //沒有出現過,則設置其出現壹次;

private String content = null;

public void scan()

{

Scanner scan = new Scanner(System.in);

content = scan.nextLine();

}

public void countWord()

{

int index = -1;

String [] text = content.split(" ");

for(int i=0; i<text.length; i++)

{

if(vectorStr.contains(text[i])){ //若次單詞已經存在與vector中則只要修改對應的個數

index = vectorStr.indexOf(text[i]);

int value = vectorInt.get(index)+1;

vectorInt.setElementAt(value, index);

}

else{//若不存在則添加該單詞,同時要初始化對應下標的單詞的個數

vectorStr.add(text[i]);

vectorInt.add(ONE);

}

}

System.out.println(vectorStr);

}

public void display()

{

for(int i=0; i<vectorStr.size(); i++)

{

System.out.println(vectorStr.get(i) + "->" + vectorInt.get(i));

}

}

public static void main(String[] args) {

MyVector mv = new MyVector();

mv.scan();

mv.countWord();

mv.display();

}

}

最後壹個了不容易!考慮加點分吧!

import java.util.*;

public class Exp {

private int integer;

private boolean bool = false;

private Stack<Integer> stack = new Stack<Integer>();

public void scan()

{

System.out.print("輸入壹個整數:");

Scanner scan = new Scanner(System.in);

while(true){

try{

integer = scan.nextInt();

break;

}catch(Exception e){

System.out.println("輸入錯誤!");

}

}

}

public void operation()

{

for(int i=1; i<integer; i++)

{

int sum = 0;

for(int j = i; j<=integer; j++)

{

stack.push(j);

sum += j;

if(sum == integer)

{

int k = 0, n = stack.size();

bool = true;

System.out.print(integer + " = ");

while(!stack.empty())

{

k++;

if(k<n)

{

System.out.print(stack.pop().intValue() + " + ");

}

else

{

System.out.print(stack.pop().intValue() + "\n");

}

}

stack.clear();

}else if(sum > integer){

stack.clear();

}

}

}

if(!bool)

System.out.println("該整數沒有連續正整數序列!");

}

public static void main(String[] args) {

Exp e = new Exp();

e.scan();

e.operation();

}

}

以上運行過了可以!

  • 上一篇:100人核檢至少
  • 下一篇:怎麽制作折紙戰隊的機器人
  • copyright 2024編程學習大全網