我希望你们做得很好,我正在学习 Javascript,我了解了“继续”,我们可以在循环中使用它进行迭代。但这是我无法得到的
首先看一下代码:
let k = 1
do {
if (k === 9) {
k++;
continue;
}
console.log(k + 1);
k++;
} while (k < 15);Run Code Online (Sandbox Code Playgroud)
当console.log中的值为(k+1)时,则打印9,而漏掉10。不明白为什么?
但是当使用这段代码时
let k = 1
do {
if (k === 9) {
k++;
continue;
}
console.log(k);
k++;
} while (k < 15);Run Code Online (Sandbox Code Playgroud)
当console.log中为(k)时,不打印9而打印10。
无法理解何时使用简单 (k) 和何时使用 (k+1) 背后的逻辑?
这就是我所理解的。如果K=1。控制台.log(k+1)。2 被打印。然后由于 k++。k 变为 2 并且条件经过测试,由于它为真,因此它将移动到另一个循环。这种情况还在继续。正确的?
谢谢
我正在尝试创建一个bash脚本,以便在位于多个目录中的日志中轻松执行搜索.下面是我的do-while循环函数,它是发生错误的地方.如果需要完整的shell脚本进行故障排除,请告诉我."x"代表私人材料.
main_menu ()
{
TEMP=1
while [ "$TEMP" ! = "0" ]
do
clear
echo "###############################################"
echo
echo " xxxxxxxxxx xxx Search "
echo
echo "###############################################"
echo
echo " [1.] Search Logs by x/x "
echo
echo " [0.] Exit "
echo
echo " Select an Option: "
echo
read TEMP
case $TEMP in
1)
x_name
x_or_x
get_x
Run Code Online (Sandbox Code Playgroud) 我刚刚编写了一个简单的c#代码来计算数字的阶乘,但程序却被卡在姓氏上.有人为什么会被卡住?
谢谢,
OMIN
using System;
//1. Write a program which finds the factorial of a number entered by the user.
namespace Beginner1{
class ProblemOne
{
static void Main (string[] args)
{
bool play = true;
while ( play ) {
Console.Write ("Type in the number you would like to find Factorial of: ");
int num = Convert.ToInt16( Console.ReadLine ());
int sum = 1;
for (int i = 2; i <= num; i++) {
sum = sum * i;
}
Console.WriteLine …Run Code Online (Sandbox Code Playgroud) 这是每个人都告诉我要做的工作产品.谢谢大家,我会尽量保持我的代码清洁.这段代码只是练习,最终将成为我的计算机在启动时运行的锁定系统.谁想要讨厌的青春期前乱搞他们的电脑?不是这个人
import java.io.*;
import java.util.Scanner; /
class AgeChecker
{
public static void main (String[] args) throws Exception /*@Exception- thrown to allow reading
{ of single characters*/
char ans; //(Read from user input)
String name;
boolean loop = false; //To loop back after a section, add loop = true.
//To stop the program after a section, add loop = false.
do
{
Scanner dd = new Scanner(System.in);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // Needed to read the
BufferedReader in1 = …Run Code Online (Sandbox Code Playgroud) 我无法修复此错误:
Parse error: syntax error, unexpected 'while' (T_WHILE) in /xyz/xyz.php on line 76
Run Code Online (Sandbox Code Playgroud)
我正在使用php 5.4.4
有人可以帮忙吗?
xyz.php 74-96行:
if (( function_exists( 'get_magic_quotes_gpc' ) && get_magic_quotes_gpc( ) )) {
$process = array( &$_GET, &$_POST, &$_COOKIE, &$_REQUEST );
$val = while (list($key, $val) = each($process)) {;
[0];
$key = ;
if () {
foreach ($val as $k => $v) {
unset( $process[$key][$k] );
if (is_array( $v )) {
$process[$key][stripslashes( $k )] = $v;
$process[] = &$process[$key][stripslashes( $k )];
continue;
}
$process[$key][stripslashes( $k …Run Code Online (Sandbox Code Playgroud) 我刚刚发现Roslyn源代码,我看到一个代码片段如下所示:
var text = stringBuilder.ToString();
int length;
do
{
length = text.Length;
text = text.Replace("{\r\n\r\n", "{\r\n");
} while (text.Length != length);
Run Code Online (Sandbox Code Playgroud)
它看起来很奇怪,因为该String.Replace方法一次性用一个新值替换所有出现的指定字符串.所以这个循环首次执行,执行更改然后再次执行第二次并且什么都不做.然后它结束..那么使用a loop而不是只写的重点是什么:
var text = stringBuilder.ToString();
text = text.Replace("{\r\n\r\n", "{\r\n");
Run Code Online (Sandbox Code Playgroud)
甚至更短:
var text = stringBuilder.ToString().Replace("{\r\n\r\n", "{\r\n");
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
对于我的Java类,我被要求创建一个while循环,然后将其转换为do-while循环,然后比较输出.这是个问题:
- 用do while循环替换问题4中的while循环.问题4的输出与这个问题之间可能存在什么差异?
我无法找到输出的差异,可能与否.以下是两者的代码和输出.
while循环
package fordemo;
import java.util.Scanner;
public class ForDemo {
public static void main(String[] args) {
System.out.println("Input a number:");
Scanner user_input = new Scanner (System.in);
int number = user_input.nextInt();
int n = -1;
while (n < number){
n=n+2;
System.out.print(n + " ");
}
}
}
Run Code Online (Sandbox Code Playgroud)
跑:
Input a number:
15
1 3 5 7 9 11 13 15 BUILD SUCCESSFUL (total time: 1 second)
Run Code Online (Sandbox Code Playgroud)
do-while循环
package fordemo;
import java.util.Scanner;
public class ForDemo {
public static void main(String[] …Run Code Online (Sandbox Code Playgroud) 我正在使用gcc-4.7在Centos 6.5上编写以下C代码,但是程序不等待类型Y/N:语句在结果后立即退出.其中任何一个代码都不起作用.
#include <stdio.h>
int main()
{
int a,b;
char ch='y';
do
{
printf("Enter Number 1:\n");
scanf("%d",&a);
printf("Enter Number 2:\n");
scanf("%d",&b);
printf("Result is:%d\n",a+b);
printf("Type y/N:\n");
scanf("%c",&ch);
}while(ch=='y' || ch=='Y');
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
要么
#include<stdio.h>
int main()
{
int a,b;
char ch='y';
while(ch=='y' || ch=='Y')
{
printf("Enter Number 1:\n");
scanf("%d",&a);
printf("Enter Number 2:\n");
scanf("%d",&b);
printf("Result is:%d\n",a+b);
printf("Type y/N:\n");
scanf("%c",&ch);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我是编程新手,所以我使用的代码相当于我学到的东西.无论如何我正在开发一种场景,在这种场景中,用户输入两个选项中的一个,这两个选择将触发不同的代码集来执行,或者至少是我试图做的事情.然而,当我浏览我的代码时,它正在执行它,好像没有if语句所有内容都作为一个块运行.我很确定我的逻辑是正确的,如果有的话我的语法是不确定的.如果有什么可以做的话,你们可以做些什么来帮助解决我的问题,如果可能的话,我想学习而不仅仅是解决问题.感谢您的时间.
int main()
{
int move = 0, pstrength = 10, pdefense = 20, pintelligence = 40, php = 10, estrength = 30,
edefense = 40, eintelligence = 25, ehp = 10;
printf("Battle Start!\n\nYour HP: %d Enemy HP: %d\n\n1 - Attack\n2 - Magic\n\n", php, ehp);
float pap = (float)pstrength / (float)edefense * 5;
float pmp = (float)pintelligence / (float)eintelligence * 5, eap = (float)estrength /
(float)pdefense * 5;
do {
printf("Select your move: ");
Run Code Online (Sandbox Code Playgroud)
如果选择了动作1,/player将选择执行什么动作然后玩家攻击如果敌人没有跌到0马力,则敌人攻击然后循环重置并提示第二轮输入 /
scanf("%d", &move); …Run Code Online (Sandbox Code Playgroud) 我完全难过了.无论我做什么,我的编译器都认为我需要在上一次printf中使用while循环?它只是一直在寻找没有意义的事情.当我将它们添加到其中时只是告诉我它不应该存在,当我删除它时我被告知我需要它.
int main() {
float userInput; //The grades that the user inputs.
int passingGrade = 0; //Number of passing grades
int failingGrade = 0; //Failing grades
int invalidGrade = 0; //Invalid grades
//This do for loop is simply counting the number of grades of each catagory posted.
do {
printf("Enter a grade (Enter -1 to quit):");
scanf("%f", &userInput);
printf("You entered: %.1f\n", userInput);
if ((userInput > 100) || (userInput < 0)) { //Over 100 is impossible.
invalidGrade = invalidGrade + 1; …Run Code Online (Sandbox Code Playgroud) do-while ×10
while-loop ×5
c ×3
c# ×2
java ×2
loops ×2
bash ×1
boolean ×1
centos ×1
compare ×1
continue ×1
if-statement ×1
javascript ×1
parsing ×1
php ×1
printf ×1
shell ×1
syntax ×1
user-input ×1