我从 Scala 开始,在功能上很难思考。
我有以下变量:
val dateTimeBegin = Calendar.getInstance()
dateTimeBegin.set(2016, Calendar.MAY, 25, 12, 0, 0)
val dateTimeEnd = Calendar.getInstance()
dateTimeEnd.set(2016, Calendar.MAY, 25, 16, 0, 0)
var slotsString = List[String]()
var slots = List[Calendar]()
Run Code Online (Sandbox Code Playgroud)
然后,我写了这个while循环:
while (dateTimeBegin.getTime().compareTo(dateTimeEnd.getTime()) == -1) {
slotsString = dateTimeBegin.getTime().toString() :: slotsString
dateTimeBegin.add(Calendar.MINUTE, 50)
}
Run Code Online (Sandbox Code Playgroud)
它基本上选择 dateTimeBegin 和 dateTimeEnd 并创建 50 分钟的插槽。
这是 while 循环给出的输出:
2016年5月25日星期三12:00:00 WEST 2016 WEST 25 12:50:00 WEST 2016 WEST 25 13:40:00 WEST 2016 WEST 2016 WEST 14:30:00 WEST 2016 WEST 202006 WEST
但是,我知道这不是在 Scala …
我想三次执行命令,并且每次将命令执行的输出存储在不同的变量中。下面的代码是每次将它存储在同一个变量中。我如何存储在不同的变量中?
int $i=0;
while ( $i< 3 ) {
$a = ( " command gets executed her " );
$i = $i+1 ;
}
Run Code Online (Sandbox Code Playgroud)
我希望输出存储在三个不同的变量中,比如 $a $ b 和 $ c。
我有一个 while 循环,其中有 2 个由“或”分隔的条件:
while (($i <= 8) || ($x <= 5)){
$i++;
$x++;
}
Run Code Online (Sandbox Code Playgroud)
我希望循环在达到最低数字时结束 - 在上面的案例 5 中。但是,当满足第一个更高的条件时,上述停止并循环 8 次。
当我交换值时仍然会发生这种情况,例如:
while (($i <= 5) || ($x <= 8))
Run Code Online (Sandbox Code Playgroud)
循环仍然会忽略 <= 5 并循环 8 次。
有谁知道我如何解决这个问题以使循环周期停止在较低的数字?
我有一个对象,它可以包含自己类型的列表。例如,项目类包含各种属性。另一个类 ItemSet 由一个项目列表组成,但也可以有一个嵌套的 Item 集。换句话说,一个 ItemSet 可以包含其他项目集。这看起来如下:
public class Item
{
public int Property1 { get; set; }
public int Property2 { get; set; }
public int Property3 { get; set; }
public int Property4 { get; set; }
}
public class ItemSet
{
public List<Item> Items { get; set; }
//.
//.
//.
//.
public List<ItemSet> ItemSets { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我一直在兜圈子(哈哈,明白了吗?)试图找出如何循环遍历 ItemSet 对象。我一直无法适应父子集的无限可能性。我觉得我让这比它需要的要困难得多。
ItemSets = getItemSets(....);
bool hasSets = false;
ItemSet currentItemSet;
foreach(ItemSet itemSet in currentItemSets)
{ …Run Code Online (Sandbox Code Playgroud) 我在看一个词法分析器代码,我遇到了这个:
while ("false")
{
c = fgetc(f);
switch (state)
...
Run Code Online (Sandbox Code Playgroud)
我测试了它并且代码有效,它产生了正确的结果。如果false有条件,为什么循环运行?循环不应该运行吗?
我是编程的新手,我现在正在我的高中学习入门课程。我们现在正在做的一项任务是一个程序,它询问用户他们想要计算什么公式(各种面积/体积/等类型的公式),询问他们进行计算所需的数据,然后给他们答案。我之所以对编程非常感兴趣,主要是因为这项任务,因为我已经意识到这项工作有多大的脑筋急转弯。
有了这个,我决定超越一点。我清理了我凌乱的代码,现在我正在尝试创建一个 while 循环,允许用户继续计算公式而无需再次运行程序,并在他们输入不正确的数据时给他们一条错误消息。后面的部分我已经想通了,但是我想在将第二个问题的解决方案放入代码之前实现前者。
import java.util.Scanner;
public class FormulaRemake {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n","What formula would you like to calculate?","Area of a Circle (AOC)","Circumference of a Circle (COC)","Area of a Trapezoid (AOT)","Volume of a Cylinder (VOC)","Volume of a Sphere (VOS)","Volume of a Cone (VON)");
String formula = input.nextLine();
while((formula.equalsIgnoreCase("AOC"))||(formula.equalsIgnoreCase("COC"))||(formula.equalsIgnoreCase("AOT"))||(formula.equalsIgnoreCase("VOC"))||(formula.equalsIgnoreCase("VOS"))||(formula.equalsIgnoreCase("VON")))
{
if(formula.equalsIgnoreCase("AOC"))
{
System.out.println("What is the circle's radius?");
double inputRadius = input.nextDouble();
System.out.printf("%.2f",Math.PI*(Math.pow(inputRadius,2)));
}
if(formula.equalsIgnoreCase("COC"))
{
System.out.print("What is …Run Code Online (Sandbox Code Playgroud) 我正在使用 while 循环遍历一些表并对字段执行替换,以删除所有连字符。所有字段都是 varchar。
DECLARE @Table TABLE (TableName VARCHAR(max),Id int identity(1,1))
INSERT INTO @Table
Select distinct table_name From INFORMATION_SCHEMA.COLUMNS
DECLARE @max int
DECLARE @SQL VARCHAR(max)
DECLARE @TableName VARCHAR(max)
DECLARE @id int = 1
select @max = MAX(Id) from @Table
WHILE (@id <= @max)
BEGIN
SELECT @TableName = TableName FROM @Table WHERE Id = @id
SET @SQL = 'update '+ @TableName +' set colA = replace(colA,'-','');'
EXEC(@SQL)
SET @id = @id +1
END
Run Code Online (Sandbox Code Playgroud)
我收到的错误是:
数据类型 varchar(max) 和 varchar 在减法运算符中不兼容。
我尝试将 varchar …
该脚本将 cvs 列表分成三列。我们专注于“名称”列。我想发现字符最多的名字。找到包含最多字符的名称后,我想将其分配给一个变量。
#!/bin/bash
for i in $(cat homeaway.txt )
do
echo $i | while IFS=, read -r area name host
do
maxLength=0
length=${#name}
if [ $length -gt $maxLength ] ; then
maxLength=$length
else
:
fi
printf "%s\n" $maxLength
done
done
Run Code Online (Sandbox Code Playgroud)
脚本说 - 英文 - 如果长度大于 maxlength,则将 length 设置为 maxLength,如果不是,则不执行任何操作。其中字符数最多的区域字符串是“script_name_12345678999999”,有26个。当脚本读取所有字符时,$maxLength应该返回26。
__DATA__
HOME,script_name_12345,USAhost.com
AWAY,script_name_123,USAhost.com
HOME,script_name_1,EUROhost.com
AWAY,script_name_123,USAhost.com
HOME,script_name_123456,EUROhost.com
AWAY,script_name_12345678999999,USAhost.com
HOME,script_name_1234,USAhost.com
AWAY,script_name_1234578,USAhost.com
HOME,script_name_12,EUROhost.com
AWAY,script_name_123456789,USAhost.com
Run Code Online (Sandbox Code Playgroud)
一旦脚本达到包含 26 个字符的区域值,它应该停止向 $maxLength 分配任何内容。相反,它返回每个字符串长度的列表,我不知道零是如何进入这里的
casper@casper01.com $ ./length_test.sh
17
0 ### how does the zero get in here ? …Run Code Online (Sandbox Code Playgroud) 假设我有这个循环:
boolean modified = true;
while(modified == true){
modified = false;
for (Object o : objectList){
// do logic
if (... condition...){
modified = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢我最初必须将修改后的变量声明为 true。如果在一轮中没有发生更多修改,则循环应终止。
我正在学习 Python 并且在理解 While 循环时遇到了一些麻烦。我试图计算负数的总数,但无法正确计算。
given_list3=[7,5,4,4,3,1,-2,-3,-5,-7]
total6=0
i=0
while i<len(given_list3)and given_list3[i]<0:
total6+=given_list3[i]
i+=1
print(total6)
Run Code Online (Sandbox Code Playgroud)
这是我计算正数总和的代码(工作正常)
given_list=[5,4,4,3,1,-3,-4,-5]
total3=0
i=0
while i<len(given_list) and given_list[i]>0:
total3+= given_list[i]
i+=1
print(total3)
Run Code Online (Sandbox Code Playgroud)