1.将1 到1000 之间的奇数打印出来

package com.guet;

//1到1000的奇数打印出来

public class JiShu {

public static void main(String[] args) {

// TODO Auto-generated method stub

int number = 0;

int now = 1;

for (int i = 1; i <= 1000; i++) {

if (now++ % 2 == 1)

number++;

}

System.out.print("1到1000的奇数有"+number+"个");

}

}

4-30第二次

public class One {// 将1到1000之间的奇数打印出来

public static void main(String[] args) {

// TODO Auto-generated method stub

int i;

for (i = 1; i < 1001; i++) {

if (i % 2 == 1) {

System.out.println(i);

}

}

}

}

2.判断一个数能否同时被3和5 整除

package com.guet;

import java.util.Scanner;

//判断一个数能否同时被3和5 整除

public class ZhengChu {

public static void main(String[] args) {

// TODO Auto-generated method stub

int number;

System.out.print("输入一个数:");

Scanner numScanner=new Scanner(System.in);

number=numScanner.nextInt();

if(number%3==0&&number%5==0)

System.out.println(number+"可以 同时被3和5 整除");

else

System.out.println(number+"不能同时被3和5 整除");

}

}

4-30

import java.util.Scanner;

public class Two {// 判断一个数能否同时被3和5整除

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int x = scanner.nextInt();

if (x % 3 == 0 && x % 5 == 0) {

System.out.print("可以同时被3和5整除");

} else {

System.out.print("不可以同时被3和5整除");

}

}

}

3.输入10个数,找出最大一个数,并打印出来。

package com.guet;

import java.util.Scanner;

//输入10个数,找出最大一个数,并打印出来。

public class Max10 {

public static void main(String[] args) {

// TODO Auto-generated method stub

int number;

int temp = 0;

Scanner in = new Scanner(System.in);

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

System.out.print("输入第" + i + "个数:");

number = in.nextInt();

if (number > temp)

temp = number;

}

System.out.print("这10个数中最大的是:" + temp);

}

}

4.给出一百分制成绩,要求输出成绩等级’A’,’B’,’C’,’D’,’E’。90 分以上为’A’,80~89 分为’B’,70~79 分为’C’,60~69 分为’D’,60分以下为’E’

package com.guet;

import java.util.Scanner;

//给出一百分制成绩,要求输出成绩等级’A’,’B’,’C’,’D’,’E’。

//90 分以上为’A’,80~89 分为’B’,70~79 分为’C’,60~69 分为’D’,60分以下为’E’

public class GradeLevel {

public static void main(String[] args) {

// TODO Auto-generated method stub

int grade ,level;

System.out.print("输入你的百分制成绩:");

Scanner inScanner=new Scanner(System.in);

grade=inScanner.nextInt();

if(grade>100||grade<0){

System.out.println("你输入的成绩有误");

System.exit(0);

}

switch (grade%10){

case 10:

case 9:level=65;break;//A

case 8:level=66;break;//B

case 7:level=67;break;//C

case 6:level=68;break;

default :level=69;

}

if(grade==100)

level=65;

System.out.println("你的成绩是"+(char)level);

}

}

5.把一个正整数分解质因数。!注:此方法存在很大问题,因为我还没想好怎么解决质数递增的问题。当前代码运行后,不会自动停止。

package com.google;

import java.util.Scanner;

//把一个正整数分解质因数

public class Five {

public static void main(String[] args) {

// TODO Auto-generated method stub

int number , i=2;

Scanner numScanner = new Scanner(System.in);

System.out.print("输入一个正整数:");

number = numScanner.nextInt();

System.out.print(number+"分解出的质因数有:");

do{

if(number%i==0){

number=number/2;

System.out.print(i+" ");

}else{

i++;

}

}while (true);

}

}

6.打印出菱形

package com.google;

//打印出如下图案(菱形)

// *

// ***

// *****

//*******

// *****

// ***

// *

public class Diamond {

public static void main(String[] args) {

// TODO 自动生成的方法存根

int x, y;

for (x = 1; x < 5; x++) {

for (y = 1; y < (4 + x); y++) {

if (y <= (4 - x))// 原始if(y<=(3+x-(2*x-1)))

System.out.print(" ");

else

System.out.print("*");

}

System.out.println();

}

for (x = 5; x < 8; x++) {

for (y = 1; y < 7; y++) {

if ((x - 4) < y && y < (12 - x))

System.out.print("*");

else if(y<=x-4)

System.out.print(" ");

}

System.out.println();

}

}

}

7.将1至7 的数字转换为星期日到星期六的字符串

package com.google;

import java.util.Scanner;

//将1至7 的数字转换为星期日到星期六的字符串

//这个题目目前不是很理解

public class SevenDays {

public static void main(String[] args) {

// TODO 自动生成的方法存根

int x;

String xString = new String();

System.out.print("输入1到7其中的一个数字:");

Scanner num = new Scanner(System.in);

x = num.nextInt();

switch (x) {

case 1: {

xString = "星期日"+x;

}

break;

case 2: {

xString = "星期一";

xString = Integer.toString(x);

}

break;

case 3: {

xString = "星期二";

xString = Integer.toString(x);

}

break;

case 4: {

xString = "星期三";

xString = Integer.toString(x);

}

break;

case 5: {

xString = "星期四";

xString = Integer.toString(x);

}

break;

case 6: {

xString = "星期五";

xString = Integer.toString(x);

}

break;

case 7: {

xString = "星期六";

xString = Integer.toString(x);

}

break;

}

System.out.print("结果:"+xString);

}

}

8.有任意三个整数a,b,c,请输出其中最大的

importjava.util.Scanner;//有任意三个整数a,b,c,请输出其中最大的

public classEight {public static voidmain(String[] args) {//TODO 自动生成的方法存根

inta, b, c;

Scanner in= newScanner(System.in);

System.out.print("输入第一个数a:");

a=in.nextInt();

System.out.print("输入第二个数b:");

b=in.nextInt();

System.out.print("输入第三个数c:");

c=in.nextInt();if (a >b) {if (a >c) {

System.out.print("a最大");

}else{

System.out.print("c最大");

}

}else{if (b >c) {

System.out.print("b最大");

}else{

System.out.print("c最大");

}

}

}

}

9.将任意三个整数a,b,c按从小到大的顺序输出

importjava.util.Scanner;//将任意三个整数a,b,c按从小到大的顺序输出

public classNine {public static voidmain(String[] args) {//TODO 自动生成的方法存根

inta, b, c;

Scanner in= newScanner(System.in);

System.out.print("输入a:");

a=in.nextInt();

System.out.print("输入b:");

b=in.nextInt();

System.out.print("输入c:");

c=in.nextInt();if (a == b || a == c || b ==c) {

System.out.print("你输入的数字中有相等的,程序结束。");

System.exit(0);

}if (a >b) {if (a >c) {//************* a>c *************

if (b >c) {

System.out.print("a>b>c");

}else{

System.out.print("a>c>b");

}//************* a>c *************

} else{

System.out.print("c>a>b");

}

}else{if (b >c) {if (a >c) {

System.out.print("b>a>c");

}else{

System.out.print("b>c>a");

}

}else{

System.out.print("c > b > a");

}

}

}

}

10.水仙花数:用程序找出每位数的立方和等于该数本身值的所有的3位数

import java.util.Scanner;

//用程序找出每位数的立方和等于该数本身值的所有的3位数

public class Ten {

public static void main(String[] args) {

// TODO 自动生成的方法存根

Scanner inScanner = new Scanner(System.in);

int number, number_cal, a, b, c;

// 先 确定该数的位数,可代码实现,为了方便可手动从控制台输入

System.out.print("输入一个三位数数字:");

number_cal = number = inScanner.nextInt();

a = number_cal / 100;

number_cal = number_cal - a * 100;

b = number_cal / 10;

c = number_cal - b * 10;

if ((a * a * a + b * b * b + c * c * c) == number) {

System.out.print(number + "是水仙花数");

} else {

System.out.print(number + "不是水仙花数");

}

}

}

11.计算1累加到n(n>=2的整数)的和

import java.util.Scanner;

//计算1 加到n ( n>=2的整数)的总和

public class Eleven {

public static void main(String[] args) {

// TODO 自动生成的方法存根

int number, sum = 0;

Scanner in = new Scanner(System.in);

System.out.print("输入一个>=2的整数:");

number = in.nextInt();

for (int i = 1; i <= number; i++) {

sum = sum + i;

}

System.out.print("结果是" + sum);

}

}

12.得到一个整数的绝对值

import java.util.Scanner;

//得到一个整数的绝对值

public class Twelve {

public static void main(String[] args) {

// TODO 自动生成的方法存根

int number;

Scanner inScanner = new Scanner(System.in);

System.out.print("输入一个整数:");

number = inScanner.nextInt();

if (number < 0) {

number = 0 - number;

}

System.out.print("绝对值是:" + number);

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐