Java Zero Basic Introduction - Java Flow Control Statement (Part 2)

Get into the habit of writing together! This is the 10th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Hello, everyone, I'm Meow Shou.

       Today, I want to share with you some of the knowledge points that I have learned every day, and communicate with you in the form of words, learn from each other, and grow faster, right?

       I am a java developer, so I have the most daily contact with java, so I will take advantage of my free time to recollect and output what I have learned. I don’t expect anything in return, I just want to help To more friends, just fine.

In the process of reviewing, if you think the article is good, please like, favorite, and follow. Sanlian is the best encouragement and support for Meow Shou on my creative path!

I. Introduction

       I don’t know if my friends have digested the knowledge points written in the last article. Let’s first announce the answers to the after-school homework of the next issue. I hope that the friends will check carefully to see if what they wrote is correct.

First, a single if statement is used to judge the writing method:

/**
 * 判断数字在什么区间
 */
public String test(){

    System.out.println("请输入一个数字:");
    Scanner input = new Scanner(System.in);
    int num = input.nextInt(); // 接收键盘输入数据

    if(num > 100){
        return "数字大于100!";
    }
    if(num<100 && num>=50){
        return "数字在50-100之间!";
    }
    if(num<50 && num>=0){
        return "数字在0-50之间!";
    }
    return "数字小于0!";
}
复制代码

The following is an if-else conforming statement:

/**
 * 判断数字在什么区间
 */
public String test(){

    System.out.println("请输入一个数字:");
    Scanner input = new Scanner(System.in);
    int num = input.nextInt(); // 接收键盘输入数据
    if(num > 100){
        return "数字大于100!";
    }else if(num<100 && num>=50){
        return "数字在50-100之间!";
    }else if(num<50 && num>=0){
        return "数字在0-50之间!";
    }else{
        return "数字小于0!";
    }
}
复制代码

Guys, is your writing correct? If it is not written correctly, then look at my analysis below!

       First, because there are three judgment processes, you should write three ifs first, and then use a variable to judge the number input by the user. The difficulty may be how to monitor and obtain the number input by the user. In fact, this is beyond the outline, but I didn't allow you to Baidu, you will know after Baidu!

Scanner is a class, nextDouble() is a member function of Scanner, System.in is passed as a parameter to the constructor of Scanner, so that Scanner uses the keyboard as input, and then uses new to instantiate a Scanner in memory, so that other variables can be called this memory area.

       This way you will understand, I will tell you about this category later.

Next, I will finish the third type of process control.

2. Process Control - Loop Structure

       You may ask, what is a loop, and the program can also perform loop processing. Programs are written by people, and people can do one thing in a loop. For example, they have to commute to get off work on weekdays. This is not just one thing, except in special circumstances.

       所以程序也一样,唯独就是程序进行循环是需要人为确认循环什么结束的,是需要有条件约束的,要不然程序死循环?知道程序崩塌这可不行,首先我写给大家举个简单的例子,然后通过代码的形式给大家讲,这样或许你们就能理解循环结构了。

比如,计算1-100之间的奇数有多少个?这个就得用到循环控制啦。

public int count(){

    //用来计数
    int count = 0;
    for(int i=1;i<=100;i++){
        if( i % 2 == 0){
            //说明是偶数
            continue;
        }
        count++;
    }
    //返回计数结果
    return count;
}
复制代码

       我们直接定义一个变量i,然后i从1一直循环到100,然后循环嵌套一个单if判断,目的是判断该i是奇数还是偶数,我们选择用i取余的方式进行,因为奇数取2余数肯定不为0,所以奇数就直接+1,偶数就直接进行continue,continue的中文意思就是继续,在程序中就是表示循环继续,不再进行语句之下的任何操作,跳过此次循环,所以我们就顺利的取到了1-100之间的奇数个数啦。

三、for循环语句

for循环语句的语法如下:

for(条件表达式1;条件表达式2;条件表达式3) {
    语句块;
}
复制代码

解释:

  • 条件表达式1:形式:赋值语句。作用:循环结构的开始,为循环变量赋初始值。eg:int i=1;
  • 条件表达式2:形式:条件语句。作用:循环结构的循环条件。eg:i<=100。
  • 条件表达式3:形式:迭代语句,通常使用 '++' 或者 '--' 运算符。eg:i++。

需要注意的是:

       for关键字后边括号里头的三个条件表达式,必须要用“;”隔开,要不然是会报错的。

还有除了for能进行循环之外,还有while、do while,foreach等。

下边我们再来演示一题。

       比如我们经常会遇到数学题求数字的多少次方,对吧,我们就可以自己写程序。

比如我们来求一个2的十次方。

public static int sum() {

    //用来统计和
    int sum = 2;
    for (int i = 1; i <= 10; i++) {
        sum *=2;
    }
    return sum;
}
复制代码

接下来我们用while语句进行转换。

public static int sum() {

    //用于统计和
    int sum = 2;
    int i = 1;
    while (i <= 10) {
        sum *=2;
    }
    return sum;
}
复制代码

相比之下,在已知循环次数的情况下,是不是使用while语句更为方便一些,有没有。

四、课后作业

  • 题目1:请写一个循环打印1-100以内的所有质数(质数的定义:在大于1的自然数中,除了1和它本身以外不再有其他因数的数我们就称为质数。)
  • 题目2:请实现一个从1加到n的算法,要求是n可以由用户手输。

给点提示:

  • First tip:

       A prime number has no other factors than 1 and itself. So I'm afraid that you won't. I'll give you the code for judging whether it is a prime number. You can refer to it yourself, but the premise is that it is allowed if you don't know it, and you must use your brain first.

public boolean isZS(int num){

    //排除1不是质数
    if(num<2){
        return false;
    }
    //判断是否为质数
    for (int i = 2; i <= num - 1; i++) {
        if (num % i == 0) {
            return false;
        }
    }
    return true;
}
复制代码
  • Tip for the second question:

It is simpler than the first question, just superimpose the results after each summation for the next time. for example:

   sum+=i;
复制代码

5. Ending

Well, the above is the whole content of this issue. If you have any questions, please leave a message below. See you in the next issue.

... ...

There is no priority in learning, no amount of knowledge; no matter how big or small, you should ask for advice with an open mind; if you walk with three people, there must be my teacher! ! !

wished for you successed !!!

---------------------------------------------------------------------

⭐️If you like me, please follow me.

⭐️If it is useful to you, please like it.

⭐️If you have any questions, please let me know in the comments.

---------------------------------------------------------------------

Guess you like

Origin juejin.im/post/7084643166645092383
Recommended