77范文网 - 专业文章范例文档资料分享平台

JAVA编程题全集(100题及答案)(6)

来源:网络收集 时间:2019-08-20 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

class getChar{

public char getChar() {

Scanner s = new Scanner(System.in); String str = s.nextLine(); char ch = str.charAt(0); if(ch<'A' || ch>'Z') {

System.out.println(\输入错误,请重新输入\ ch=getChar(); }

return ch; } }

【程序27】

题目:求100之内的素数

//使用除sqrt(n)的方法求出的素数不包括2和3 public class lianxi27 {

public static void main(String[] args) { boolean b =false;

System.out.print(2 + \ System.out.print(3 + \ for(int i=3; i<100; i+=2) {

for(int j=2; j<=Math.sqrt(i); j++) { if(i % j == 0) {b = false;

break; } else{b = true;} }

if(b == true) {System.out.print(i + \ } } }

//该程序使用除1位素数得2位方法,运行效率高通用性差。 public class lianxi27a {

public static void main(String[] args) { int[] a = new int[]{2, 3, 5, 7};

for(int j=0; j<4; j++)System.out.print(a[j] + \ boolean b =false;

for(int i=11; i<100; i+=2) { for(int j=0; j<4; j++) {

if(i % a[j] == 0) {b = false;

break; } else{b = true;} }

if(b == true) {System.out.print(i + \ }

} }

【程序28】

题目:对10个数进行排序 import java.util.*; public class lianxi28 {

public static void main(String[] args) { Scanner s = new Scanner(System.in); int[] a = new int[10];

System.out.println(\请输入10个整数:\ for(int i=0; i<10; i++) { a[i] = s.nextInt(); }

for(int i=0; i<10; i++) { for(int j=i+1; j<10; j++) { if(a[i] > a[j]) { int t = a[i]; a[i] = a[j]; a[j] = t; } } }

for(int i=0; i<10; i++) {

System.out.print(a[i] + \ } } }

【程序29】

题目:求一个3*3矩阵对角线元素之和 import java.util.*; public class lianxi29 {

public static void main(String[] args) { Scanner s = new Scanner(System.in); int[][] a = new int[3][3];

System.out.println(\请输入9个整数:\ for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { a[i][j] = s.nextInt(); } }

System.out.println(\输入的3 * 3 矩阵是:\ for(int i=0; i<3; i++) { for(int j=0; j<3; j++) {

System.out.print(a[i][j] + \

}

System.out.println(); }

int sum = 0;

for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(i == j) {

sum += a[i][j]; } } }

System.out.println(\对角线之和是:\} }

【程序30】

题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

//此程序不好,没有使用折半查找插入 import java.util.*; public class lianxi30 {

public static void main(String[] args) {

int[] a = new int[]{1, 2, 6, 14, 25, 36, 37,55}; int[] b = new int[a.length+1]; int t1 =0, t2 = 0;

int i =0;

Scanner s= new Scanner(System.in); System.out.print(\请输入一个整数:\ int num = s.nextInt(); if(num >= a[a.length-1]) { b[b.length-1] = num;

for(i=0; i

} else {

for(i=0; i= a[i]) { b[i] = a[i]; } else { b[i] = num; break; } }

for(int j=i+1; j

for (i = 0; i < b.length; i++) { System.out.print(b[i] + \ }

} }

【程序31】

题目:将一个数组逆序输出。 import java.util.*; public class lianxi31 {

public static void main(String[] args) { Scanner s = new Scanner(System.in); int a[] = new int[20];

System.out.println(\请输入多个正整数(输入-1表示结束):\ int i=0,j; do{

a[i]=s.nextInt(); i++;

}while (a[i-1]!=-1);

System.out.println(\你输入的数组为:\ for( j=0; j

System.out.print(a[j]+\ \}

System.out.println(\数组逆序输出为:\ for( j=i-2; j>=0; j=j-1) {

System.out.print(a[j]+\ \}

} }

【程序32】

题目:取一个整数a从右端开始的4~7位。 import java.util.*; public class lianxi32 {

public static void main(String[] args) { Scanner s = new Scanner(System.in);

System.out.print(\请输入一个7位以上的正整数:\ long a = s.nextLong();

String ss = Long.toString(a); char[] ch = ss.toCharArray(); int j=ch.length;

if (j<7){System.out.println(\输入错误!\

else {

System.out.println(\截取从右端开始的4~7\ } } }

【程序33】

题目:打印出杨辉三角形(要求打印出10行如下图) 1

1 1 1 2 1

1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 ????

public class lianxi33 {

public static void main(String[] args) { int[][] a = new int[10][10]; for(int i=0; i<10; i++) { a[i][i] = 1; a[i][0] = 1; }

for(int i=2; i<10; i++) { for(int j=1; j

a[i][j] = a[i-1][j-1] + a[i-1][j]; } }

for(int i=0; i<10; i++) {

for(int k=0; k<2*(10-i)-1; k++) { System.out.print(\ }

for(int j=0; j<=i; j++) {

System.out.print(a[i][j] + \ \ }

System.out.println(); } } }

【程序34】

题目:输入3个数a,b,c,按大小顺序输出。 import java.util.Scanner; public class lianxi34 {

public static void main(String[] args) { Scanner s = new Scanner(System.in);

位是:

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库JAVA编程题全集(100题及答案)(6)在线全文阅读。

JAVA编程题全集(100题及答案)(6).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/jiaoyu/681460.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: