赞
踩
public class Test04 { public static void main(String[] args) { //1、 // int i =1; // //while循环100遍 // while (i<=100){ // System.out.println("第"+i+"做100道编程题"); // i++; // } // //2、 //do while循环 100遍 int i=1; do { System.out.println("第"+i+"做100遍编程题"); i++; }while (i<=100); //3、 //for 循环 100遍 // for (int i =1;i<=100;i++){ // System.out.println("第"+i+"做100道编程题"); // } } }
//奇数 class OddNumber{ //判断100以内的偶数和奇数 public static void main(String[] args) { // int i =0; // while (i<=100){ // // if (i!=0&&i%2==0){ // System.out.println("偶数为:"+i); // }else if (i%2!=0){ // System.out.println("奇数:"+i); // }else { // System.out.println(i+"既不是偶数也不是奇数"); // } // i++; // } // //do while 找出100里面偶数和奇数 // int i=0; // do { // if (i!=0&&i%2==0){ // System.out.println("偶数为:"+i); // }else if (i%2!=0){ // System.out.println("奇数:"+i); // }else { // System.out.println(i+"既不是偶数也不是奇数"); // } // i++; // // }while (i<=100); // //用for循环 找出100以内的偶数和奇数 for (int i =0;i<=100;i++){ if (i!=0&&i%2==0){ System.out.println("偶数为:"+i); }else if (i%2!=0){ System.out.println("奇数:"+i); }else { System.out.println(i+"既不是偶数也不是奇数"); } }
//偶数和 class EvenSum{ public static void main(String[] args) { // //while 循环100内偶数 // int i =0; // int sum=0; // while (i<=100){ // if (i%2==0){ // sum+=i; // } // i++; // } // System.out.println("偶数和为:"+sum); // // //do while 循环100以内偶数和 // int i=0; // int sum=0; // do { // if (i%2==0){ // sum+=i; // } // i++; // // }while (i<=100); // System.out.println("偶数和为:"+sum); //for 循环 100以内的偶数和 int sum=0; for (int i =0;i<=100;i++) { if (i % 2 == 0) { sum += i; } } System.out.println("100以内的偶数和为:"+sum); } }
public class Time { //知道几年几月和几天 算一年总天数 public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输入year:"); int year = input.nextInt(); System.out.println("请输入month:"); int month = input.nextInt(); System.out.println("请输入day:"); int day =input.nextInt(); int sumDays =0; switch (month){ case 12: sumDays+=30; case 11: sumDays+=31; case 10: sumDays+=30; case 9: sumDays+=31; case 8: sumDays+=31; case 7: sumDays+=30; case 6: sumDays+=31; case 5: sumDays+=30; case 4: sumDays+=31; case 3: if ((year%4==0 && year%100 !=0) || year%400==0){ //判断是否是闰年 //闰年 sumDays+=29; }else{ //平年 sumDays+=28; } case 2: sumDays+=31; case 1: sumDays+=day; break; default: System.out.println("请输入正确的时间观:"); break; } if (month>=1 && month<=12) { System.out.println(year + "年" + month + "月" + day + "日是当年的第" + sumDays + "天"); } } }
class Quarter{ public static void main(String[] args) { System.out.println("请输入月份:"); Scanner input = new Scanner(System.in); int month = input.nextInt(); switch (month){ case 1: case 2: case 3: System.out.println("该季节为春季!"); break; case 4: case 5: case 6: System.out.println("该季节为夏季!"); break; case 7: case 8: case 9: System.out.println("该季节为秋季!"); break; case 10: case 11: case 12: System.out.println("该季节为冬季!"); break; } } }
提示:输入操作数1,输入操作数2,输入操作符,然后根据输入的数据和操作类型进行判断及计算
import java.util.Scanner; //计算器功能:+、-、*、/ public class Calculator { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输第一个操作数:"); int number1 =input.nextInt(); System.out.println("请输入第二个操作数:"); int number2 =input.nextInt(); System.out.println("请输入操作符:"); char operator =input.next().charAt(0); switch (operator){ case '+': System.out.println("number1"+"+"+"number2"+"="+(number1+number2)); break; case '-': System.out.println("number1"+"-"+"number2"+"="+(number1-number2)); break; case '*': System.out.println("number1"+"*"+"number2"+"="+(number1*number2)); break; case '/': System.out.println("number1"+"/"+"number2"+"="+(number1/number2)); break; default: if (number1!=operator && number2!=operator) System.out.println("请输入正确的格式:"); } } }
public class Output {
public static void main(String[] args) {
//循环输出1~100
for (int i =1;i <=100;i++){
System.out.println(i);
}
}
}
25=10
410=40
6*15=90
……
? * 100 = ?
上述 ‘?’ 需要通过循环得出具体值
class Result{
public static void main(String[] args) {
int i =2;
int j =5;
int sum =0;
do {
i+=2;j+=5;
System.out.println(i+"*"+j+"="+sum);
}while (j<100);
}
}
//循环累加,直到出现0、 class Accumulation { public static void main(String[] args) { int sum = 0; do { Scanner input = new Scanner(System.in); int number1 = input.nextInt(); if (number1 != 0) { sum += number1; System.out.println("相加和为:" + sum); } else if (number1 == 0) { break; } }while (true) ; } }
提示:
1.java产生随机数的方法 Math.random() 可以产生 0 ~ 1 之间的随机数,包括0,但不包括1;
2.控制台输入需要使用:
Scanner sc = new Scanner(System.in); 扫描工具
调用 sc.nextInt(); 可以得到用户输入的整数
调用 sc.next(); 可以得到用户输入的字符串
import java.util.Scanner; //猜拳小游戏 public class GuessingGame { public static void main(String[] args) { //随机数 int computer = (int) (Math.random() * 4); System.out.println("请输入你的: (1.石头、2.剪刀、3.布)"); Scanner input = new Scanner(System.in); int man = input.nextInt(); if (man == 1) { switch (computer) { case 1: System.out.println("石头 VS 石头, 平局!"); break; case 2: System.out.println("石头 VS 剪刀,电脑输! "); break; case 3: System.out.println("石头 VS 布,电脑赢!"); break; default: System.out.println("输入错误,请重新输入:"); } } else if (man == 2) { switch (computer) { case 1: System.out.println("剪刀 VS 石头, 电脑赢!"); break; case 2: System.out.println("剪刀 VS 剪刀,平局! "); break; case 3: System.out.println("剪刀 VS 布,电脑输!"); break; default: System.out.println("输入错误,请重新输入:"); } } else if (man == 3) { switch (computer) { case 1: System.out.println("布 VS 石头, 电脑输!"); break; case 2: System.out.println("布 VS 剪刀,电脑赢! "); break; case 3: System.out.println("布 VS 布,平局!"); break; default: System.out.println("输入错误,请重新输入:"); } } else { System.out.println("输入错误,请重新输入"); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。