「java数字处理类题目」java能处理的最大数字
今天给各位分享java数字处理类题目的知识,其中也会对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 StackInteger stack= new StackInteger();//栈用来存放素因子
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(n2 0 == n%2)//是大于2偶数
{
return false;
}else{
for(int i=3; in; 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; in; 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/b1) 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 MapString, Integer map = new LinkedHashMapString, 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; itext.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 MapString, V sortMap(MapString, V map) {
class MyMapM, 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;
}
}
ListMyMapString, V list = new ArrayListMyMapString, V();
for (IteratorString i = map.keySet().iterator(); i.hasNext(); ) {
MyMapString, V my = new MyMapString, V();
String key = i.next();
my.setKey(key);
my.setValue(map.get(key));
list.add(my);
}
Collections.sort(list, new ComparatorMyMapString, V() {
public int compare(MyMapString, V o1, MyMapString, V o2) {
if(o1.getValue().equals(o2.getValue())) {
return o1.getKey().compareTo(o2.getKey());
}else{
return (int)(o2.getValue().doubleValue() - o1.getValue().doubleValue());
}
}
});
MapString, V sortMap = new LinkedHashMapString, V();
for(int i = 0, k = list.size(); i k; i++) {
MyMapString, 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();
MapString, Integer sortMap = cc.sortMap(cc.map);
IteratorEntryString, Integer it = sortMap.entrySet().iterator();
Map.EntryString,Integer entry = null;
int i=0;
while(it.hasNext() i10) {//去前面10个
i++;
entry = (EntryString,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;i0;i--)
{
System.out.println(a);
a=a/2;
}
}
}
第五个:
import java.util.Scanner;
import java.util.Vector;
public class MyVector {
private VectorString vectorStr = new VectorString(); //用来方单词
private VectorInteger vectorInt = new VectorInteger();//用来记录对应下标的单词的个数
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; itext.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; ivectorStr.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 StackInteger stack = new StackInteger();
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; iinteger; 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(kn)
{
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编程题目:编写一个程序,输入一个三位正整数,输出个、十、百位数字的立方和。新手,请详细一点
import java.util.*;
public class YUGI{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("输入一个三位数: ");
int abc = sc.nextInt();
sc.close();
int a = abc/100;
int b = abc / 10 % 10;
int c = abc % 10;
int x = Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3);
System.out.println("立方和:" +x);
}
}
java数字的处理
while(i%3==0i=(str1.length()-1)) 改成if(i%3==0i=(str1.length()-1))
否则是个死讯坏,直到内存耗尽为止
关于java数字处理类题目和java能处理的最大数字的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。