由于某些原因,循环只是让我循环。 我有问题创建和识别正在增加的权利变量和不正确的变量。
无论如何,我必须使用一个循环来不断提示用户输入一个数字,直到他们输入0然后将所有输入加在一起。
所以我的基本代码是:
let numPrompt = +prompt("Enter a number");
if (isNaN(numPrompt)) {
numPrompt = +prompt("Enter a number");
}
while (numPrompt != 0) {
numPrompt = +prompt("Enter another number");
console.log(numPrompt);
}
循环的工作原理是要求输入直到0.但是我有一个问题是想办法跟踪输入,然后将它们全部加在一起成为1值。
答案 0 :(得分:0)
var input = prompt("Enter a number");
var min = parseInt(input);
while (input !== "0") {
input = parseInt(input);
if (input < min) {
min = input;
}
input = prompt("Enter a number");
}
alert(min);
答案 1 :(得分:0)
好的尝试!我想指出一些事情。当您调用prompt()时,用户输入的任何内容都将是string类型。因此,当您使用isNaN()进行验证时,它将始终返回true。
接下来,由于prompt()返回字符串,您需要将其转换为带有parseInt()的整数,如下面的代码所示。
最后,在循环中,每次迭代都会为numPrompt重新分配一个新值。您需要将等号更改为&#39; + =&#39;为了增加numPrompt,或声明另一个变量(total)来保存所有用户输入的总和,使用&#39; + =&#39;再次运算符,如下所示。
然后我们在循环完成后记录循环外的总数。
let total = 0;
let numPrompt;
while (numPrompt != '0'){
numPrompt = prompt("Enter another number")
total += parseInt(numPrompt)
// Log each input
console.log(numPrompt)
}
// Log the total
console.log(total)
答案 2 :(得分:0)
您可以编写如下的版本:
n>=10
一直持续到0
。
如果您希望在n=0;
pr=-1;
while (pr!=0) {
pr=parseInt(prompt('enter a number'));
n+=pr;
}
被输入时终止,则需要一个额外的变量来存储输入的值:
Sub trial()
Dim wb As Workbook, wb2 As Workbook, wb3 As Workbook
Dim ws As Worksheet
Dim fn As String
Set wb = ActiveWorkbook
Set ws = Sheets.Add(After:=Sheets(Worksheets.Count))
Dim Ret
Ret = Application.GetOpenFilename("Lkl Files (*.lkl), *.lkl")
If Ret <> False Then
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & Ret, Destination:=Range("$A$1"))
.Name = "SPC_PLTB_450B_12092107_25°C_CW"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileDecimalSeparator = ","
.TextFileThousandsSeparator = "."
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End If
Sheets(2).Activate
'this is for the date (loop)
Dim FirstCell As String
Dim i As Integer
FirstCell = "C19"
Range(FirstCell).Select
Do Until ActiveCell.Value = ""
If ActiveCell.Value = "" Then
Exit Do
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
ActiveCell = Format(Date, "mm/dd/yyyy")
ws.Activate
ws.AutoFilterMode = False
ws.Range("$A$9:$P$417").AutoFilter Field:=5, Criteria1:= _
"1"
Range("F31:F401").Select
Selection.Copy
Sheets(2).Activate
'this is for the raw data
FirstCell = "D19"
Range(FirstCell).Select
Do Until ActiveCell.Value = ""
If ActiveCell.Value = "" Then
Exit Do
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Sheets(3).Activate
FirstCell = "C19"
Range(FirstCell).Select
Do Until ActiveCell.Value = ""
If ActiveCell.Value = "" Then
Exit Do
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
ActiveCell = Format(Date, "mm/dd/yyyy")
ws.Activate
Range("D31:D401").Select
Application.CutCopyMode = False
Selection.Copy
Sheets(3).Activate
FirstCell = "D19"
Range(FirstCell).Select
Do Until ActiveCell.Value = ""
If ActiveCell.Value = "" Then
Exit Do
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Sheets(4).Activate
FirstCell = "C19"
Range(FirstCell).Select
Do Until ActiveCell.Value = ""
If ActiveCell.Value = "" Then
Exit Do
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
ActiveCell = Format(Date, "mm/dd/yyyy")
ws.Activate
Range("G31:G401").Select
Application.CutCopyMode = False
Selection.Copy
Sheets(4).Activate
FirstCell = "D19"
Range(FirstCell).Select
Do Until ActiveCell.Value = ""
If ActiveCell.Value = "" Then
Exit Do
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Worksheets(5).Delete
End Sub
答案 3 :(得分:0)
1~~3~4~5
如果用户输入的是字符而不是整数,则需要进行处理。
field 1: 1
field 2: 3
field 3: 4
field 4: 5
field 5:
用于跟踪所有用户输入(如果您不需要保存,可以跳过它)