@Hubertoo
2017-07-17T04:56:08.000000Z
字数 6195
阅读 625
达内
if(){}if(){} else{}if(){} else if(){} else{}switch-case / break defaultJDK7.0之后支持字符串操作while(){}do{} while();for( ; ; ){}
package day01;import java.util.Scanner;/*** 在原有基础上加入满500打八折,并保证收取金额不足时提醒功能* @author zeyan**/public class Cashier2 {public static void main(String[] args) {Scanner console = new Scanner(System.in);//console 控制台System.out.println("请输入单价(¥):");int unitPrice = console.nextInt();System.out.println("请输入数量:");double amount = console.nextDouble();System.out.println("请输入收取金额(¥):");double money = console.nextDouble();console.close();//计算商品总价double totalPrice = 0.0;totalPrice = unitPrice * amount;//判断余额是否可以打折if(money < totalPrice){System.out.println("收取金额不足:");}else if(totalPrice >= 500){totalPrice *= 0.8;//计算找零double change = money - totalPrice;System.out.println("商品总价:" + totalPrice + ",应找零为:" + change);}else{//计算找零double change = money - totalPrice;System.out.println("商品总价:" + totalPrice + ",应找零为:" + change);}}}
package day01;import java.util.Scanner;//使用分支结构完成学生成绩划分:优,良,中,差public class LevelOfScore {public static void main(String[] args) {Scanner scan = new Scanner(System.in);System.out.println("请输入学生分数(0-100):");double score = scan.nextDouble();//用if-else-if完成成绩等级划分if(score < 0 || score > 100){System.out.println("输入成绩有误");}else if(score >= 90){System.out.println("优秀");}else if(score >= 80){System.out.println("良好");}else if(score > 60){System.out.println("中等");}else{System.out.println("差,要加油哦");}}}
package day01;import java.util.Scanner;public class CommendPro {public static void main(String[] args) {Scanner scan = new Scanner(System.in);System.out.println("请选择功能:1.显示所有记录 ,2.查询登录记录,0.退出");//功能实现int command = -1;command = scan.nextInt();scan.close();/* Scanner 类实例化的时候需要一个InputStream流作为参数,Scanner的close就是关闭InputStream流的。* 输入流,不关闭,只会占用资源。输出流,不关闭,可能会造成最后一部分数据丢失。*/switch(command){case 1:System.out.println("显示全部记录");break;case 2:System.out.println("查询登录记录");break;case 0:System.out.println("退出");break;default:System.out.println("输出错误");}}}
内置1~100的数字做为猜测结果,用户猜测一次数字,然后系统大小
package day01;import java.util.Scanner;//实现的循环方式有很多种public class GuessNum {public static void main(String[] args) {Scanner scan = new Scanner(System.in);//随机生成范围数字int num = (int)(Math.random() * 100 + 1);System.out.println(num);//保存用户猜测次数,用于计分int count = 0;// while(true){// if(guessNum > num){// System.out.println("猜测数字大了");// count ++;// }else if(guessNum < num){// System.out.println("猜测数字小了");// count ++;// }else{// System.out.println("恭喜你 ,猜对了");// break;// }// }for(int i=0; i<10; i++){System.out.println("请输入你猜测的整数(1 ~ 100,输入-1结束):");int guessNum = scan.nextInt();//用于判断if(guessNum == -1){System.out.println("下次再来");System.out.println("你猜测次数是:" + count);break;}else if(guessNum < num){System.out.println("猜测数字小了");count ++;}else if(guessNum > num){System.out.println("猜测数字大了");count ++;}else{System.out.println("恭喜你 ,猜对了");int grade = 100 - count * 10;System.out.println("你的到分数是:" + grade);break;}}}}
package day01;import java.util.Scanner;public class RandomAddtion {public static void main(String[] args) {Scanner scan = new Scanner(System.in);//用来存放两个随机整数int[] arr = new int[2];//用来显示第几道题目,同时用来得出分数int count = 0;for(int i=1; i<=10; i++){arr[0] = (int)(Math.random() * 100 + 1);arr[1] = (int)(Math.random() * 100 + 1);//出题System.out.println("第(" + i + ")题目是:"+ arr[0] + " + "+ arr[1] + " = ? " +",共10道题,输入-1结束");int num = arr[0] + arr[1];System.out.println("请输入你的计算结果");int result = scan.nextInt();//用来判断结果if(result == num){count ++;}else if(result == -1){break;}}System.out.println("你的得分是:" + count*10);}}
package day01;import java.util.Random;public class MaxOfThree {public static void main(String[] args) {Random rand = new Random();//int数组存放三个比较的数int[] arr = new int[3];//随机生成三个数,并赋值for(int i=0; i<arr.length; i++){arr[i] = rand.nextInt(100);System.out.print(arr[i] + ",");}//找出最大数//开始把下面的i写成了1,导致测试出错for(int i=1; i<arr.length; i++){if(arr[0] < arr[i]){arr[0] = arr[i];// int t = -1;// t = arr[0];// arr[0] = arr[i];// arr[i] =t;}}System.out.println("最大值是:" + arr[0]);}}
package day01;import java.util.Random;public class SortOfThree {public static void main(String[] args) {Random rand = new Random();//int数组存放三个比较的数int[] arr = new int[3];//随机生成三个数,并赋值for(int i=0; i<arr.length; i++){arr[i] = rand.nextInt(100);System.out.print(arr[i] + ",");}//普通比较排序,开始本来使用冒泡排序,没有写成功if(arr[0] > arr[1]){int t = arr[0];arr[0] = arr[1];arr[1] = t;}if(arr[0] > arr[2]){int t = arr[0];arr[0] = arr[2];arr[2] = t;}if(arr[1] > arr[2]){int t = arr[1];arr[1] = arr[2];arr[2] = t;}//遍历数字并输出for(int i : arr){System.out.println();System.out.println(i);}}}
package day01;import java.util.Scanner;public class FindDays {public static void main(String[] args) {Scanner scan = new Scanner(System.in);System.out.println("请输入年份(如:2001):");int year = scan.nextInt();System.out.println("请输入月份(如:11):");int month = scan.nextInt();switch(month){case 1:System.out.println(year + "年," + month + "月有31天");break;case 3:System.out.println(year + "年," + month + "月有31天");break;case 5:System.out.println(year + "年," + month + "月有31天");break;case 7:System.out.println(year + "年," + month + "月有31天");break;case 8:System.out.println(year + "年," + month + "月有31天");break;case 10:System.out.println(year + "年," + month + "月有31天");break;case 12:System.out.println(year + "年," + month + "月有31天");break;case 4:System.out.println(year + "年," + month + "月有30天");break;case 6:System.out.println(year + "年," + month + "月有30天");break;case 9:System.out.println(year + "年," + month + "月有30天");break;case 11:System.out.println(year + "年," + month + "月有30天");break;case 2:if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){System.out.println(year + "年," + month + "月有28天");}else{System.out.println(year + "年," + month + "月有29天");}}}}
package day01;public class Test {public static void main(String[] args) {for(int i=0,j=0,k=0; i < 2 && j< 3 && k <4; i++,j++,k++){System.out.println(i);System.out.println(j);System.out.println(k);}}}//结果是0 0 0 1 1 1,是可以这样写的
package day01;//最后结果可能出错出错public class Addtion999 {public static void main(String[] args) {//使用int超出范围double sum = 0;int count = 1;int num = 0;for(int i=0; i<=10; i++){count *= 10;num = count -1;sum += num;}System.out.println(sum);}}