導航:首頁 > 編程語言 > java經典編程練習題

java經典編程練習題

發布時間:2023-10-07 09:41:59

A. java編程基礎練習題

這道題的答案是C。

double[] num1; //定義一個double類型的數組num1
double num3=2.0; //定義一個double類型的變數並賦值為2.0
int num2=5; //定義一個int類型的冰涼num2,並賦值為5
num1=new double[num2+1];
//給double類型的數組num1賦值,並初始化為5+1個大小, num2=5,所以是5+1
num1[num2]=num3;
//上面這句翻譯過來就是:數組num1[5]=2.0 。 由於數組下標從0開始,所以下標為5的元素是最後一個元素。 所以答案是C

B. 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();
}

}

以上運行過了可以!

閱讀全文

與java經典編程練習題相關的資料

熱點內容
為什麼安卓沒白鳥 瀏覽:233
程序員投行 瀏覽:313
java多線程讀取文件 瀏覽:145
香港外貿伺服器有什麼好處 瀏覽:612
鄧倫參加密室大逃脫結果變成團寵 瀏覽:847
購買文件伺服器怎麼選擇 瀏覽:720
空調壓縮機高壓報警 瀏覽:498
u盤數控程序放哪個文件夾 瀏覽:853
python模擬微信登錄其他APP 瀏覽:301
綁扎鋼筋加密區規范 瀏覽:671
怎麼更換手機壁紙安卓 瀏覽:808
閑魚app賣手機怎麼走驗機 瀏覽:821
安卓三個按鍵音怎麼關閉 瀏覽:64
esp8266手機app源碼 瀏覽:713
伺服器如何建立多個站點 瀏覽:151
加密狗可以在筆記本上做賬嗎 瀏覽:888
學生雲伺服器推薦 瀏覽:509
android銀行卡快捷支付 瀏覽:828
海口手機直播系統源碼 瀏覽:416
怎麼給加密貨幣加圖標 瀏覽:999