是否有可能在while语句中有三个条件?
例如:
cout << "\n\nHow would you like your parcel shipped?\n (please type standard, express"
" or same day)" << endl;
cin >> method;
while (method != ("standard" && "express" && "same day"))
{
cout << "invalid input: please follow the instructions carefully.." << endl;
cout << "\n\nHow would you like your parcel shipped?\n (please type 'standard',"
" 'express' or 'same day')" << endl;
cin >> method;
}
Run Code Online (Sandbox Code Playgroud)
我问的原因是当我运行这段代码时,它会进入一个无限循环,我无法理解为什么.
好我即时学习python和我试图制作这种文本游戏而且我坚持在循环中......我想要做的是有可以使用的东西列表,并将用户的raw_input与此列表进行比较,如果他们选择了正确的话5次尝试之一继续,否则死于消息.这是我的代码:
def die(why):
print why
exit(0)
#this is the list user's input is compared to
tools = ["paper", "gas", "lighter", "glass", "fuel"]
#empty list that users input is appended to
yrs = []
choice = None
print "You need to make fire"
while choice not in tools:
print "Enter what you would use:"
choice = raw_input("> ")
yrs.append(choice)
while yrs < 5:
print yrs
die("you tried too many times")
if choice in tools:
print "Well done, %s was what you …Run Code Online (Sandbox Code Playgroud) 我注意到在我的java程序中使用while循环时,使用布尔类型来终止它似乎不起作用.我打了一些简单的东西来测试它.代码通过i = 9完成,然后test打印为false.
public class LoopTesting {
public static void main(String[] args) {
boolean test = true;
while(test) {
for (int i = 1; i < 10; i++) {
System.out.println(i);
if(i == 5) test = false;
}
}
System.out.println(test);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:为了回应afzalex的答案,我测试了这段代码:
while(test) {
for (int i = 1; i < 10; i++) {
System.out.println(i);
}
test = false;
for(int i = 11; i < 20; i++) {
System.out.println(i);
}
}
Run Code Online (Sandbox Code Playgroud)
它打印到19.
我写了那段代码,我想在不使用的情况下迭代字符串s.length.我无法意识到while循环中的适当条件是什么.
int i = 0;
String s = "abcdefg";
while (s.charAt(i) != null) {
System.out.println(s.charAt(i));
i++;
}
Run Code Online (Sandbox Code Playgroud) 想象一下,Scanner传递任何字符串输入,如"11 22 ab 22",该方法应该计算所有数字的总和(55为提到的例子).我在这里编写了一些内容,但我无法跳过字符串.有人可以帮我吗?
System.out.println("Please enter any words and/or numbers: ");
String kbdInput = kbd.nextLine();
Scanner input = new Scanner(kbdInput);
addNumbers(input);
public static void addNumbers(Scanner input) {
double sum = 0;
while (input.hasNextDouble()) {
double nextNumber = input.nextDouble();
sum += nextNumber;
}
System.out.println("The total sum of the numbers from the file is " + sum);
}
Run Code Online (Sandbox Code Playgroud) 我试图让用户有机会在介绍产生错误的东西之后重复输入但是有些东西不起作用,因为一旦错误被捕获,尝试的东西不会再次被执行,而是直接转到捕获的东西产生永恒cicle.这是我的代码:
while (err==1){
err=0;
try{
dim = keyboard.nextInt();
} catch(Exception e){
System.out.println("Oops! What you entered is not an integer.");
err=1;
}
}
Run Code Online (Sandbox Code Playgroud) 我在3周前开始学习C语言,在学习while循环的同时我尝试构建一个附加程序,基本上你不断添加它添加的数字,在第二个数字之后它会给你一个小计,每次添加,如果你按0退出它给你最后的总和然后退出.现在我有一个主要的,一个额外的问题.主要问题是,我必须sum = 0在while函数之前使用,如果我在"while"之后使用它,它会给我输入的数字作为结果.现在我真的很想知道背后的想法是什么.当我像下面这样写它时,它将"sum与0"等同于开头,并在我输入另一个数字时更改值,或者它背后还有其他一些想法.
另外一个问题是,为什么我需要使用2 getchar();来让我的程序保持在屏幕上,为什么不一个?
#include <stdio.h>
int main(void)
{
float num;
float sum;
printf(" please enter a number +0 to start the program (0 to quit): \n");
scanf(" %f", &num);
sum =0; //THIS HERE**********************
while (num > 0)
{
printf("please enter integer:\n");
scanf("%f", &num);
sum = sum + num;
printf("current sum is = %f\n", sum);
}
printf("final sum is = %f\n", sum);
getchar();
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我try except在while循环中遇到问题.我还是比较新的编程,这是我的A2 Comp4项目的一部分.
def CheckValidInitials(initials):
CheckIfTrue = False
Count = 1
while CheckIfTrue == False:
while len(initials) == int(3) or len(initials) == int(4):
listInitials = list(initials)
print(len(initials))
while len(listInitials) - Count >= 0:
print(len(initials))
print(len(listInitials) - Count)
Count = Count + 1
print(listInitials)
try:
int(listInitials[Count])
except IndexError and ValueError:
CheckIfTrue = True
else:
print("One of your initials is a number, this is not valid")
print()
Count = 1
initials = input("Please enter valid initials: ")
listInitials …Run Code Online (Sandbox Code Playgroud) 我希望我的代码接受用户输入,然后提示用户继续,如果他们回答'y'然后它要求另一个输入,如果他们回答'n'程序停止并且如果他们键入任何其他字符它只是继续提示他们直到他们输入'y'或'n'.
正如代码所示,我正在尝试使用while循环来持续提示用户,直到他们输入'y'或'n'.但是当我到达while循环时,当输入'y'或'n'时它不会停止.
def test():
number = input('Input a number then press enter:')
print(number)
prompt = input('Continue (y/n)? ')
if prompt == 'y':
number = input('Input a number then press enter:')
print(number)
prompt = input('Continue (y/n)? ')
elif prompt == 'n':
pass
else:
while prompt != 'y' or 'n':
prompt = input('Continue (y/n)? ')
Run Code Online (Sandbox Code Playgroud) 嗨我一直在尝试在python 2.7中编写一个程序,它以一个单词作为输入并输出一个单词中的字母数.起初它正在工作,但发生了一些事情,现在它不断返回错误,第一行不是while循环的一部分.这是代码的一部分:
def number_of_letters(input):
nol = input.find(input[-1])
while input[nol:] != input[-1]:
nol = input.find(input[-1], input.find(input[-1] + 1)
nol = nol + 1
print nol
Run Code Online (Sandbox Code Playgroud)
python解释器通过我尝试在while块之后放置的任何东西(在这种情况下'nol = nol + 1')继续返回语法错误我试过玩它但没有任何效果.请帮忙.顺便说一句,如果有任何模块可以帮助这个程序,这将是伟大的,但我也想知道为什么这个不工作
while-loop ×10
java ×4
python ×4
try-catch ×2
c ×1
c++ ×1
if-statement ×1
loops ×1
nested-loops ×1
python-3.x ×1