switch 中嵌套for循环

如下代码为何还会执行for循环,明明for循环在case 30里面呀?
public class Test {
public static void main(String args[]){
int x = 40;

  switch( x ){

     case 10:
     System.out.print("Value of X is 10");
     break;

     case 20:

     System.out.print("Value of X is 20");
     break;

     case 30:
     System.out.println("Value of X is 30");

     for(;x<=50;){
     x++;
     }
     //break;

     case 40:
     System.out.println("Value of X is 40");
     //break;

     case 50:
     System.out.println("Value of X is 50");
     //break;


     default:
     System.out.println("这是 default 语句");

    }

 System.out.print("x=" + x);

}
}

3个回答

我复制你的代码之后的运行结果,case 30: 并没有执行

 Value of X is 40
Value of X is 50
这是 default 语句
x=40请按任意键继续. . .

我照着你的代码写了 没有进入循环

可能当时是你运行有点问题吧!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
Python+OpenCV计算机视觉

Python+OpenCV计算机视觉

java嵌套for循环的跳出

for(Object object : objects) { for(Object2 object2 : objects2) { if(x>0)...... } } 怎么才能让if(x>0)时结束object2的循环,并且让object执行下一次循环 !在线等,求救

javascript中switchi和for循环的break语句放在一起,是跳出哪个语句呢?

javascript中switchi和for循环的break语句放在一起,是跳出哪个语句呢?

javascript里可以把for循环放在switchi里面么?

javascript里可以把for循环放在switchi里面么?其中的break语句是结束循环还是结束选择?

如何在switch里面停止循环?

怎么能在case9里面结束循环 不再显示 Please choose from one of the following menu options - 1) Display levels and hourly rates of consultants 2) Display levels and hourly bill rates of consultants 3) Staff project 4) Display project staffing proposal 5) Calculate profit 9) Submit project staffing proposal and exit option: 让结果显示如下: ![图片说明](https://img-ask.csdn.net/upload/201910/12/1570837717_226996.png) 这是我现在的代码 ![图片说明](https://img-ask.csdn.net/upload/201910/12/1570837877_397571.png) ![图片说明](https://img-ask.csdn.net/upload/201910/12/1570837884_982435.png) public static int displayMenu() { Scanner input = new Scanner(System.in); while (true) { System.out.println(); System.out.println("Please choose from one of the following menu options -"); System.out.println("1) Display levels and hourly rates of consultants"); System.out.println("2) Display levels and hourly bill rates of consultants"); System.out.println("3) Staff project"); System.out.println("4) Display project staffing proposal"); System.out.println("5) Calculate profit"); System.out.println("9) Submit project staffing proposal and exit"); System.out.println("option:"); int option = input.nextInt(); switch (option) { case 9: System.out.println("You have successfully submitted your project proposal. Good-bye! "); break;

foreach 循环体中使用switch...continue

按照我的理解,switch...case中使用的continue是对外层的循环体作用的,continue后面的语句不会执行,程序会跳转到下一次循环。 但是,下面的执行结果不能理解,请各位大大帮忙解释一下,谢谢了! 代码清单: ```php $array = ['nofile.php', 'demo.php']; foreach ($array as $v) { echo '$v:'. $v. BS; switch (true) { case false === $v = realpath(filter_var($v)): case !is_file($v): case !is_readable($v): echo $v. ' in case'. BS; continue; // or return false, throw new InvalidArgumentException default: echo 'break'. BS; break; } //... echo $v. ' in foreach'. BS; } ``` 输出结果: ```bash $ php demo.php $v:nofile.php in case in foreach $v:demo.php break /var/www/html/test/demo.php in foreach ``` 第一个 case 做了什么,为什么当 $v 这个文件不存在时,$v会变成false; 当$v这个文件不存在时,会执行到 continue ,然后为什么又执行了switch后面的语句,`in case`和`in foreach`都有打印。 这个例子是从[php manual - CURLFile 类](http://php.net/manual/zh/class.curlfile.php)看到的,不常见的写法但是非常考基础。

循环语句和switch...case

请问在循环语句中使用了switch...case,如何在分支结束后再执行另一个分支?

foreach循环中嵌套while循环的结果集结构

<div class="post-text" itemprop="text"> <p>I'm using Sphinx Search to search item on my website.I'm using foreach to find out the index,where the search is coming from? Foreach loop will iterate through all items that matches search term and will find out the index.</p> <p>All items in database have user_id that reference to user from user table.So,what I want to output is show few items(searches for) and put it together under that user.If search for 'Book',then output Like this</p> <pre><code>Alex //Book1,Book2,Book3 all of them has user_id of 2 which refers to Alex Book1 Book2 Book3 Jordan //Book11,Book12,Book18 all of them has user_id of 5 which refers to Joradn Book11 Book12 Book18 and so on. </code></pre> <p>Here is the code for that</p> <pre><code>foreach ( $res["matches"] as $doc =&gt; $docinfo ) { switch($docinfo['attrs']['table_id']) { case 1: //if search come from index 1 $res_db = mysqli_query($connect3,'select id_2, image, user_id, title, detail from lunch_menu where id_2 = (('.$doc.'-300000))'); if ($res_db === false) { echo "Error in mysql query #" . mysqli_errno($connect) . ' - ' . mysqli_error($connect); } else { $row = mysqli_fetch_assoc($res_db); $connect4=mysqli_connect('localhost', 'root', '','user'); $sql2="SELECT * FROM `user`.`user_det` WHERE id='".$row['user_id']."' GROUP BY id ORDER BY id DESC"; $query2=mysqli_query($connect4,$sql2); while($row2 = mysqli_fetch_assoc($query2)){ //line X $userx=$row2['id']; $image=$row2['img']; $busi_title=$row2['busi_title']; $page_owner.="&lt;img src='../../account/$userx/$image' width='140' height='140'&gt;&lt;b&gt;$busi_title&lt;/b&gt;&lt;hr&gt;"; $res_db2 = mysqli_query($connect3,'select id_2, image, user_id, title, detail from lunch_menu where id_2 in ((' . $doc . '-300000)) order by field(id_2,('.$doc.'-300000))'); $alu=""; while($row3 = mysqli_fetch_assoc($res_db2)){ $alu.= '&lt;img src="../../'.$row3['user_id'].'/lunch/'.$row3['image'].'" width="100" height="100"&gt;' . '&lt;a href="../../'.$row3['user_id'].'/menu_item2.php"&gt;' . $row3['title'] . '&lt;a&gt;' . '&lt;br/&gt; '.$row3['detail'].'&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;' ; } $all.="&lt;div id='' style='border:2px solid black;'&gt;".$page_owner.".".$alu."&lt;/div&gt;"; } } </code></pre> <p>But above code is giving me result like this</p> <pre><code>Alex Book1 Alex Book2 Alex Book3 Jordan Book11 Jordan Book12 Jordan Book18 and so on. </code></pre> <p>What modification do I need to get my code work like expected.I have tried my best to find a solution,but didn't help.So,just made a SO account to post the question.</p> <p>Please help,if you can.That would be a really awesome.</p> <p>Thanks in advance</p> <p><strong>Thank you Ryan for your reply.We are almost there</strong></p> <p>1 more fix,then we are done.Now what I'm getting is</p> <pre><code>Problem Result is almost the way,I was looking for except for the first one.This is what I'm getting Alex Book1 Jordan Book11 Book12 Book18 Alex Book2 Book3 </code></pre> <p><strong>If I can do a temporary <code>order by user_id</code> before echoing out <code>$all</code>,this bug might be fixed.</strong></p> <p>Because <code>$all</code> prints <code>user</code> and it's <code>corresponding item</code> the way it is sorted out in database</p> <p>Please be informed,I'm using the second way,you showed.First one is not working.</p> <p><strong>I have figured out Second part of the problem.To achieve that desired result,I needed to do this</strong></p> <pre><code>$cl-&gt;SetSortMode(SPH_SORT_EXTENDED, '@relevance DESC,user_id ASC'); </code></pre> <p>This line of code output most relevant search results first,and categorize by <code>user_id</code> Ascending.</p> <p><code>Special Thanks to Ryan</code></p> </div>

java中continue语句,在主串中匹配字串,那个双层嵌套不是很懂,有大神帮解释下吗

``` class ContinueWithLabelDemo{ public static void main(String[] args) { String searchMe = "Look for a substring in me"; String substring = "sub"; boolean foundIt = false; int max = searchMe.length() - substring.length(); test: for(int i = 0; i <= max; i++){ int n = substring.length(); int j = i; int k = 0; while(n-- != 0){ if(searchMe.charAt(j++) != substring.charAt(k++)){ continue test; } } foundIt = true; break test; } System.out.println(foundIt ? "Found it" : "Didn't find it"); } } ```

switch和if语句的综合运用错误

<!DOCTYPE HTML> <html> <head> <meta charset="gbk"> <title>switch和if语句的运用</title> <script> var now=new Date(); var year=now.getFullYear(); var month=now.getMonth()+1; var lngDay; switch (month){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: lngDay=31; break; case 4: case 6: case 9: case 11: lngDay=30; break; case 2: if((year%4==0 && year%100 !=0)||(year%400==0)){ lngDay=29; }else{ lngDay=28: } break; }//switch document.write("本月共有"+lngDay+"天"); </script> </head> <body> </body> </html>

java中,break的作用是跳出当前循环还是跳出当前作用域?哪个才是严瑾而正确的?

java中,break的作用是跳出当前循环还是跳出当前作用域?哪个更正确一点呢?小白学这里做练习遇到困难了,请大牛们指教一下。谢谢大家!

C++使用while for do...while分别计算一个数字的阶乘

C++使用while for do...while分别计算一个数字的阶乘 要求编写三个程序,用三种循环分别实现

求教N-S图和流程图。。。。。

分别使用if-else if结构和if else嵌套结构来完成下列分段函数的计算,使用循环语句讨论x的3种情况 画N-S图和流程图 ![图片说明](https://img-ask.csdn.net/upload/201412/25/1419519880_98594.png)

从循环内部获取最后一个日期

<div class="post-text" itemprop="text"> <p>This is what I've tried so far:</p> <pre><code>&lt;?php for( $i = 1; $i &lt;= $month_amortization; $i++ ) { $nextmonth = date('F d, Y', strtotime('+'.$i.' month')); $last = substr($i, -1, 1); switch($last) { case "1": $os = 'st'; break; case "2": $os = 'nd'; break; case "3": $os = 'rd'; break; default: $os = 'th'; } ?&gt; &lt;tr&gt; &lt;td&gt;&lt;?php echo $i; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $nextmonth; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $i .$os . ' Monthly Amortization'; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo 'PHP '. number_format($monthly_amortization, 2); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php } ?&gt; </code></pre> <p>This displays dates. I want to get the last date from the loop. I tried echoing $nextmonth; but I'm getting only the date today. How can I get the last date from inside the loop? Any ideas? Help is much appreciated. Thanks.</p> </div>

php foreach多维数组递归无法正常工作?

<div class="post-text" itemprop="text"> <p>Some kind of followup to my last question: <a href="https://stackoverflow.com/q/26380947/4112973">for loop - move deeper on numeric key in multidimensional array</a></p> <p>I have this array as input:</p> <pre><code>Array ( [0] =&gt; apl_struct Object ( [funcname] =&gt; say [args] =&gt; Array ( [0] =&gt; Array ( [0] =&gt; apl_struct Object ( [funcname] =&gt; text [args] =&gt; Array ( [value] =&gt; hello ) ) ) ) ) ) </code></pre> <p>I now have 2 functions working for me. One is a func only for getting the next key/value in an associative array. None of the next(), prev(), etc. were working for me like on indexed arrays:</p> <pre><code>function getnext($array, $key) { $keys = array_keys($array); if ((false !== ($p = array_search($key, $keys))) &amp;&amp; ($p &lt; count($keys) - 1)) { return array('key' =&gt; $keys[++$p], 'value' =&gt; $array[$keys[$p]]); } else {return false;} } </code></pre> <p>The next function is my executer or constructer. he creates a semi-xmlstruct for me. I tried to add recursion for skipping the numeric key. They're obviously nonsense and can be skipped.</p> <p>I then want to check if all the values of the non-numeric keys are arrays or not. If it is an array it indicates arguments to be followed and output should look like: INPUT.</p> <p>If not, it's either the functionname (funcname) or indeed a real value for us like "hello".</p> <pre><code>function arr2xml($array, $level = 1, $pos = 1) { $xml = ''; foreach ($array as $key =&gt; $value) { if (is_object($value)) {$value = get_object_vars($value);}// convert object to array if (is_numeric($key)) { $xml .= arr2xml($value); } else { if (!is_array($value)) { switch ($key) { case 'funcname': $nextkey = getnext($array, $key); $xml .= str_repeat("\t", $level) . "&lt;apl:$value&gt; "; $xml .= arr2xml($nextkey['value'], $level++); $xml .= str_repeat("\t", $level) . "&lt;/apl:$value&gt; "; break; case 'value': $xml .= str_repeat("\t", $level) . "\t$value "; break; } } else { $xml .= str_repeat("\t", $level) . "&lt;$key pos='$pos'&gt; \t"; $xml .= arr2xml($value, $level++, $pos++); $xml .= str_repeat("\t", $level) . "&lt;/$key&gt; "; } } } return $xml; } </code></pre> <p>but what I am getting out of this so far is this: the function name was inserted right. it is say and text. also, in some wild circumstances, the -tag and the value are executed properly.</p> <pre><code>&lt;apl:say&gt; &lt;apl:text&gt; hello &lt;/apl:text&gt; &lt;args pos='1'&gt; hello &lt;/args&gt; &lt;/apl:say&gt; &lt;args pos='1'&gt; &lt;apl:text&gt; hello &lt;/apl:text&gt; &lt;args pos='1'&gt; hello &lt;/args&gt; &lt;/args&gt; &lt;/xml&gt; </code></pre> <p>for me it looks like the recursion isn't really working. Am i missing something here? I've tried to rebuild it from previous mentioned post.</p> <p>Also I'm wondering about the multiple output I am getting here. The tags seem to get filled right, but the actual arrangement is quite confusing for me.</p> <p>I was expecting the output to look like this:</p> <pre><code>&lt;apl:say&gt; &lt;args pos='1'&gt; &lt;apl:text&gt; &lt;args pos='1'&gt; hello &lt;/args&gt; &lt;/apl:text&gt; &lt;/args&gt; &lt;/apl:say&gt; </code></pre> <p>Thanks in advance</p> </div>

2019 Python开发者日-培训

2019 Python开发者日-培训

150讲轻松搞定Python网络爬虫

150讲轻松搞定Python网络爬虫

设计模式(JAVA语言实现)--20种设计模式附带源码

设计模式(JAVA语言实现)--20种设计模式附带源码

YOLOv3目标检测实战:训练自己的数据集

YOLOv3目标检测实战:训练自己的数据集

java后台+微信小程序 实现完整的点餐系统

java后台+微信小程序 实现完整的点餐系统

三个项目玩转深度学习(附1G源码)

三个项目玩转深度学习(附1G源码)

初级玩转Linux+Ubuntu(嵌入式开发基础课程)

初级玩转Linux+Ubuntu(嵌入式开发基础课程)

2019 AI开发者大会

2019 AI开发者大会

玩转Linux:常用命令实例指南

玩转Linux:常用命令实例指南

一学即懂的计算机视觉(第一季)

一学即懂的计算机视觉(第一季)

4小时玩转微信小程序——基础入门与微信支付实战

4小时玩转微信小程序——基础入门与微信支付实战

Git 实用技巧

Git 实用技巧

Python数据清洗实战入门

Python数据清洗实战入门

使用TensorFlow+keras快速构建图像分类模型

使用TensorFlow+keras快速构建图像分类模型

实用主义学Python(小白也容易上手的Python实用案例)

实用主义学Python(小白也容易上手的Python实用案例)

程序员的算法通关课:知己知彼(第一季)

程序员的算法通关课:知己知彼(第一季)

MySQL数据库从入门到实战应用

MySQL数据库从入门到实战应用

机器学习初学者必会的案例精讲

机器学习初学者必会的案例精讲

手把手实现Java图书管理系统(附源码)

手把手实现Java图书管理系统(附源码)

极简JAVA学习营第四期(报名以后加助教微信:eduxy-1)

极简JAVA学习营第四期(报名以后加助教微信:eduxy-1)

.net core快速开发框架

.net core快速开发框架

玩转Python-Python3基础入门

玩转Python-Python3基础入门

Python数据挖掘简易入门

Python数据挖掘简易入门

微信公众平台开发入门

微信公众平台开发入门

程序员的兼职技能课

程序员的兼职技能课

Windows版YOLOv4目标检测实战:训练自己的数据集

Windows版YOLOv4目标检测实战:训练自己的数据集

HoloLens2开发入门教程

HoloLens2开发入门教程

微信小程序开发实战

微信小程序开发实战

Java8零基础入门视频教程

Java8零基础入门视频教程

Python可以这样学(第一季:Python内功修炼)

Python可以这样学(第一季:Python内功修炼)

C++语言基础视频教程

C++语言基础视频教程

Python可以这样学(第四季:数据分析与科学计算可视化)

Python可以这样学(第四季:数据分析与科学计算可视化)

网络工程师小白入门--【思科CCNA、华为HCNA等网络工程师认证】

网络工程师小白入门--【思科CCNA、华为HCNA等网络工程师认证】

Python数据分析与挖掘

Python数据分析与挖掘

相关热词 c# cad插入影像 c#设计思想 c#正则表达式 转换 c#form复制 c#写web c# 柱形图 c# wcf 服务库 c#应用程序管理器 c#数组如何赋值给数组 c#序列化应用目的博客园
立即提问