标签:语句 JAVA Scanner int day02 System println public out
程序逻辑结构
大致可以分为:
1.顺序结构:
顺序执行,根据编写的顺序,从上到下运行
2.条件分支结构:
- 判断语句:1. if 2.if...else 3.if...else if...else
- 选择语句:1.switch
if
代码说明
import java.util.Scanner;
//找出公约数是5的数,再找出公约数是2的数
//当x和y大于2时,求和
public class Deom01 {
public static void main(String[] args) {
Scanner num1 = new Scanner(System.in);
System.out.println("请输入第一个数x");
int x = num1.nextInt();
Scanner num2 = new Scanner(System.in);
System.out.println("请输入第二个数y");
int y = num2.nextInt();
if(x%5 == 0) {
System.out.println("x是5的公约数");
}
if(y%2 == 0) {
System.out.println("y是2的公约数");
}
if(x > 2 && y > 2) {
int sum = x + y;
System.out.println("sum = " + sum);
}
}
}
if...else
代码说明
import java.util.Scanner;
//判断年份是不是闰年
//四闰百不闰 四百闰
public class Deom02 {
public static void main(String[] args) {
Scanner year = new Scanner(System.in);
System.out.println("请输入一个年份x");
int x = year.nextInt();
if(0 == x % 4 && 0 != x % 100 || 0 == x % 400) {
System.out.println("x是闰年哦");
}else {
System.out.println("x不是闰年哦");
}
}
}
if...else if...else
代码说明
/*
* 需求:用户自己输入:商品单价50和商品数量12需求:收银台收款程序
输入:实付金额600
如果:应付金额>500,打8折
输出:找零金额120
升级版:判断实付金额够不够
*/
import java.util.Scanner;
public class Deom03 {
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
System.out.println("请输入商品数量n");
int n = num.nextInt();
Scanner money = new Scanner(System.in);
System.out.println("请输入实付金额m");
int m = money.nextInt();
if (m < n*50) {
System.out.println("您的实付金额不够哦");
}else if(n*50 > 500 ) {
double n1 = (n * 50) * 0.8;
System.out.println("打8折优惠哦 您需要付款 " + n1);
double n2 = m - n1;
System.out.println("找您 " + n2);
}
}
}
switch
switch(条件)只适用于判断值是否相等,这里面需要判断的条件可以是byte、short、int、char、String类型的条件。
case具有穿透性,在switch语句中,如果case后面不写break,就会出现穿透现象,也就是说不会判断下一个case的值,直接往后运行了,直到又遇见break或者switch整体结束。
代码说明
//输出几天后是星期几
import java.util.Scanner;
public class Deom04 {
public static void main(String[] args) {
Scanner day = new Scanner(System.in);
System.out.println("请输入天数d");
int d = day.nextInt();
int w = d % 7;
switch(w) {
case 1:
System.out.println("今天是周一");
break;
case 2:
System.out.println("今天是周二");
break;
case 3:
System.out.println("今天是周三");
break;
case 4:
System.out.println("今天是周四");
break;
case 5:
System.out.println("今天是周五");
break;
case 6:
System.out.println("今天是周六");
break;
case 0:
System.out.println("今天是周日");
break;
default:
System.out.println("输入的数字有误");
break;
}
}
}
3.循环结构:1.for 2.while 3.do...while
for
使用for循环时,他应该是知道规律且知道循环次数才会使用for循环的
代码说明
//打印九九乘法表
/*
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
*/
public class Deom06 {
public static void main(String[] args) {
for (int x = 1;x < 10;x++) {
for(int y = 1;y <= x; y++) {
System.out.print(y + "*"+ x + "=" + x*y +"\t");
}
System.out.println();
}
}
}
//这里涉及到嵌套循环
import java.util.Scanner;
/*有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,
假如兔子都不死,问每个月的兔子总数为多少?
*1: 1
*2: 1
*3: 2
*4: 3
*5: 5dui
*/
public class Test01 {
public static void main(String[] args) {
int m1 = 1 ;
int m2 = 1;
int sum ;
for (int m3 = 3; m3 <= 12 ;m3++) {
/*
* 第一个月:1对
* 第二个月:1对
* 第三个月:2对
* 第四个月:3对
* 第五个月:5对
* m1 = 1
* m2 = 1
* m3 = 1+1 ->m2+m1
* m4 = 2+1 ->m3+m2
* m5 = 3+2 ->m4+m3
* */
sum = m1 + m2 ;
m1 = m2;
m2 = sum;
System.out.println("兔子现在有 " + sum + "对");
}
}
}
while
当不知道规律,不知道需要循环多少次时使用while循环
代码说明
/*
* 需求:
有猜数字游戏,其游戏规则为:程序内置一个 1 到 1000 之间的数字作为猜测的结果,
由用户猜测此数字。
用户每猜测一次,由系统提示猜测结果:大了、小了或者猜对了;
直到用户猜对结果,则提示游戏结束。
用户可以提前退出游戏,即,游戏过程中,如果用户录入数字0,则游戏终止。*/
import java.util.Scanner;
public class Deom05 {
public static void main(String[] args) {
Scanner num = new Scanner (System.in);
System.out.println("猜数游戏开始啦");
int n1 = (int)(Math.random()* 1000) ;
int n = 1;
while(n != n1) {
System.out.println("请输入你心里想的数字");
n = num.nextInt();
if(n < n1) {
System.out.println("数字小了哦");
}else if(n > n1) {
System.out.println("数字大了哦");
}else {
System.out.println("恭喜你猜对啦");
break;//结束循环
}
}
num.close();//强制结束
System.out.println("游戏结束");
}
}
do...while
与while循环不一样的是,它先执行一次(do),再循环
代码说明
//do while
public class Deom08 {
public static void main(String[] args) {
int a = 1;
do {
System.out.println(a);
}while(a < 2 );
int b = 1;
while(b < 2) {
System.out.println(b);
b++;
}
}
}
补充说明
break
continue
死循环:也就是循环中的条件永远为true,死循环的是永不结束的循环。例如:while (true)
{} 。 嵌套循环:标签:语句,JAVA,Scanner,int,day02,System,println,public,out 来源: https://blog.csdn.net/barcelona733/article/details/118554407
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。