Java学习笔记
源代码(一)
Scanner
package shangxt;
/*
* 测试获得键盘输入,Scanner类
* */
//import java.sql.SQLOutput;
import java.util.Scanner;
public class TestScanner {
public static void main(String[] arg){
Scanner s = new Scanner(System.in);
//要求输入姓名,年龄,薪水
System.out.println("请输入您的用户名:");
String name = s.nextLine();
System.out.println("用户名:"+name);
System.out.println("请输入您的年龄:");
int age = s.nextInt();
System.out.println("年龄:"+age);
System.out.println("请输入您的薪水:");
double mouthsalary = s.nextDouble();
System.out.println("薪水"+mouthsalary);
}
}
if
测试if语句,掷骰子游戏
package shangxt;
/*
测试if语句,掷骰子游戏
*/
public class Test_If{
public static void main(String[] arg) {
double a = Math.random();//通过Math.random()产生随机数
System.out.println(a);
double b = 6 * Math.random();//产生大于等于0小于6的随机数
System.out.println(b);
double c = 6 * Math.random() + 1;//产生1-7之间的随机数,不包含7
System.out.println(c);
System.out.println("测试结束,游戏开始");
//开始掷骰子,3次
int i = (int) (6 * Math.random()) + 1;//double类型强制转int类型
System.out.println("第一次得分:"+i);
int j = (int) (6 * Math.random()) + 1;
System.out.println("第二次得分:"+j);
int k = (int) (6 * Math.random()) + 1;
System.out.println("第三次得分:"+k);
int sum = i + j + k;
//和大于15说明运气好
if (sum > 15) {
System.out.println("骰子和为:" + sum + "\t运气好,真不错");
}
if(sum<=15&&sum>9) {
System.out.println("骰子和为:" + sum + "\t运气一般般");
}
if(sum<=9) {
System.out.println("骰子和为:" + sum + "\t运气差,不得行");
}
}
}
随机产生一个[0.0,4.0)区间的半径,并根据半径求圆的面积和周长;
package shangxt;
/*
随机产生一个[0.0,4.0)区间的半径,并根据半径求圆的面积和周长;
*/
public class Test_if02 {
public static void main(String[] argd){
int a = (int)(4*Math.random());
System.out.println("随机选择半径为:"+a);
int mj = (int)(a*a*Math.PI);
int zc =(int) (2*a*Math.PI);
System.out.println("圆的面积为:"+mj+"圆的周长为"+zc);
if(mj>zc) {
System.out.println("面积大于周长");
}
if(mj<=zc) {
System.out.println("面积小于周长");
}
}
}
多分支结构,if_else if_else
package shangxt;
/*多分支结构,if_else if_else*/
public class Test_if03 {
public static void main(String[] args){
int age = (int)(100*Math.random());
System.out.print("您的心理年龄是:"+age+"\n属于:");
if(age<10)
System.out.println("小孩子一个!");
else if (age<20)
System.out.println("青少年!");
else if(age<30)
System.out.println("成年人了!");
else if(age<50)
System.out.println("中年人了!");
else if(age<70)
System.out.println("老年人了!");
else
System.out.println("古来稀!");
}
}
switch
switch测试,春夏秋冬季节测试
package shangxt;
/*
switch测试,春夏秋冬季节测试
*/
public class Test_switch {
public static void main(String[] args){
System.out.println("if形式输出。"); //if
int mouth = (int)(12*Math.random()+1);
System.out.println("现在是"+mouth+"月");
if(mouth==1|| mouth==2|| mouth==3){
System.out.println("现在是春季。");
}
else if(mouth==4||mouth==5||mouth==6){
System.out.println("现在是夏季。");
}
else if(mouth==7||mouth==8||mouth==9){
System.out.println("现在是秋季。");
}
else{
System.out.println("现在是冬季。");
}
System.out.println("switch形式输出");//switch
switch (mouth) {
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;
}
}
}
while
从1加到100的和
package shangxt;
/*
测试循环while
*/
public class Test_while {
public static void main(String[] arys){
/*
从1加到100的和
*/
int i = 0;
int sum = 0;
while (i<=100) {
sum = sum+i;
i++;
System.out.print("\t"+i);
}
System.out.println("");
System.out.println("\t"+sum);
}
}
=薪水计算器=
package shangxt;
/*
=====薪水计算器=====
*/
import java.util.Scanner;
public class SalaryCalculator {
public static void main(String[] args){
Scanner scanner =new Scanner(System.in);
System.out.println("=====薪水计算器=====");
while(true) {
System.out.println("请输入您的月薪:");
double m_s = scanner.nextDouble();
System.out.println("请输入您一年工作几个月:");
double y = scanner.nextDouble();
double y_s = y * m_s;
System.out.println("您的年薪为:" + y_s + "元");
System.out.println("输入exit表示结束或输入next表示重新输入薪水,输入任意字符表示继续判断!");
scanner.nextLine();
String commend = scanner.nextLine();
//System.out.println(commend+"=======");
/*当我们输入数字后,是不是按下了回车键, 这个时候,nextInt()从缓冲区把我们输入的数字读走了,但留下了最后的换行符,等运行到nextLine()的时候,它开始读缓冲区里的内容,然而好巧不巧的是, 刚好缓冲区里留有一个换行符,相当于还没给我们机会去输入东西,就按回车结束了。*/
if("exit".equals(commend)){
System.out.println("over!");
break;
}
if ("next".equals(commend)){
System.out.println("again!");
continue;
}
if (y_s < 100000) {
System.out.println("你啥也不是!");
}
if (y_s >= 100000 && y_s < 200000) {
System.out.println("恭喜你,超过了90%的国人。");
}
if (y_s >= 200000) {
System.out.println("恭喜你,超过了98%的国人。");
}
}
}
}
do_while
package shangxt;
public class Test_dowhile {
public static void main(String[] args){
/*
while:先判断再循环!!
*/
int i = 0;
while (i<0){
// System.out.println(i);
i++;
}
System.out.println("***");
/*
do_while:先循环再判断!!
*/
System.out.println("开始do_while");
do {
System.out.println(i);
i++;
}while (i<0);
}
}
for
1-100和,其中奇数的和,偶数的和
package shangxt;
/*
测试for循环
*/
public class Test_for {
//1-100和,其中奇数的和,偶数的和
public static void main(String[] args){
int i =1;
int sum =0;
int ossum = 0;
int jssum = 0;
/* while(i<=100){ //while方法
if(i%2==0){
ossum = ossum+i;
}
else {
jssum = jssum+i;
}
sum =sum+i;
i++;
// System.out.println(sum);
}
System.out.println("总和为:"+sum);
System.out.println("偶数总和为:"+ossum);
System.out.println("奇数总和为:"+jssum);*/
System.out.println("以下是for方法");//for方法
for(i=1;i<=100;i++){
if(i%2==0){
ossum = ossum+i;
}
else {
jssum = jssum+i;
}
sum = sum+i;
//System.out.println(sum);
}
System.out.println("总和为:"+sum);
System.out.println("偶数总和为:"+ossum);
System.out.println("奇数总和为:"+jssum);
}
}
测试嵌套循环
- 数字方阵
- 九九乘法表
- 奇偶方阵
package shangxt;
/*
测试嵌套循环
*/
import java.util.Scanner;
public class qiantao_xh {
public static void main(String[] args){
/*
数字方阵
*/
Scanner scanner = new Scanner(System.in);
int s =scanner.nextInt();
//for (int i = 1; i <=s; i++) {
// for (int j = 1; j <= s; j++) {
// System.out.print(i + "\t");
// }
// System.out.println("");
// }
/*
九九乘法表
*/
//1*4=4 2*4=8 3*4=12 4*4=16
/* for(int j= 0;j<=9;j++) { //把4循环起来
for (int i = 0; i <= j; i++) {
System.out.print(i + "*" + j + "=" + (i * j) + "\t");
}
System.out.println("");
}*/
/*
奇偶方阵
*/
for (int i = 0;i<s;i++){
for(int j = 0;j<s;j++){
if ((i+j)%2==0){
System.out.print("喻言\t");
}
else{
System.out.print("宝宝\t");
}
}
System.out.println("");
}
}
}
break_continue
package shangxt;
/*
break测试,直接跳出循环
*/
import java.util.Scanner;
public class break_continue {
public static void main(String[] args){
/* Scanner scanner =new Scanner(System.in); //输入一个数,摇到塔就停止
int s = scanner.nextInt();
int total = 0;
while (true) {
int sz = (int) Math.round(10 * Math.random()); //Math.round作用为四舍五入
if (sz == s) {
break;
}
else {
total++; //计数
System.out.print(sz + "\t");
}
}
System.out.println("一共摇了"+total+"次");*/
/*
找出426-526之间不能被4整除的数,并且把她五个为一行的打印出来。 测试continue
*/
int total = 0;
for(int i = 426;i<=526;i++){
if(i%4==0){
continue;
}
else {
total++;
// for (int j=0;j<5;j++ ) {
System.out.print(i+"\t");
// }
}
if(total%5==0){
System.out.println("\t五个啦");
}
}
System.out.println("\n这些数一共有"+total+"个");
}
}
goto_continue
输出1-100之间的质数(质数:只能被1和它本身整除)
package shangxt;
/*
带标签的break 和continue
*/
//输出1-100之间的质数(质数:只能被1和它本身整除)
public class goto_continue {
public static void main(String [] args){
outer:for(int i =1;i<=100;i++){ //outer:为continue的标签
for(int j = 2;j<i/2;j++){
if(i%j==0){
continue outer;
}
//else {
//System.out.print(i+"\t");
//}
}
System.out.print(i+"\t");
}
}
}