您的位置:首页 > 编程语言 > Java开发

Java的一些简单示例(2)

2016-05-03 23:04 555 查看

1、乘法口诀表(1)

public class Test {

public static void main(String[] args) {

int array[][] = new int[10][10];

for(int i=0;i<10;i++){
array[0][i] = i;
array[i][0] = i;
}

for(int i=1;i<10;i++){
for(int j=1;j<10;j++){
array[i][j] = array[i][0] * array[0][j];
}
}

for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(array[i][j] < 10){
System.out.print(" ");
}
System.out.print(array[i][j] + "   ");
}
System.out.println();
}
}
}


结果:

0    1    2    3    4    5    6    7    8    9
1    1    2    3    4    5    6    7    8    9
2    2    4    6    8   10   12   14   16   18
3    3    6    9   12   15   18   21   24   27
4    4    8   12   16   20   24   28   32   36
5    5   10   15   20   25   30   35   40   45
6    6   12   18   24   30   36   42   48   54
7    7   14   21   28   35   42   49   56   63
8    8   16   24   32   40   48   56   64   72
9    9   18   27   36   45   54   63   72   81


乘法口诀表(2)

public class Test {

public static void main(String[] args) {

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

System.out.print(i+"*"+j+"="+(i*j));
if(i*j < 10){
System.out.print(" ");
}
System.out.print("    ");
}
System.out.println();
}
}
}


结果:

1*1=1
2*1=2     2*2=4
3*1=3     3*2=6     3*3=9
4*1=4     4*2=8     4*3=12    4*4=16
5*1=5     5*2=10    5*3=15    5*4=20    5*5=25
6*1=6     6*2=12    6*3=18    6*4=24    6*5=30    6*6=36
7*1=7     7*2=14    7*3=21    7*4=28    7*5=35    7*6=42    7*7=49
8*1=8     8*2=16    8*3=24    8*4=32    8*5=40    8*6=48    8*7=56    8*8=64
9*1=9     9*2=18    9*3=27    9*4=36    9*5=45    9*6=54    9*7=63    9*8=72    9*9=81


2、10个学生考完期末考试评卷完成后,A老师需要划出及格线,要求如下:

(1) 及格线是10的倍数;

(2) 保证至少有60%的学生及格;

(3) 如果所有的学生都高于60分,则及格线为60分

public class Test {

public static void main(String[] args) {

int score[] = new int[10];
int s[] = new int[7];
int j;
Scanner sc = new Scanner(System.in);
for(int i=0;i<10;i++){
score[i] = sc.nextInt();
switch(score[i
e8e9
]/10){
case(10):
case(9):
case(8):
case(7):
case(6):s[6]++; break;
case(5):s[5]++; break;
case(4):s[4]++; break;
case(3):s[3]++; break;
case(2):s[2]++; break;
case(1):s[1]++; break;
case(0):s[0]++; break;
}
}
for(j=6;j>0;j--){
if(s[j] >= 6){
break;
}else{
s[j-1] = s[j-1] + s[j];
}
}
System.out.println("及格线是:"+(j*10));
}
}


3、有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和。

public class Test{

public static void main(String args[]){

double sum = 0;
int m = 1;
int n = 2;
int temp;

for(int i=1;i<=20;i++){
sum = sum + (double)n/m;
temp = n;
n = m + n;
m = temp;
}

System.out.println(sum);
}
}


4、给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。

import java.util.Scanner;

public class Test{

public static void main(String args[]){
Scanner s = new Scanner(System.in);
System.out.println("请输入一个正整数!");
long num = s.nextLong();
if(num<=0){
System.out.println("输入错误,请重新输入!");
System.exit(0);
}
String snum = Long.toString(num);
char cnum[] = snum.toCharArray();
int length = cnum.length;
System.out.println("输入的数字的长度为:"+length);

System.out.println("逆序输出为:");
for(int i=length-1;i>=0;i--){
System.out.print(cnum[i]);
}
}
}


5、判断输入的数是否是回文数。

import java.util.Scanner;

public class Test{

public static void main(String args[]){
Scanner s = new Scanner(System.in);
System.out.println("请输入一个正整数!");
long num = s.nextLong();
if(num<=0){
System.out.println("输入错误,请重新输入!");
System.exit(0);
}
String snum = Long.toString(num);
char cnum[] = snum.toCharArray();
int length = cnum.length;
boolean temp = true;

for(int i=0;i<length/2;i++){
if(cnum[i] != cnum[length-1-i]){
temp = false;
break;
}
}
if(temp == true){
System.out.println("这是一个回文数!");
}else{
System.out.println("这不是一个回文数!");
}
}
}


6、请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。

import java.util.Scanner;

public class Test {

public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.println("请输入星期!");
String ss = s.nextLine();
char ch = ' ';
char day[] = ss.toCharArray();
if (day[0] < 'A' || day[0] > 'Z') {
System.out.println("输入错误,请重新输入");
System.exit(0);
} else {
ch = day[0];
}

switch (ch) {
case ('M'):
System.out.println("Monday");
break;
case ('W'):
System.out.println("Wednesday");
break;
case ('F'):
System.out.println("Friday");
break;
case ('T'):
ch = day[1];
if (ch == 'u') {
System.out.println("Tuesday");
break;
} else if (ch == 'h') {
System.out.println("Thursday");
break;
} else {
System.out.println("error!");
}
case ('S'):
ch = day[1];
if (ch == 'a') {
System.out.println("Saturday");
break;
} else if (ch == 'u') {
System.out.println("Sunday");
break;
} else {
System.out.println("error!");
}
default:System.out.println("输入不正确,请重新输入!");
}
}
}


7、求100以内的素数。

public class Test {

public static void main(String args[]) {

for (int i = 2; i <= 100; i++) {
boolean temp = true;
for (int k = 2; k <= i / 2; k++) {
if (i % k == 0) {
temp = false;
break;
}
}
if (temp == true) {
System.out.print(i + "  ");
}
}
}
}


8、输入10个数,并将其按照从小到大的顺序排列。

import java.util.Scanner;

public class Test {

public static void main(String args[]) {

int num[] = new int[10];
int temp = 0;
Scanner s = new Scanner(System.in);
System.out.println("请输入数据!");
for(int i=0;i<10;i++){
num[i] = s.nextInt();
}

System.out.println("排序前:");
for(int k=0;k<10;k++){
System.out.print(num[k]+"  ");
}

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

System.out.println("排序后:");
for(int k=0;k<10;k++){
System.out.print("\n"+num[k]+"  ");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: