3.代码填空. 键盘输入Scanner
下列Java应用程序是用户从键盘输入个人信息,然后程序将个人信息输出。
请按程序实现要求,将下面程序的【代码】替换为Java程序代码。
文件Main.java
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner reader = new Scanner(System.in);
System.out.println("输入姓名(回车确认):");
String name = reader.next(); // 从键盘为name赋值 【代码1】
System.out.println("输入年龄(回车确认):");
byte age = reader.nextByte(); // 从键盘为age赋值 【代码2】
System.out.println("输入身高(回车确认):");
float height = reader.nextFloat(); // 从键盘为height赋值 【代码3】
System.out.println("--基本信息--");
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
System.out.println("身高:" + height);
}
}
8.代码填空. 求四位回文数之和
下面程序是求四位回文数之和。例如1221是一个回文数,它从左边读和从右边读是一样的,下面求所有这样的四位十进制数之和。
请将下面程序的【代码】替换为Java程序代码,使程序运行正确。
文件Main.java
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum = 0; // 【代码1】
for (int i = 1000; i < 10000; i++) { // 【代码2】
int k = i / 1000;
int m = (i % 1000) / 100;// 【代码3】
int n = (i / 10) % 10;
int l = i % 10;
if (k == l && m == n) {
sum += i; // 【代码4】
}
}
System.out.println(sum);// 【代码5】
}
}
16.编程序求[200,M]中的所有素数之和
输入格式
输入1个整数M。
输出格式
[200,M]中的所有素数之和。
样例1输入:
300
样例1输出:
4048
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader = new Scanner(System.in);
while (reader.hasNext()) {
int m, sum = 0;
m = reader.nextInt();
for (int i = 200; i <= m; i++) {
boolean flag = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
flag = false;
break;
}
}
if (flag == true)
sum += i;
}
System.out.println(sum);
}
}
}
17.编程:统计大小写字母个数(字符串)
输入格式
输入一个字符串。
输出格式
输出一个整数。
样例输入
abc123AB
样例输出
5
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader = new Scanner(System.in);
while (reader.hasNext()) {
int sum = 0;
String str;
str = reader.next();
char[] ch = str.toCharArray();
for(int i = 0; i < str.length(); i++)
if((ch[i] >= 'a' && ch[i] <= 'z')||(ch[i] >= 'A' && ch[i] <= 'Z'))
sum+=1;
System.out.println(sum);
}
}
}
18.编程序求[200,M]上的所有闰年之和
判定公历闰年遵循的规律为: 四年一闰,百年不闰,四百年再闰.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader = new Scanner(System.in);
int m,sum = 0;
while(reader.hasNext()){
m = reader.nextInt();
for(int i = 200;i <= m;i++){
if(i%4==0&&i%100!=0||i%400==0)
sum+=i;
}
System.out.println(sum);
}
}
}
19.编程:求n个实数中的最大数
输入格式
n
n个用空格分隔开的实数。
输出格式
输出一个实数。
样例输入
4
2.9 20 8.1 9.4
样例输出
20.0
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader = new Scanner(System.in);
while(reader.hasNext()) {
int n = reader.nextInt();
double [] exam = new double [10005];
for(int i = 0; i < n; i++) {
exam[i]=reader.nextDouble();
}
double max = exam[0];
for(int i = 0; i < n; i++) {
if(exam[i] > max)
max = exam[i];
}
System.out.println(max);
}
}
}
22.编写程序:斐波那契图形
编写一个求斐波那契数列的递归函数,输入n值,使用该递归函数,输出如样例输出的斐波那契数列。
输入:
一个整型数n
输出:
题目可能有多组不同的测试数据,对于每组输入数据,
按题目的要求输出相应的斐波那契图形。
注意输出换行前不能有多余的空格。
样例输入:
6
样例输出:
0
0 1 1
0 1 1 2 3
0 1 1 2 3 5 8
0 1 1 2 3 5 8 13 21
0 1 1 2 3 5 8 13 21 34 55
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader = new Scanner(System.in);
while (reader.hasNext()) {
int n = reader.nextInt();
for(int i = 1; i <= n; i++) {
int index = 1;
for(int j = 0; j < 4*i-3; j++) {
if(j == 0)
System.out.print(0);
else if(j%2==0) {
System.out.print(getFibo(index));
index++;
}
else
System.out.print(' ');
}
System.out.println();
}
}
}
public static int getFibo(int i) {
if (i == 1 || i == 2)
return 1;
else
return getFibo(i - 1) + getFibo(i - 2);
}
}
29.编写程序输出对称平方数
打印所有不超过n(0<=n<256)的,其平方具有对称性质的数。
如11*11=121(即输出256以内的不包括256的符合该性质的数)
public class Main {
public static boolean isSymmetryNumber(long n) {
long num = n * n;
String str = String.valueOf(num);
return isSymmetString(str);
}
public static boolean isSymmetString(String str) {
int count = str.length();
String str1 = str.substring(0, count / 2);
String str2 = str.substring(((count & 0x1) == 0) ? (count / 2) : (count / 2 + 1)); //& 0x1的作用是只保留第一位的值
StringBuffer strBuf = new StringBuffer(str2);
str2 = strBuf.reverse().toString(); //String类中没有reverse方法
return str1.equals(str2);
}
public static void main(String[] args) {
for (long i = 0; i < 256; i++) {
if(Main.isSymmetryNumber(i))
System.out.println(i);
}
}
}
31.编写程序求各位数字之和(10分)
题目描述:
对于给定的正整数 n,计算其十进制形式下所有位置数字之和,并计算其平方的各位数字之和。
输入:
每行输入数据包括一个正整数n(0<n<40000),如果n=0 表示输入结束,并不用计算。
输出:
对于每个输入数据,计算其各位数字之和,以及其平方值的数字之和,输出在一行中,之间用一个空格分隔,但行末不要有空格。
样例输入:
4
12
97
39999
0
样例输出:
4 7
3 9
16 22
39 36
import java.util.*;
public class Main extends Thread {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
if (n != 0) {
System.out.println(getsum(n) + " " + getsum(n * n));
}
}
}
private static int getsum(int i) {
// TODO Auto-generated method stub
int sum = 0;
int[] s = new int[10];
int index = 0;
while (i != 0) {
s[index] = i % 10;
sum += s[index];
index++;
i = i / 10;
}
return sum;
}
}
32.与7无关的数平方和
题目描述:
一个正整数,如果它能被7整除,或者它的十进制表示法中某个位数上的数字为7,
则称其为与7相关的数.现求所有小于等于n(n<100)的与7无关的正整数的平方和。
输入:
案例可能有多组。对于每个测试案例输入为一行,正整数n,(n<100)
输出:
对于每个测试案例输出一行,输出小于等于n的与7无关的正整数的平方和。
样例输入:
21
样例输出:
2336
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
while(reader.hasNext()) {
int num = reader.nextInt();
int sum = 0;
for(int i = 1; i <= num; i++) {
if(i%7 != 0 && (i %10 != 7) && ( i/10 != 7)) { //全部为且条件
sum+=i*i;
}
}
System.out.println(sum);
}
}
}
65. 编写部分代码, 使用内部类判断一个long型整数n是否为回文数
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner reader=new Scanner(System.in);
long n=reader.nextLong();
Calc a=new Calc();
boolean b=a.judge(n);
if(b)
System.out.println("yes");
else
System.out.println("no");
System.out.println("The Main class is end.");
}
}
class Calc {
public Calc(){
}
public boolean judge(long n){
/*-----请编写代码-----*/
long rev = 0 ;
if(n < 0 || (n % 10 == 0 && n != 0)) {
return false;
}
while (n > rev)
{
rev = rev*10+n%10;
n /= 10;
}
return n == rev || n == rev/10;
}
/*-----编写的代码结束-----*/
}
66. 填写代码,算术运算异常处理(Try/Catch-finally)例子
public class Main {
public static void main(String[] args) {
try {
int i = 0;
i = 3 / i;// 将产生算术运算异常。
} catch (ArithmeticException e)// 找到了匹配的异常,执行该catch块。
{
System.out.println("发生了ArithmeticException异常");
} catch (ArrayIndexOutOfBoundsException e)// 不是匹配的异常,则不会再捕获
// 异常,如果发生数组下标(索引)超出范围所产生的异常,将执行该catch块。
{
System.out.println("发生了ArrayIndexOutOfBoundsException异常");
} catch (Exception e)
// 前两个catch块均无法捕获try块中发生的异常时,才会执行该catch块。
{
System.out.println("发生了异常");
} finally {
{
System.out.println("执行Finally");
}
}
}
}
67. 填写代码,算术运算异常处理(Try/Catch)例子
import java.util.*;
public class Main {
public static void main(String[] args) {
// 键盘输入数n作为分母,做除法
Scanner in = new Scanner(System.in);
int y = 0;
int n = 0;
try {
n = in.nextInt();
y = (3 / n);
} catch (NumberFormatException e) {// 输入数据不匹配
// TODO Auto-generated catch block
e.printStackTrace();// 显示堆栈数据
System.out.println("请输入整数!");
} catch (ArithmeticException e) {
// TODO Auto-generated catch block
System.out.println("请输入非零整数!");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("出现了未知情况!");
}
System.out.println("程序正常结束!");
}
}
68.填写代码,自定义异常处理类(继承Exception)例子
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String st = in.nextLine();// 键盘输入字符串作为提示信息
try {
throw new MyException(st);
} catch (MyException e) {
// TODO Auto-generated catch block
System.out.println(st);
}
}
}
// 子类,继承Exception
class MyException extends Exception {
public MyException() {
super();
// TODO Auto-generated constructor stub
}
public MyException(String s) {
super(s);
// TODO Auto-generated constructor stub
}
}
69.填写代码,ArrayList类的方法
import java.util.*;
public class Main {
public static void main(String[] args) {
Show aShow = new Show();// 显示元素
ArrayList<String> list1 = new ArrayList<String>();
list1.ensureCapacity(200);
Scanner scanner = new Scanner(System.in);
list1.add("abc");
list1.add("123");
int i = scanner.nextInt();
list1.add(Integer.toString(i));// 转换为字符串再加入
aShow.showList(list1);
Object[] ob = list1.toArray();// 转换为字符串
String st1 = (String) ob[0];// 取出下标为0的元素
System.out.println("下标为0的元素为:" + st1);
String st2 = list1.get(2);// 取出下标为2的元素
System.out.println("下标为2的元素为:" + st2);
list1.remove(1);// 删除下标为1的元素
aShow.showList(list1);
ArrayList<String> arrayList1 = new ArrayList();
arrayList1.add(0, "The");
arrayList1.add(1, "fox");
arrayList1.add(1, "jumps");
arrayList1.addAll(1, list1);// 将list1加入arrayList1
aShow.showList(arrayList1);
}
}
// 显示元素的类
class Show {
public void showList(ArrayList<String> s) {
Iterator<String> iter = s.iterator();
while (iter.hasNext()) {
String st = iter.next();
System.out.print(st + " ");
}
System.out.println();
}
}
70.填写代码,使用ArrayList类进行排序
import java.util.*;
public class Main {
public static void main(String[] args) {
Show aShow = new Show();// 显示元素
ArrayList<Integer> list1 = new ArrayList<Integer>();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
list1.ensureCapacity(n);
while (n-- > 0) {// 输入n个数
int k = in.nextInt();
list1.add(k);// 加入元素
}
Collections.sort(list1);// 排序
aShow.showList(list1);
}
}
// 显示元素的类
class Show {
public void showList(ArrayList<Integer> list1) {
Iterator<Integer> iter = list1.iterator();
while (iter.hasNext()) {
int st = iter.next();
System.out.print(st + " ");
}
System.out.println();
}
}
67.
import java.util.*;
public class Main {
public static void main(String[] args) {
// 键盘输入数n作为分母,做除法
Scanner in = new Scanner(System.in);
int y = 0;
int n = 0;
try {
n = in.nextInt();
y = (3 / n);
} catch (NoSuchElementException e) {//输入数据不匹配
// TODO Auto-generated catch block
e.printStackTrace();//显示堆栈数据
System.out.println("请输入整数!");
} catch (ArithmeticException e) {
// TODO Auto-generated catch block
System.out.println("请输入非零整数!");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("出现了未知情况!");
}
System.out.println("程序正常结束!");
}
}
69.填写代码,ArrayList类的方法
import java.util.*;
public class Main {
public static void main(String[] args) {
Show aShow =new Show();//显示元素
ArrayList<String> list1=new ArrayList<String>();
list1.ensureCapacity(200);
Scanner scanner=new Scanner(System.in);
list1.add("abc");
list1.add("123");
int i=scanner.nextInt();
list1.add(Integer.toString(i));//转换为字符串再加入
aShow.showList(list1);
Object[] ob=list1.toArray();//转换为字符串
String st1=(String)ob[0];//取出下标为0的元素
System.out.println("下标为0的元素为:"+st1);
String st2=list1.get(2);//取出下标为2的元素
System.out.println("下标为2的元素为:"+st2);
list1.remove(1);//删除下标为1的元素
aShow.showList(list1);
ArrayList<String> arrayList1 = new ArrayList();
arrayList1.add(0, "The" );
arrayList1.add( 1, "fox" );
arrayList1.add( 1, "jumps" );
arrayList1.addAll(1,list1);//将list1加入arrayList1
aShow.showList(arrayList1);
}
}
//显示元素的类
class Show{
public void showList(ArrayList<String> s) {
Iterator<String> iter=s.iterator();
while(iter.hasNext()){
String st=iter.next();
System.out.print(st+" ");
}
System.out.println();
}
}
70.填写代码,使用ArrayList类进行排序(Collections工具类)
import java.util.*;
public class Main {
public static void main(String[] args) {
Show aShow =new Show();//显示元素
ArrayList<Integer> list1=new ArrayList<Integer>();
Scanner in=new Scanner(System.in);
int n=in.nextInt();
list1.ensureCapacity(n);
while(n--!=0){//输入n个数
int k=in.nextInt();
list1.add(k);//加入元素
}
Collections.sort(list1);//排序 Collections 工具类
aShow.showList(list1);
}
}
//显示元素的类
class Show{
public void showList(ArrayList<Integer> list1) {
Iterator<Integer> iter=list1.iterator();
while(iter.hasNext()){
int st=iter.next();
System.out.print(st+" ");
}
System.out.println();
}
}
71.编写部分代码. 求散列码之和(arraylist).
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
ArrayList<Integer> a = new ArrayList<Integer>();
for (int i = 0; i < n; i++)
a.add(reader.nextInt());
HashCodeSum x = new HashCodeSum();
System.out.println(x.sum(a));// 显示结果
System.out.println("The Main class is end.");
}
}
class HashCodeSum {
int sum(ArrayList<Integer> a) {
int sum = 0;
/*-----请编写代码-----*/
for(int i = 0; i < a.size(); i++)
sum += a.get(i);
/*-----编写的代码结束-----*/
return (sum);
}
}
73.填写代码,功能是实现Runnable并输出字符串
import java.util.*;
public class Main implements Runnable{ //[implements]
static String s;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
s = scanner.nextLine();
Thread t=new Thread(new Main()); //Main
t.run();
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(s);
}
}