当前位置:首页 > 竞赛java编程题50题(有答案)
}
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++) {
共分享92篇相关文档