这是我的循环:
sure = input('Are you sure that you wish to reset the program?(y/n)')
while sure != 'y' or sure != 'n':
sure = input('Please awnser y or n, Are you sure that you wish to reset the program?(y/n)')
Run Code Online (Sandbox Code Playgroud)
循环持续进行,即使循环y或者n被输入.
我的评论说明了我的推理,但显然我有些不对劲.我的代码直接跳到"你的总债务......"
# Dictionary of Bills
expenses = {'mortgage':[], 'personal':[], 'power':[], 'insurance':[], 'water':[], 'food':[], 'savings':[], 'fuel':[], 'phone':[], 'internet':[], 'credit':[], 'emergencies':[]}
totalDebt = 0
switch = "A"
while switch == switch.isalpha(): # Condition is true, switch is a letter
for each in expenses: # Iterates through each bill
debt = input("%s: "%each) # User input
if debt.isdigit(): # checks to make sure they only enter numbers
debt = int(debt) # Converts debt to its integer value
totalDebt = totalDebt + debt # …Run Code Online (Sandbox Code Playgroud) 由于某种原因,我的原始数组被这个代码消灭了.我想不通为什么?运行之后@my_array是空的,我在while循环中取出了所有的工作,看看是不是原因,但事实并非如此.
foreach (@my_array)
{
open MY_FH, "<", $_;
while (<MY_FH>)
{
}
close MY_FH;
}
Run Code Online (Sandbox Code Playgroud) 程序跳过我的while循环并结束.超级沮丧.我甚至在while循环之前将AnsCheck的值设置为false.没运气.该程序不执行While循环中的任何操作.这是相关的代码:
bool AnsCheck;
AnsCheck = false;
while (AnsCheck = false)
{
getline(cin, Ans1);
if (Ans1 != "T" || Ans1 != "F")
{
cout << "Please Enter T for true or F for False" << endl;
cout << "answer not T or not F" << endl; // debugging
}
else
{
AnsCheck = true;
cout << "changed bool to true" << endl;
}
}
Run Code Online (Sandbox Code Playgroud) 我无法理解我在循环时遇到的问题.这是一个简单的例子:
i = 1
while i <10
bb = 2
i = i + 1
end
bb
bb not defined
while loading In[36], in expression starting on line 1
Run Code Online (Sandbox Code Playgroud)
我的问题是如何在这里定义bb?这是范围问题吗?我不认为是,因为我最后增加到10.
这里有很多关于项目欧拉问题的问题,关于找到低于1000的3和5的所有倍数的总和,但我是Java的初学者,我试图制作我自己的程序来试图弄清楚这一点我的代码存在的问题是,它不断给出答案466.466是这个问题的错误答案,所以我想知道我做错了什么.
这是我的代码.
package sumofmultiples;
public class sumofMultiples{
public static void main(String[] args){
int number = 1; //<- This is the number that we are checking to be a multiple.
int totalNumber= 0; //<- Total number of multiples counted so far.
while (number < 1000){
if ((number % 3) == 0){
totalNumber++;
//The number is a multiple of 3.
} else if ((number % 5) == 0){
totalNumber++;
//The number is a multiple of 5
}
number++; …Run Code Online (Sandbox Code Playgroud) 每次输入内容时,此代码都会继续循环.怎么解决?
public void inputPlayer()
{
Scanner input = new Scanner(System.in);
while(name.length() < 1)
{
System.out.println("Name: ");
this.setName(input.nextLine());
//name = input.nextLine();
if(name.length() < 1)
System.err.println("Must have one or more characters");
}
}
Run Code Online (Sandbox Code Playgroud)
Heres是整个事情的截图:
好的,所以对于Java来说,我是一个完全的菜鸟,所以如果这是一个愚蠢的问题,请道歉.我有一个while循环设置如下:
do {
//quiz questions, lose a life when you get one wrong
} while (livesRemain > 0)
Run Code Online (Sandbox Code Playgroud)
然而,当你失去所有生命时,即使所有生命都丧失,循环也不会结束并继续循环.我是否会使用除了while或do之外的其他东西,而当达到某个条件时它会结束?
问题是这样的:
System.out.print(question1)
if (guess == 1) {
System.out.print("Correct")
score++;
} else {
livesRemain--;
}
Run Code Online (Sandbox Code Playgroud)
当你到达测验结束时,它会打印你的分数,如果你低于0,它最终会进入底片.
我在下面使用while循环:
do {
scanf("%c", &turnChoice);
if (turnChoice == 'r')
{
invalidSelection = false;
}
else if (turnChoice == 'h')
{
invalidSelection = false;
}
else
{
printf("Invalid input.\n");
}
} while (invalidSelection == true);
Run Code Online (Sandbox Code Playgroud)
但是,在停止用户输入之前,它会运行一次完整循环(因此它显示"无效输入",然后要求用户输入一个字母).我究竟做错了什么?
我有一个文本文档,如下所示:
2 2 1 4 2 8
2 4 1 4 2 8
2 6 1 4 2 8
Run Code Online (Sandbox Code Playgroud)
我想找到第二列(2,4,6)中所有数字的平均值.我写了一个我认为应该这样做的循环.totalVal是加在一起的所有数字的总值,totalNum是数字的总数.
String line = data.nextLine();// read one line into a String
while (data.hasNextLine()) {
String[] field = line.split(" ");
totalVal += Double.parseDouble(field[1]);
totalNums += 1;
line = data.nextLine();
}
System.out.println(totalVal / totalNums);
Run Code Online (Sandbox Code Playgroud)
问题是,我得到的答案返回值3,这意味着它只取文档中前两行文本的平均值,并省略第三行.
有人可以告诉我如何更改循环以便它在第三行文本中添加值吗?
while-loop ×10
java ×4
python ×2
c ×1
c++ ×1
calculator ×1
foreach ×1
if-statement ×1
julia ×1
logic ×1
loops ×1
perl ×1
scanf ×1