【Java 基础语法】逻辑控制

本文阅读 8 分钟
首页 代码,Java 正文

一、逻辑控制语句

1. 顺序结构

像我们写的代码,执行时会按照从上到下一行一行的执行。这就是顺序结构,不同的顺序,结果可能就不一样。如

System.out.println(1)
System.out.println(2)    
System.out.println(3)

该代码结果为 1(换行)2(换行)3,但是如果把顺序改变结果就变了

System.out.println(2)
System.out.println(3)    
System.out.println(1)

该代码结果就变为 2(换行)3(换行)1

2. 分支结构

2.1 if 语句

和 C 语言一样,在 Java 中 if 语句也有三种基本的形式

形式一

但是从形式上我们就可以看出还是有一些不同的

这种代码就是错的,条件必须是布尔值

2.2 switch 语句

Java 中的 switch 语句和 C 语言中的几乎一样,大家如果不太熟悉 switch 以及该章所讲解的一些逻辑结构可以去看看我写的 C语言三大语句注意事项 。但是既然不是完全一样,那哪里有区别呢?

在 Java 中,switch(表达式) 中的表达式是有类型限制的。

它只能使用:整数(只包括 byte、short、int)、字符(char)、字符串(String)、枚举类型

3. 循环结构

3.1 while 循环

舒服,Java 的 while 循环和 C 语言也是极其类似,只有一个小的差异

while (表达式) 中的表达式必须是布尔值

3.2 break

break 的功能是:让最靠近包裹它的整个循环提前结束。这也是和 C 语言是一样的

3.3 continue

continue 的功能是:跳过本次循环,立即进入下次循环。这也是和 C 语言是一样的

3.4 for 循环

Java 中的 for 循环和 C 语言中的也很类似,只有一个差异

for(表达式1;表达式2;表达式3) 中的表达式2是循环的判断条件,要使用布尔值

3.5 do while 循环

Java 中的 do while 循环也是和 C 语言很类似,只有一个差异

while 中的循环条件只能是布尔值

但是一般不推荐使用,因为它至少会执行一次循环,不注意可能会犯错

二、输入输出方式

1. 输出到控制台

  1. 基本语法 System.out.println(msg); // 输出一个字符串,自带换行
    System.out.print(msg); // 输出一个字符串,不带换行

System.out.printf(format,msg); // 格式化输出,括号内内容类似于 C 语言的 printf

  1. 代码示例 int a = 10;
    System.out.println(a); // 输出 a,自带换行

System.out.print(a); // 输出 a,不带换行
System.out.printf("%dn",a); // 格式化输出,括号内内容类似于 C 语言的 printf

  1. 格式化输出表(随用随查)
转换符 类型 例子 输出结果
d 十进制整数 ("%d",100) 100
x 十六进制整数 ("%x",100) 64
o 八进制整数 ("%o",100) 144
f 定点浮点数 ("%f",100f) 100.000000
e 指数浮点数 ("%e",100f) 1.000000e+02
g 通用浮点数 ("%g",100f) 100.000
a 十六进制浮点数 ("%a",100) 0x1.9p6
s 字符串 ("%s",100) 100
c 字符 ("%d",‘1’) 1
b 布尔值 ("%b",100) true
h 散列码 ("%h",100) 64
% 百分号 ("%.2f%%",2/7f) 0.29%

2. 从键盘输入

  1. 读入一个字符(不推荐使用)
<p>直接使用 System.in.read() 可以读入一个字符,但是需要搭配异常处理。如果不搭配异常,如下面代码就会编译出错</p>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{ 
     <!-- --></span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Enter a char:"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">char</span> i <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">)</span><span class="token class-name">System</span><span class="token punctuation">.</span>in<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The char is :"</span> <span class="token operator">+</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

<p>我们需要将鼠标放到 read 上面,再按住 Alt + Enter 键就会出现在这里插入图片描述</p>

<p>再点击第一个或者第二个就不会报错了。这里其实是处理了异常,但是一个简单的读入字符就要这么复杂所以不推荐使用。</p>
  1. 使用 Scanner 读取字符/字符串/整数/浮点数/布尔值
    2. 首先需要导入 util 包 import java.util.Scanner; ,在 IDEA 中,其可以通过输入 Scanner 后直接回车快速导入 2. 然后再构造一个 Scanner 对象并与”标准输入流“ System.in 关联: Scanner 对象名 = new Scanner(System.in); 2. 再就可以通过 Scanner 类的各种方法实现输入操作了 <pre><span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Scanner</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestDemo</span> <span class="token punctuation">{ <!-- --></span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{ <!-- --></span> <span class="token class-name">Scanner</span> in <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> a <span class="token operator">=</span> in<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//读入一个整数</span> <span class="token keyword">double</span> b <span class="token operator">=</span> in<span class="token punctuation">.</span><span class="token function">nextDouble</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//读入一个浮点数</span> <span class="token keyword">boolean</span> c <span class="token operator">=</span> in<span class="token punctuation">.</span><span class="token function">nextBoolean</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//读入一个布尔值</span> <span class="token class-name">String</span> s <span class="token operator">=</span> in<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//读入一个字符串</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> </pre> 但是要注意,上述读入字符串是按空白符当作分隔符的,故只能读入第一个空格之前的字符。如果想要读取该行的所有字符(包含空格),应该使用 String s = in.nextLine(); 但是使用这个方法还是有很大的缺陷,如果这行代码上面还有其他读入的语句,可能这行代码就不会实行。因为上一行的回车可能被它读入,直接这行结束。因此,只能将其放在读入代码的第一个或者在它前面加一个 in.nextLine(); 来读取掉之前的回车。
  1. 我们知道在 C 语言中可以写类似于这样的代码输入多组数据 int a;
    while(scanf("%d",&a) != EOF)

{

// 内容

} 在 C++ 中我们也可以写类似于这样的代码输入多组数据 int a;
while(cin>>a)
{

// 内容

} 那么在 Java 中怎么做呢?代码如下 <pre><span class="token class-name">Scanner</span> in <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span>in<span class="token punctuation">.</span><span class="token function">hasNextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{

<!-- --></span>
<span class="token keyword">int</span> a <span class="token operator">=</span> in<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 内容</span>

<span class="token punctuation">}</span>
</pre> 并且在 IDEA 上我们可以手动输入 Ctrl + D 结束,在 Windows 的 cmd 上 我们手动输入 Ctrl + Z 结束,在 Linux 或 Mac 上,输入 Ctrl + D 结束

三、猜数字游戏

学 C 语言的时候我们可能都接触过随机数的生成,或者直接写一个猜随机数的游戏。那么使用 Java 该怎么写呢?

  1. 首先我们要知道在 Java 中随机数是怎么生成的。
    1. 首先需要导入 util 包 import java.util.Random; ,在 IDEA 中,其可以通过输入 Random 后直接回车快速导入 1. 然后通过 Random 类中的方法生成我们想要的,如 <pre><span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Random</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestDemo</span> <span class="token punctuation">{ <!-- --></span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{ <!-- --></span> <span class="token class-name">Random</span> random <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 默认随机种子是系统时间</span> <span class="token keyword">int</span> rand <span class="token operator">=</span> random<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span>bound<span class="token operator">:</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment">// random.nextInt(bound:100) 是生成[0,100)间的随机整数</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> </pre>
  1. 接下来就是很简单了,只要我们输入的数字与随机数字进行一下比较。直到判断正确再退出就可。故最终的代码是 <pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestDemo</span> <span class="token punctuation">{

    <!-- --></span>
    
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{ 
    <!-- --></span>
        <span class="token class-name">Scanner</span> scanner <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Random</span> random <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> rand <span class="token operator">=</span> random<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{ 
    <!-- --></span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"请输入你所猜测的数字:"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">int</span> num <span class="token operator">=</span> scanner<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>num <span class="token operator">==</span> rand<span class="token punctuation">)</span><span class="token punctuation">{ 
    <!-- --></span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"恭喜你,猜对了!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">break</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>num <span class="token operator">&lt;</span> rand<span class="token punctuation">)</span><span class="token punctuation">{ 
    <!-- --></span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"很遗憾,你猜的数字小了!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{ 
    <!-- --></span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"很遗憾,你猜的数字大了!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token punctuation">}</span>

四、总结

这章讲了 Java 的逻辑控制结构,包含了顺序结构、顺序结构和分支结构。还讲了 Java 的输入输出方式。并在最后用 Java 程序写了一个简单的猜数字游戏。我们可以发现,很多都是和 C 语言类似的,这也降低了我们学习的难度。

下一章则是关于方法学习的介绍,这也是类似于 C 语言函数的东东。希望大家喜欢我的文章。自己也要继续加油,奥里给! img

本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://t4dmw.blog.csdn.net/article/details/119581808
-- 展开阅读全文 --
BUUCTF Web [极客大挑战 2019]Knife
« 上一篇 06-24
安全面试之XSS(跨站脚本攻击)
下一篇 » 07-24

发表评论

成为第一个评论的人

热门文章

标签TAG

最近回复