题目1: 统计正数和负数的个数然后计算这些数的平均值
import java.util.Scanner;
class Home13{
/*
数据:输入的数字、正、负数的个数、平均值
步骤:1、提示用户输入一串数字,以0为结束
2、用while循环读取输入数字,并进行判断,若为0则跳出循环,终止输入
3、同时判断输入数字的正负,并统计个数
4,求出输入数字之和,计算平均值
5、显示输出结果
*/
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer,the input ends if it is 0: ");
int num=1;
int pos=0;
int neg=0;
int count=-1;
double total=0;
double average=0;
while(num!=0){
num = scanner.nextInt();
if(num>0){
pos++;
}else if(num<0){
neg++;
}
++count;
total+=num;
average = total/count;
}
System.out.println("The number of the positives is "+pos);
System.out.println("The number of the negatives is "+neg);
System.out.println("The total is "+total);
System.out.println("The average is "+average);
}
}
题目2:
import java.lang.Math;
class Home14{
/*
数据:变量n
步骤:1、定义循环变量n
2、因为不知道循环的次数所有用while循环
3、写循环结束条件,显示输出结果
*/
public static void main(String[] args){
int n = 1;
while(Math.pow(n,2)<=12000){
n++;
}
System.out.println("n为:"+n);
}
}
题目三:显示闰年
class Home17{
/*
闰年通常的判断方法是:
A: 可以被4整除的,但不能被100整除
B: 可以被400整除
只要满足AB任意一种情况就是闰年。
数据:年份数
步骤:1、遍历101~2100的数
2、根据闰年的判断方法,找到符合闰年的数
3、符合一个,统计一个,
*/
public static void main(String[] args){
int count = 0;
for(int i =101;i<=2100;i++){
if(i%4==0&&i%100!=0||i%400==0){
count++;
if(count%10!=0){
System.out.print(i+"\t");
}else{
System.out.print(i+"\n");
}
}
}
System.out.println("\n"+"101~2100期间共有:"+count+"个闰年");
}
}
题目四:用循环语句打印四个图案
图案1:
class Home15_1{
public static void main(String[] args){
for(int i = 1;i<7;i++){ //行数循环,循环一次增加一行
for(int j = 1;j<=i;j++){ //行内循环,打印的最后一个数字就是所在行数
System.out.print(j+" ");
}
System.out.println();
}
}
}
图案2:
class Home15_2{
public static void main(String[] args){
for(int i = 1;i<7;i++){ //行数循环,循环一次增加一行
for(int j = 1;j<8-i;j++){ //行内循环,打印的数的个数为总行数+1减当前行
System.out.print(j+" ");
}
System.out.println();
}
}
}
图案3:
class Home15_3{
public static void main(String[] args){
for(int i = 1;i<7;i++){ //行数的循环
for(int j = 1;j<7-i;j++){ //打印行前空格,循环方式为图案2的子循环
System.out.print(" ");
}
for(int k = i;k>0;k--){ //打印数字
System.out.print(k+" ");
}
System.out.println();
}
}
}
图案4:
class Home15_4{
public static void main(String[] args){
for(int i = 1;i<7;i++){ //循环行数
for(int j = 1;j<i;j++){ //打印行前空格,循环方式为图案1的子循环
System.out.print(" ");
}
for(int k = 1;k<8-i;k++){ // 打印数字,循环方式为图案2的子循环
System.out.print(k+" ");
}
System.out.println();
}
}
}
题目5:打印金字塔形状的数字
import java.lang.Math;
class Home16{
/*
数据:定义的变量
步骤思路:1、以行数写一个for循环,其余循环都在这个循环中进行
2、然后将数字金字塔分为三部分
1~8行前的空格为一部分,一个for循环
前八列为一个循环
后七列为一个循环
3、三个子循环的初值和循环结束条件都与行数循环相关
(输出时中间空格时用/t使数字排列整齐)
*/
public static void main(String[] args){
for(int i = 1;i<9;i++){
for(int j = 1;j<9-i;j++){
System.out.print("\t");
}
//数字部分从中间分为左右两部分,从左到右左边递增,右边递减,分别循环实现
for(int k = 1;k<Math.pow(2,i);k*=2){
System.out.print(k+"\t");
}
for(int h = (int)Math.pow(2,i-2);h>=1;h/=2){
System.out.print(h+"\t");
}
System.out.println();
}
}
}
题目6:最大数的出现次数
import java.util.Scanner;
class Home18{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in); //Scanner类新建一个对象
System.out.println("Enter number: ");
int num = 1;
int max = 0;
int count = 0;
do{
num = scanner.nextInt(); //从控制台读取输入
if(max==num){ //
count++;
}
if(max<num){ //最大值对比新输入的值,取两者最大值并给最大值重新赋值
max=num;
count = 1;
}
}
while(num!=0); //循环终止条件,输入为0时结束循环
System.out.println("The largest number is "+max);
System.out.println("The occurrence count of the largest number is "+count);
}
}
实现的另一种方法:
import java.util.Scanner;
import java.util.ArrayList; //导入包
public class Home18{
private static Scanner input; //私有静态类引用input
public static void main(String[] args){
ArrayList<Integer> al=new ArrayList<Integer>(); //创建一个数组对象al
int max=0,number,count=0;
input=new Scanner(System.in); //Scanner类新建一个对象,将对象赋给类的引用.效果同上面的数组对象创建
System.out.println("input the number:");
do{
number=input.nextInt(); //input对象调用Scanner类的方法nextInt(接受一个整型值),并赋值给number
al.add(number); //al对象调用ArrayList类的add方法,即添加一个元素到数组.
if(number>max)
max=number; //找出最大值
}
while(number!=0);
for(int i=0;i<al.size();i++){ //al对象调用ArrayList类的size方法,即求数组大小.
if(al.get(i)==max){ //al对象调用ArrayList类的get方法,即从数组中获取一个元素.与max比较
count++; //累计出现次数
}
}
System.out.println("max="+max+" "+"次数:"+count);//输出运行结果
}
}