导航:首页 > 编程语言 > 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经典编程练习题相关的资料

热点内容
光遇为什么之前没有安卓服 浏览:745
移动硬盘显示可用加密 浏览:946
python万能库开发 浏览:875
向日葵远程解压 浏览:883
androidedittext布局 浏览:320
题库管理app哪个好用 浏览:989
安卓游戏中亮度自动调节如何关闭 浏览:892
求派算法 浏览:551
pythonweb编程实例 浏览:190
鞋盒怎么做文件夹收纳盒视频 浏览:757
模拟电子技术第四版pdf 浏览:961
解压车贷后gps怎么找 浏览:352
源码数据库怎么配备 浏览:138
知乎程序员小灰 浏览:574
新概念英语第一册书pdf 浏览:8
安卓ans文件怎么打开 浏览:895
选择题改进分治算法的方法有 浏览:110
下载云服务器有什么好处 浏览:23
江苏机架式服务器云主机 浏览:411
linux补全命令 浏览:514