找出三个数中的最大数和最小数
public class MaxNum {
public static void main(String[] args)
{
int num1 = 13 * 15;
int num2 = 21 * 7;
int num3 = 14 *17;
int max ,min;
System.out.print("这三个数是:"+ num1 + " " + num2 + " " + num3);
max = num1 >= num2 ? num1:num2;
max = max >= num3 ? max:num3;
min = num1 <= num2 ? num1:num2;
min = min <= num3 ? min:num3;
System.out.print("最大值是:" + max);
System.out.print("最小值是:" + min);
}
}
求1 + 2 + ... + 100的值
public class Sum
{
public static void main(String[] args)
{
//方法1 for循环
int sum = 0,i = 1;
for(i = 1;i <= 100;i++)
sum += i;
System.out.print("1 + 2 + ... +100 =" + sum);
//方法2 while循环
sum = 0;i = 1;
while(i <= 100)
{
sum += i;
i += 1;
}
System.out.print("1 + 2 + ... +100 =" + sum);
//方法3 do...while循环
sum = 0;i = 1;
do
{
sum += i;
i += 1;
}
while(i <= 100);
System.out.print("1 + 2 + ... +100 =" + sum);
}
}
求100以内的素数
public class Primes
{
public static void main(String[] args)
{
final int MAX = 100;
int i,k;
boolean yes;
for(k = 2;k < MAX;k++)
{
yes = true;
i = 2;
while(i <= k-1 && yes)
{
if (k % i == 0)
yes = false;
i++;
}
if (yes)
System.out.print(k + " ");
}
}
}
计算输出1!,2!... 5!以及它们的和
public class Factorials {
public static void main(String[] args)
{
int i,j;
long s = 0,k;
for(i = 1;i <= 5;i++)
{
k = 1;
for(j = 1;j <= i;j++)
{
k = k * i;
}
System.out.print(i + "!=" +k);
s = s + k;
}
System.out.print("Total sum = " + s);
}
}
编写程序,实现10的阶乘
public class T10 {
public static void main(String[] args)
{
int sum = 1;
for(int i = 1;i <= 10;i++)
sum *= i;
System.out.print("10的阶乘为" + sum);
}
}
使用冒泡排序法对数组的元素排序
public class BubbleSort {
public static void main(String[] args)
{
int i,j;
int intArray[] = {135,-65,70,35,97};
int len = intArray.length;
//输出数组排序前的排序
for(i = 0;i < len;i++)
System.out.print(intArray[i] + " ");
System.out.print(" ");
//排序
for(i = 0;i < len - 1;i++)
for(j = i + 1;j < len;j++)
if(intArray[i]>intArray[j])
{
int t = intArray[i];
intArray[i] = intArray[j];
intArray[j] = t;
}
//输出排序结果
for(i = 0;i < len;i++)
System.out.print(intArray[i] + " ");
}
}
使用数组,编写一个可以输入30个学生数学成绩,并可以统计显示他们的总分,平均分,最低分,最高分的程序。
public class pratice3 {
public static void main(String[] args){
int[] scores = new int[3];
double sum=0;
double avg=0;
Scanner input = new Scanner(System.in);
System.out.println("请输入30个同学的成绩:");
for(int i=0;i<scores.length;i++){
System.out.print("这是第"+(i+1)+"个学生的成绩:");
scores[i]=input.nextInt();//获取输入的成绩
}
int max=scores[0];
int min=scores[0];
for(int j=0;j<scores.length;j++){
if(scores[j]>max){
max=scores[j];
}
if(scores[j]<min){
min=scores[j];
}
sum+=scores[j];
}
avg=sum/3;
System.out.println("该班的总成绩:"+sum);
System.out.println("该班的平均成绩:"+avg);
System.out.println("该班的最低分:"+min);
System.out.println("该班的最高分:"+max);
}
}